MainAcitivity.java
package com.example.imageviewtest2;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
// private float alpha = 1000;
// 定义图片资源数组
private int[] Images = { R.drawable.hills, R.drawable.winter,
R.drawable.sunset, R.drawable.xiangbi, R.drawable.shui };
private int currentImg = 0;
private ImageView imageView;
private ImageView imageView2;
private Button button3;
private Button button2;
private Button button1;
private int alpha = 255;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取Button控件
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
imageView = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
button3.setOnClickListener(listener);
button2.setOnClickListener(listener);
button1.setOnClickListener(listener);
imageView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView
.getDrawable();
// 获取第一个图片显示框中的位图
Bitmap bitmap = bitmapDrawable.getBitmap();
// bitMap图片实际大小与第一个ImageView的缩放比例
double scale = bitmap.getWidth() / 320;
// 获取需要显示的图片的开始点
int x = (int) (event.getX() * scale);
int y = (int) (event.getY() * scale);
if (x + 120 > bitmap.getWidth()) {
x = bitmap.getWidth() - 120;
}
if (y + 120 > bitmap.getHeight()) {
y = bitmap.getHeight() - 120;
}
// 显示图片的制定区域
imageView2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y,
120, 120));
imageView2.setAlpha(alpha);
return false;
}
});
}
private OnClickListener listener = new OnClickListener() {
@SuppressLint("ParserError")
public void onClick(View v) {
if (v == button3) {
if (currentImg >= 4) {
currentImg = -1;
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView
.getDrawable();
if (!bitmapDrawable.getBitmap().isRecycled()) {
bitmapDrawable.getBitmap().recycle();
}
// 改变ImageView显示的图片
imageView.setImageBitmap(BitmapFactory.decodeResource(
getResources(), Images[++currentImg]));
}
if (v == button2) {
alpha -= 20;
if (alpha <= 0) {
alpha = 0;
}
}
if (v == button1) {
alpha += 20;
if (alpha >= 255) {
alpha = 255;
}
}
imageView.setAlpha(alpha);
};
};
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加透明度" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减透明度" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下张图片" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="@drawable/sunset" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/sunset" />
</LinearLayout>