Android写的一个设置图片查看器,可以调整透明度

先来看看效果吧:

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="match_parent"
 9         android:layout_height="wrap_content" >
10 
11         <Button
12             android:id="@+id/button1"
13             android:layout_width="wrap_content"
14             android:layout_height="wrap_content"
15             android:text="增大透明度" />
16 
17         <Button
18             android:id="@+id/button2"
19             android:layout_width="wrap_content"
20             android:layout_height="wrap_content"
21             android:text="减小透明度" />
22 
23         <Button
24             android:id="@+id/button3"
25             android:layout_width="wrap_content"
26             android:layout_height="wrap_content"
27             android:text="下一张" />
28     </LinearLayout>
29     <!-- 定义显示整体图片的ImageView -->
30 
31     <ImageView
32         android:id="@+id/imageView1"
33         android:layout_width="wrap_content"
34         android:layout_height="wrap_content"
35         android:background="#0000ff"
36         android:scaleType="fitCenter"
37         android:src="@drawable/shuangta" />
38     <!-- 定义显示局部图片的ImageView -->
39 
40     <ImageView
41         android:id="@+id/imageView2"
42         android:layout_width="wrap_content"
43         android:layout_height="wrap_content"
44         android:layout_marginTop="10dp"
45         android:background="#0000ff" />
46 
47 </LinearLayout>

其中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 }
原文地址:https://www.cnblogs.com/rollenholt/p/2506331.html