Android 电子相册http://blog.sina.com.cn/s/blog_9968251c01014uu9.html

  •   基本思路

    gallery选中的图片在ImageView中显示出来,注意ImageView的一个View实际上就是一个ImageViewgllery的一个View也是一个ImageView

  • 关键代码

         对于Gallery

1.         相册循环

    public int getCount() {

       return Integer.MAX_VALUE;  //通过这个来进行循环

}

2.         制作View

 public View getView(int position, View convertView, ViewGroup parent) {

           // TODO Auto-generated method stub

           ImageView image=new ImageView(context);

       image.setImageResource(preImages[position%preImages.length]);  //通过这个来进行循环

           image.setScaleType(ImageView.ScaleType.FIT_XY);

           image.setLayoutParams(new Gallery.LayoutParams(200, 120));

image.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));

           return image;

     }

3.         设置View的背景

 typedArray = obtainStyledAttributes(R.styleable.Gallery);

image.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));

对于ImageView

1.         设置View

 //设置ImageSwitcher中的ImageView

    switcher.setFactory(new ViewFactory(){

       public View makeView() {

           ImageView image=new ImageView(Scan_pic.this);

           image.setScaleType(ImageView.ScaleType.FIT_CENTER);

           image.setLayoutParams(newImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

           return image;

       }

      });

  •    完整代码

Gallery相册的每一张相片的背景style的设置:

res/value/attr.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <declare-styleable name="Gallery"> 

        <attr name="android:galleryItemBackground"/> 

    </declare-styleable>   

</resources>

 

 

 public class Scan_pic extends Activity {

    private ImageSwitcher switcher;

    private Gallery gallery;

    private int[] preImages;

    private int[] afterImages;

    private int postion;

     public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.scanpic);

           

           

            switcher=(ImageSwitcher)findViewById(R.id.imageSwitcher_scan_pic);

            gallery=(Gallery)findViewById(R.id.gallery1_scan_pic);

            afterImages=new int[]{R.drawable.after0,R.drawable.after1,R.drawable.after2,R.drawable.after3};

            preImages=new int[]{R.drawable.pre0,R.drawable.pre1,R.drawable.pre2,R.drawable.pre3};

            gallery.setAdapter(new ImageAdapter(preImages,Scan_pic.this));

            //设置gallery的项单击事件,更换ImagerSwitcher中的图片

            gallery.setOnItemClickListener(new OnItemClickListener(){

 

              public void onItemClick(AdapterView<?> arg0, View arg1,

                     int arg2, long arg3) {

                  // TODO Auto-generated method stub

                  switcher.setImageResource(preImages[arg2%preImages.length]);

                  postion=arg2%preImages.length;

              }          

            });

           

            //设置ImageSwitcher中的ImageView

            switcher.setFactory(new ViewFactory(){

              public View makeView() {

                  // TODO Auto-generated method stub

                  ImageView image=new ImageView(Scan_pic.this);

                  image.setScaleType(ImageView.ScaleType.FIT_CENTER);

                  image.setLayoutParams(newImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

                  return image;

              }

           

            });

            switcher.setOnClickListener(new OnClickListener(){

              public void onClick(View v) {

                  // TODO Auto-generated method stub

                  Intent intent=new Intent(Scan_pic.this,StartGame.class);

                  intent.putExtra("position", postion);

                  startActivity(intent);

              }          

            });

     }

     

     class ImageAdapter extends BaseAdapter{

        int[] preImages;

        Context context;

        TypedArray typedArray;

        public ImageAdapter(int[] preImages,Context context){

            this.preImages=preImages;

            this.context=context;

            typedArray = obtainStyledAttributes(R.styleable.Gallery); //设置Gallery中的ImageView即每一项的背景

        }

       public int getCount() {

           // TODO Auto-generated method stub

           return Integer.MAX_VALUE;  //通过这个来进行循环

       }

       public Object getItem(int position) {

           // TODO Auto-generated method stub

           return position;

       }

       public long getItemId(int position) {

           // TODO Auto-generated method stub

           return position;

       }

       public View getView(int position, View convertView, ViewGroup parent) {

           // TODO Auto-generated method stub

           ImageView image=new ImageView(context);

           image.setImageResource(preImages[position%preImages.length]);  //通过这个来进行循环

           image.setScaleType(ImageView.ScaleType.FIT_XY);

           image.setLayoutParams(new Gallery.LayoutParams(200, 120));

      image.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));

           return image;

       }

     }

}


http://blog.sina.com.cn/s/blog_9968251c01014uu9.html

你可能感兴趣的:(Android 电子相册http://blog.sina.com.cn/s/blog_9968251c01014uu9.html)