改变图片的透明度,可以通过ImageView的setApha方法来实现。
查看图片和改变图片的透明度,需要定义两个ImageView,一个用于查看图片整体,一个用于查看图片局部细节。
效果如下:
界面布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<Button
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="—" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一个" />
</LinearLayout>
<!-- 定义显示图片整体的ImageView -->
<ImageView
android:id="@+id/image1"
android:layout_width="350px"
android:layout_height="250px"
android:background="#0000"
android:src="@drawable/a"
android:scaleType="fitCenter"/>
<!-- fintCenter表明ImageView -->
<!-- 定义线束图片局部细节的imageview -->
<ImageView
android:id="@+id/image2"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#0000ff"
android:layout_marginTop="10dp" />
<!-- layout_marginTop表示与上面距离 -->
</LinearLayout>
为了能动态改变图片的透明度,需要为按钮写事件监听,当用户单击时候改变图片的Alpha值。为了能动态显示图片的局部细节,程序为第一个ImageView添加OnTouchListen,用户在第一个Imageview上发生触摸事件时,程序从原始图片中读取相应部分的图片,并将其显示在第二个ImageView中。
主程序代码:
package com.example.picexplorer;
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.Menu;
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 {
int[] images=new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};
//定义默认显示的图片
int currentImg=2;
//定义图片的初始透明度
private int alpha=255;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button plus=(Button)findViewById(R.id.plus);
final Button minus=(Button)findViewById(R.id.minus);
final Button next=(Button)findViewById(R.id.next);
final ImageView image1=(ImageView)findViewById(R.id.image1);
final ImageView image2=(ImageView)findViewById(R.id.image2);
//定义查看限一张图片的监听器
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (currentImg>=3) {
currentImg=-1;
}
BitmapDrawable bitmapDrawable=(BitmapDrawable)image1.getDrawable();
//如果图片还没有回收,强制先回收
if (!bitmapDrawable.getBitmap().isRecycled()) {
bitmapDrawable.getBitmap().recycle();
}
//改变image显示的图片
image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), images[++currentImg]));
}
});
//定义改变图片透明度的办法
OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View v) {
if (v==plus) {
alpha+=20;
}
if (v==minus) {
alpha-=20;
}
if (alpha>=255) {
alpha=255;
}
if (alpha<=0) {
alpha=0;
}
//改变图片的透明度
image1.setAlpha(alpha);
}
};
//为两个按钮添加监听器
plus.setOnClickListener(listener);
minus.setOnClickListener(listener);
image1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
BitmapDrawable bitmapDrawable=(BitmapDrawable)image1.getDrawable();
//获取第一个图片显示框中的位图
Bitmap bitmap=bitmapDrawable.getBitmap();
//bitmap图片实际 大小与第一个imageview的缩放比例
double scale=bitmap.getWidth()/320.0;
//获取从需要显示的图片的开始点
int x=(int)(event.getX()*scale);
int y=(int)(event.getY()*scale);
if (x+120>bitmap.getWidth()) {
x=bitmap.getWidth()-120;
}
if (x+120>bitmap.getWidth()) {
x=bitmap.getWidth()-120;
}
//显示图片的指定区域
image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
image2.setAlpha(alpha);
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
用到了Bitmap类,它是一个代表位图的类,调用它的creatBitmap()静态方法即可截取位图的指定部分,返回截取区域生成的新位图。