创建三种图片浏览器:
1.使用ImageView
step1:创建工程ImagesShow1,其中main.xml文件内容如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <LinearLayout 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:orientation="horizontal" 11 android:gravity="center_horizontal" 12 > 13 <Button 14 android:id="@+id/plus" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="@string/plus" 18 /> 19 <Button 20 android:id="@+id/minus" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:text="@string/minus" 24 /> 25 <Button 26 android:id="@+id/prior" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="@string/prior" 30 /> 31 <Button 32 android:id="@+id/next" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:text="@string/next" 36 /> 37 </LinearLayout> 38 39 <ImageView 40 android:id="@+id/image1" 41 android:layout_width="fill_parent" 42 android:layout_height="300dp" 43 android:src="@drawable/baiyang" 44 android:background="#ffffff" 45 /> 46 47 <ImageView 48 android:id="@+id/image2" 49 android:layout_width="120dp" 50 android:layout_height="120dp" 51 android:background="#ffffff" 52 /> 53 54 </LinearLayout>
step2:ImageShow1.java
1 packagecom.cb.imagesshow; 2 3 importandroid.app.Activity; 4 importandroid.graphics.Bitmap; 5 importandroid.graphics.BitmapFactory; 6 importandroid.graphics.drawable.BitmapDrawable; 7 importandroid.os.Bundle; 8 importandroid.view.MotionEvent; 9 importandroid.view.View; 10 importandroid.view.View.OnClickListener; 11 importandroid.view.View.OnTouchListener; 12 importandroid.widget.Button; 13 importandroid.widget.ImageView; 14 15 /* 16 * 这个图片浏览器使用 ImageView 完成,功能:局部放大. 17 * 存在的问题是:getWidth()获取了整个显示界面的宽,导致鼠标点击处获取的图片与局部放大中显示的图片位置有差异。怎么解决呢???求助。 18 */ 19 20 public class ImagesShow1 extendsActivity { 21 private int[] imageIds = new int[]{ 22 R.drawable.baiyang,R.drawable.chunv,R.drawable.jinniu, 23 R.drawable.juxie,R.drawable.mojie,R.drawable.sheshou, 24 R.drawable.shizi,R.drawable.shuangyu,R.drawable.shuangzi, 25 R.drawable.shuiping,R.drawable.tiancheng,R.drawable.tianxie 26 }; 27 private int currentImg = 0; 28 private int alpha = 255; 29 30 privateButton plus; 31 privateButton minus; 32 privateButton prior; 33 privateButton next; 34 35 privateImageView image1; 36 privateImageView image2; 37 38 @Override 39 public voidonCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.main); 42 43 plus =(Button)findViewById(R.id.plus); 44 minus =(Button)findViewById(R.id.minus); 45 prior =(Button)findViewById(R.id.prior); 46 next =(Button)findViewById(R.id.next); 47 48 image1 =(ImageView)findViewById(R.id.image1); 49 image2 =(ImageView)findViewById(R.id.image2); 50 51 //定义查看上/下一张图片的监听器 52 OnClickListener listener1 = newOnClickListener() { 53 @Override 54 public voidonClick(View v) { 55 if (v ==prior) { 56 if (currentImg <= 0) { 57 currentImg = 12; 58 } 59 } 60 if (v ==next) { 61 if (currentImg >= 11) { 62 currentImg = -1; 63 } 64 } 65 BitmapDrawable bitmapDrawable =(BitmapDrawable)image1.getDrawable(); 66 //如果图片还未回收,先强制回收该图片 67 if (!bitmapDrawable.getBitmap().isRecycled()) { 68 bitmapDrawable.getBitmap().recycle(); 69 } 70 //改变image1显示的图片 71 if (v ==prior) { 72 image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), imageIds[--currentImg])); 73 } 74 else if(v ==next){ 75 image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), imageIds[++currentImg])); 76 } 77 } 78 }; 79 prior.setOnClickListener(listener1); 80 next.setOnClickListener(listener1); 81 82 //定义增大/减小透明度的监听器 83 OnClickListener listener2 = newOnClickListener() { 84 @Override 85 public voidonClick(View v) { 86 if (v ==plus) { 87 alpha += 20; 88 } 89 if (v ==minus) { 90 alpha -= 20; 91 } 92 if (alpha > 255) { 93 alpha =255; 94 } 95 if (alpha < 0) { 96 alpha = 0; 97 } 98 image1.setAlpha(alpha); 99 } 100 }; 101 plus.setOnClickListener(listener2); 102 minus.setOnClickListener(listener2); 103 104 //设置在图片上点击的监听器 105 image1.setOnTouchListener(newOnTouchListener() { 106 @Override 107 public booleanonTouch(View v, MotionEvent event) { 108 BitmapDrawable bitmapDrawable =(BitmapDrawable)image1.getDrawable(); 109 //获取第一个图片显示框中的位图 110 Bitmap bitmap =bitmapDrawable.getBitmap(); 111 //bitmap图片实际大小与第一个ImageView的缩放比例 112 double scale = bitmap.getWidth()/320.0; 113 int x = (int)(event.getX() *scale); 114 int y = (int)(event.getY() *scale); 115 if (x > bitmap.getWidth() - 120) { 116 x = bitmap.getWidth() - 120; 117 } 118 if (y > bitmap.getHeight() - 120) { 119 y = bitmap.getHeight() - 120; 120 } 121 image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120)); 122 image2.setAlpha(alpha); 123 return false; 124 } 125 }); 126 } 127 }
step3:效果如图
2.使用GridView和ImageSwitcher
step1:创建工程ShowImages,其中main.xml文件内容如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" 6 android:gravity="center_horizontal"> 7 8 <GridView 9 android:id="@+id/grid01" 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" 12 13 android:numColumns="4" 14 android:gravity="center" 15 /> 16 <ImageSwitcher 17 android:id="@+id/switcher" 18 android:layout_width="320dp" 19 android:layout_height="320dp" 20 android:gravity="center_horizontal" 21 /> 22 23 24 </LinearLayout>
另外,SimpleAdapter中的布局文件cell.xml为:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="horizontal" 6 android:gravity="center"> 7 8 <ImageView 9 android:id="@+id/image1" 10 android:layout_width="40dp" 11 android:layout_height="40dp" 12 /> 13 14 </LinearLayout>
step2:ShowImages.java
1 packagecom.cb.showimages; 2 3 importjava.util.ArrayList; 4 importjava.util.HashMap; 5 importjava.util.List; 6 importjava.util.Map; 7 8 importandroid.R.anim; 9 importandroid.app.Activity; 10 importandroid.os.Bundle; 11 importandroid.view.View; 12 importandroid.view.ViewGroup.LayoutParams; 13 importandroid.view.animation.Animation; 14 importandroid.view.animation.AnimationUtils; 15 importandroid.widget.AdapterView; 16 importandroid.widget.AdapterView.OnItemClickListener; 17 importandroid.widget.AdapterView.OnItemSelectedListener; 18 importandroid.widget.GridView; 19 importandroid.widget.ImageSwitcher; 20 importandroid.widget.ImageView; 21 importandroid.widget.SimpleAdapter; 22 importandroid.widget.ViewSwitcher.ViewFactory; 23 /* 24 * This project uses GridView and ImageSwitcher. 25 * 功能:大图预览 26 */ 27 public class ShowImages extendsActivity { 28 private int[] imageIds = new int[]{ 29 R.drawable.sunset,R.drawable.water,R.drawable.winter,R.drawable.sunset, 30 R.drawable.water,R.drawable.winter,R.drawable.sunset,R.drawable.water, 31 R.drawable.winter,R.drawable.sunset,R.drawable.water,R.drawable.winter, 32 R.drawable.sunset,R.drawable.water,R.drawable.winter,R.drawable.sunset 33 }; 34 35 privateGridView mGridView; 36 privateImageSwitcher switcher; 37 38 @Override 39 public voidonCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.main); 42 43 /* 44 * 1.创建List对象集合,其元素为Map对象 45 * 2.使用Map对象来存储单个元素--图像 46 * 3.将Map对象存入List对象中 47 */ 48 List<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>(); 49 for (int i = 0; i < imageIds.length; i++) { 50 Map<String, Object> listItem = new HashMap<String, Object>(); 51 listItem.put("images", imageIds[i]); 52 listItems.add(listItem); 53 } 54 55 switcher =(ImageSwitcher)findViewById(R.id.switcher); 56 //设置图片更换的动画效果 57 switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); 58 switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); 59 60 //为ImageSwitcher设置图片切换的动画效果。ImageSwitcher对象负责显示makeView()方法返回的imageView对象. 61 switcher.setFactory(newViewFactory() { 62 63 @Override 64 publicView makeView() { 65 ImageView imageView = new ImageView(ShowImages.this); 66 imageView.setBackgroundColor(0xff0000); 67 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 68 imageView.setLayoutParams(newImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 69 returnimageView; 70 } 71 }); 72 //创建SimpleAdapter 73 SimpleAdapter simpleAdapter = newSimpleAdapter( 74 this, 75 listItems, 76 R.layout.cell, //该布局定义List对象中各个item的布局 77 new String[]{"images"}, 78 new int[]{R.id.image1} 79 ); 80 mGridView =(GridView)findViewById(R.id.grid01); 81 mGridView.setAdapter(simpleAdapter); 82 83 //定义列表项被单击的监听器 84 mGridView.setOnItemClickListener(newOnItemClickListener() { 85 86 @Override 87 public void onItemClick(AdapterView<?>parent, View view, 88 int position, longid) { 89 //显示当前被选中的图片 90 switcher.setImageResource(imageIds[position %imageIds.length]); 91 } 92 }); 93 //定义列表项被选中的监听器 94 mGridView.setOnItemSelectedListener(newOnItemSelectedListener() { 95 96 @Override 97 public void onItemSelected(AdapterView<?>parent, View view, 98 int position, longid) { 99 switcher.setImageResource(imageIds[position %imageIds.length]); 100 } 101 102 @Override 103 public void onNothingSelected(AdapterView<?>parent) {} 104 105 }); 106 107 } 108 }
step3:效果如图:
3.使用Gallery和ImageSwitcher
step1:创建工程GalleryTest,其中main.xml文件内容如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent"> 6 <!--定义一个ImageSwitcher组件 --> 7 <ImageSwitcher android:id="@+id/switcher" 8 android:layout_width="320dp" 9 android:layout_height="320dp" 10 /> 11 <!--定义一个Gallery组件 --> 12 <Gallery android:id="@+id/gallery" 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content" 15 android:layout_marginTop="25dp" 16 android:unselectedAlpha="0.6" 17 android:spacing="3pt" 18 /> 19 </LinearLayout>
step2:GalleryTest.java
1 packageorg.crazyit.gallery; 2 3 importandroid.app.Activity; 4 importandroid.content.res.TypedArray; 5 importandroid.os.Bundle; 6 importandroid.view.View; 7 importandroid.view.ViewGroup; 8 importandroid.view.animation.AnimationUtils; 9 importandroid.widget.AdapterView.OnItemSelectedListener; 10 importandroid.widget.AdapterView; 11 importandroid.widget.BaseAdapter; 12 importandroid.widget.Gallery; 13 importandroid.widget.ImageSwitcher; 14 importandroid.widget.ImageView; 15 importandroid.widget.Gallery.LayoutParams; 16 importandroid.widget.ViewSwitcher.ViewFactory; 17 18 public class GalleryTest extendsActivity 19 { 20 int[] imageIds = new int[] 21 { 22 R.drawable.shuangzi, R.drawable.shuangyu, R.drawable.chunv, R.drawable.tiancheng, 23 R.drawable.tianxie, R.drawable.sheshou, R.drawable.juxie, R.drawable.shuiping, 24 R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu, R.drawable.mojie 25 }; 26 27 privateGallery gallery; 28 privateImageSwitcher switcher; 29 30 @Override 31 public voidonCreate(Bundle savedInstanceState) 32 { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.main); 35 36 gallery =(Gallery) findViewById(R.id.gallery); 37 switcher =(ImageSwitcher)findViewById(R.id.switcher); 38 39 switcher.setFactory(newViewFactory() 40 { 41 @Override 42 publicView makeView() 43 { 44 ImageView imageView = new ImageView(GalleryTest.this); 45 imageView.setBackgroundColor(0xff0000); 46 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 47 imageView.setLayoutParams(newImageSwitcher.LayoutParams( 48 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 49 returnimageView; 50 } 51 }); 52 //设置图片更换的动画效果 53 switcher.setInAnimation(AnimationUtils.loadAnimation(this, 54 android.R.anim.fade_in)); 55 switcher.setOutAnimation(AnimationUtils.loadAnimation(this, 56 android.R.anim.fade_out)); 57 //创建一个BaseAdapter对象,该对象负责提供Gallery所显示的图片 58 BaseAdapter adapter = newBaseAdapter() 59 { 60 @Override 61 public intgetCount() 62 { 63 returnimageIds.length; 64 } 65 @Override 66 public Object getItem(intposition) 67 { 68 returnposition; 69 } 70 @Override 71 public long getItemId(intposition) 72 { 73 returnposition; 74 } 75 76 //该方法的返回的View就是代表了每个列表项 77 @Override 78 public View getView(intposition, View convertView, ViewGroup parent) 79 { 80 //创建一个ImageView 81 ImageView imageView = new ImageView(GalleryTest.this); 82 imageView.setImageResource(imageIds[position %imageIds.length]); 83 //设置ImageView的缩放类型 84 imageView.setScaleType(ImageView.ScaleType.FIT_XY); 85 imageView.setLayoutParams(new Gallery.LayoutParams(75, 100)); 86 TypedArray typedArray =obtainStyledAttributes(R.styleable.Gallery); 87 imageView.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0)); 88 returnimageView; 89 } 90 }; 91 gallery.setAdapter(adapter); 92 gallery.setOnItemSelectedListener(newOnItemSelectedListener() 93 { 94 //当Gallery选中项发生改变时触发该方法 95 @Override 96 public void onItemSelected(AdapterView<?>parent, View view, 97 int position, longid) 98 { 99 switcher.setImageResource(imageIds[position %imageIds.length]); 100 } 101 102 @Override 103 public void onNothingSelected(AdapterView<?>parent){} 104 }); 105 } 106 }
step3:效果如图: