本文共 4408 字,大约阅读时间需要 14 分钟。
先来看看效果吧:
main.xml代码如下:
1 26 7 10 11 29 30 3116 17 22 23 28 38 39 40 46 47
其中java代码为:
1 package android.demo; 2 3 import android.app.Activity; 4 import android.graphics.Bitmap; 5 import android.graphics.BitmapFactory; 6 import android.graphics.drawable.BitmapDrawable; 7 import android.os.Bundle; 8 import android.view.MotionEvent; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.view.View.OnTouchListener; 12 import android.widget.Button; 13 import android.widget.ImageView; 14 15 public class AndroidDemo5Activity extends Activity { 16 // 定义一个访问图片的数组 17 int[] images = new int[] { R.drawable.lijiang, R.drawable.qiao, 18 R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi, 19 R.drawable.ic_launcher, }; 20 // 定义当前显示的图片 21 int currentImage = 2; 22 // 定义图片的初始透明度 23 private int alpha = 255; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 // TODO Auto-generated method stub 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.main); 30 final Button plusButton = (Button) findViewById(R.id.button1); 31 final Button minuxButton = (Button) findViewById(R.id.button2); 32 final Button nextButton = (Button) findViewById(R.id.button3); 33 34 final ImageView imageview1 = (ImageView) findViewById(R.id.imageView1); 35 final ImageView imageview2 = (ImageView) findViewById(R.id.imageView2); 36 37 // 定义查看下一张图片的时间监听器 38 nextButton.setOnClickListener(new OnClickListener() { 39 40 @Override 41 public void onClick(View v) { 42 if (currentImage >= 5) { 43 currentImage = -1; 44 } 45 BitmapDrawable bitmap = (BitmapDrawable) imageview1 46 .getDrawable(); 47 // 如果图片还没有回收,先强制回收图片 48 if (!bitmap.getBitmap().isRecycled()) { 49 bitmap.getBitmap().recycle(); 50 } 51 // 改变ImageView的图片 52 imageview1.setImageBitmap(BitmapFactory.decodeResource( 53 getResources(), images[++currentImage])); 54 } 55 }); 56 57 // 定义改变图片透明度的方法 58 OnClickListener listener = new OnClickListener() { 59 60 @Override 61 public void onClick(View v) { 62 if (v == plusButton) { 63 alpha += 20; 64 } 65 if (v == minuxButton) { 66 alpha -= 20; 67 } 68 if (alpha > 255) { 69 alpha = 255; 70 } 71 if (alpha <= 0) { 72 alpha = 0; 73 } 74 // 改变图片的透明度 75 imageview1.setAlpha(alpha); 76 77 } 78 }; 79 80 // 为2个按钮添加监听器 81 plusButton.setOnClickListener(listener); 82 minuxButton.setOnClickListener(listener); 83 imageview1.setOnTouchListener(new OnTouchListener() { 84 85 @Override 86 public boolean onTouch(View arg0, MotionEvent arg1) { 87 // TODO Auto-generated method stub 88 BitmapDrawable bitmapDeaw = (BitmapDrawable) imageview1 89 .getDrawable(); 90 // 获取第一个图片显示框中的位图 91 Bitmap bitmap = bitmapDeaw.getBitmap(); 92 double scale = bitmap.getWidth(); 93 // 或许需要显示图片的开始点 94 int x = (int) (arg1.getX() * scale); 95 int y = (int) (arg1.getY() * scale); 96 if (x + 120 > bitmap.getWidth()) { 97 x = bitmap.getWidth() - 120; 98 } 99 if (y + 120 > bitmap.getHeight()) {100 y = bitmap.getHeight() - 120;101 }102 103 // 显示图片的指定区域104 imageview2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y,105 120, 120));106 imageview2.setAlpha(alpha);107 return false;108 }109 });110 }111 112 }
转载地址:http://mpxwm.baihongyu.com/