vue.js合作做网站么,江西 网站 建设 开发,微信登录网页版登录入口,洛阳网站建设首选洛阳铭信科技Gallery组件主要用于横向显示图像列表#xff0c;不过按常规做法。Gallery组件只能有限地显示指定的图像。也就是说#xff0c;如果为Gallery组件指定了10张图像#xff0c;那么当Gallery组件显示到第10张时#xff0c;就不会再继续显示了。这虽然在大多数时候没有什么关系… Gallery组件主要用于横向显示图像列表不过按常规做法。Gallery组件只能有限地显示指定的图像。也就是说如果为Gallery组件指定了10张图像那么当Gallery组件显示到第10张时就不会再继续显示了。这虽然在大多数时候没有什么关系但在某些情况下我们希望图像显示到最后一张时再重第1张开始显示也就是循环显示。要实现这种风格的Gallery组件就需要对Gallery的Adapter对象进行一番改进。 以下通过Gallery模拟循环显示图像在单击某一个Gallery组件中的图像时在下方显示一个放大的图像使用ImageSwitcher组件。 最终效果图如下 一、Layout布局文件 A:主界面布局文件 View Code ?xml version1.0 encodingutf-8?LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:orientationvertical android:layout_widthfill_parent android:layout_heightfill_parentImageButton android:idid/ibtnHead android:layout_height60px android:layout_width60px android:srcdrawable/icon android:scaleTypefitXY/ImageButton/LinearLayout B:显示Gallery和ImageSwitcher的布局文件 View Code ?xml version1.0 encodingutf-8?RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:orientationvertical android:layout_widthfill_parent android:layout_heightfill_parentGallery android:idid/img_gallery android:layout_widthfill_parent android:layout_height110px android:layout_marginTop2px android:layout_alignParentLefttrue/GalleryImageSwitcher android:idid/img_switcher android:layout_width90px android:layout_height90px android:layout_centerHorizontaltrue android:layout_belowid/img_gallery/ImageSwitcher/RelativeLayout C:String文件 View Code ?xml version1.0 encodingutf-8?resourcesstring nameaddContact_PleaseChooseImg请选择图像/string/resources 二、设置图片资源 在drawable-hdpi文件夹下放置10个要显示的图片如下图 三、主Activity类 View Code package cn.moon.contact;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageButton;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ImageView.ScaleType;import android.widget.ViewSwitcher.ViewFactory;public class AddContactActivity extends Activity { ImageButton ibtnImgChoose; AlertDialog imgChooseDialog; Gallery gallery; ImageSwitcher imageSwitcher;int selectedImage;private int[] images { R.drawable.png0001, R.drawable.png0002, R.drawable.png0003, R.drawable.png0004, R.drawable.png0005, R.drawable.png0006, R.drawable.png0007, R.drawable.png0008, R.drawable.png0009, R.drawable.png0010, };public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.addcontact); System.out.println(AddContact); ibtnImgChoose (ImageButton) findViewById(R.id.ibtnHead); ibtnImgChoose.setOnClickListener(new View.OnClickListener() {public void onClick(View v) { initImageChoose(); imgChooseDialog.show(); } }); }private void initImageChoose() { LayoutInflater layoutInflater LayoutInflater.from(this); View view layoutInflater.inflate(R.layout.userheadchoose, null); gallery (Gallery) view.findViewById(R.id.img_gallery); gallery.setAdapter(new imageAdapter(this)); gallery.setSelection(images.length / 2); imageSwitcher (ImageSwitcher) view.findViewById(R.id.img_switcher); imageSwitcher.setFactory(new switcherFactory(this)); gallery.setOnItemSelectedListener(new OnItemSelectedListener() {public void onItemSelected(AdapterView? arg0, View arg1,int arg2, long arg3) { imageSwitcher.setImageResource(images[arg2]); selectedImage arg2; }public void onNothingSelected(AdapterView? arg0) {// TODO Auto-generated method stub } }); AlertDialog.Builder builder new AlertDialog.Builder(this); builder.setTitle(请选择图像); builder.setPositiveButton(确认, new OnClickListener() {public void onClick(DialogInterface dialog, int which) { ibtnImgChoose.setImageResource(images[selectedImage]); } }); builder.setNegativeButton(取消, new OnClickListener() {public void onClick(DialogInterface dialog, int which) { } }); builder.setView(view); imgChooseDialog builder.create(); }class imageAdapter extends BaseAdapter {private Context context;public imageAdapter(Context context) {super();this.context context; }public int getCount() {return images.length; }public Object getItem(int arg0) {// TODO Auto-generated method stub return null; }public long getItemId(int position) {// TODO Auto-generated method stub return 0; }public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView new ImageView(context);// 构造一个ImageView imageView.setImageResource(images[position]);// 设置ImageView图片源 imageView.setAdjustViewBounds(true); imageView.setScaleType(ScaleType.FIT_XY);// 自适应高和宽 imageView.setLayoutParams(new Gallery.LayoutParams(80, 80));// 设置显示图处的大小 imageView.setPadding(10, 5, 10, 5);// 设置四边的距离 return imageView; } }class switcherFactory implements ViewFactory {private Context context;public switcherFactory(Context context) {super();this.context context; }public View makeView() { ImageView imageView new ImageView(context); imageView.setLayoutParams(new ImageSwitcher.LayoutParams(90, 90));return imageView; } }} 转载于:https://www.cnblogs.com/ycmoon/archive/2011/04/12/2013497.html