Java代码
package com.android.myandroid;
import android.R.integer;
import android.app.Activity;
import android.graphics.Bitmap;
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;
import android.widget.Toast;
public class MainActivity extends Activity {
//定义一个访问图片的数组
int[] images = new int []
{R.drawable.de8,R.drawable.de9,R.drawable.de10,R.drawable.de11,R.drawable.de12,R.drawable.de13,
R.drawable.de14,R.drawable.de15,R.drawable.de16,R.drawable.de17,R.drawable.de18,R.drawable.de19,
R.drawable.de20,R.drawable.de21,R.drawable.de22,R.drawable.de23,R.drawable.de24,R.drawable.de25,R.drawable.de27,
R.drawable.de27
};
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.button1);
final Button minus = (Button)findViewById(R.id.button2);
final Button next = (Button)findViewById(R.id.button3);
final ImageView image1 = (ImageView)findViewById(R.id.imageView1);
final ImageView image2 = (ImageView)findViewById(R.id.imageView2);
image1.setImageResource(images[2]);
next.setOnClickListener(new OnClickListener()
{
public void onClick(View v){
Toast toast =Toast.makeText(MainActivity.this, "少女时代!", 3000);
toast.show();
image1.setImageResource(images[++currentimg%images.length]);
}
});
//定义改变透明度、alpha是图片的透明度 取值在0--250之间、数值越大透明度越高
OnClickListener listener = new OnClickListener(){
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) {
// TODO Auto-generated method stub
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(y+120>bitmap.getHeight()){
y=bitmap.getHeight()-120;
}
image2.setImageBitmap(Bitmap.createBitmap(bitmap,x,y,120,120));
image2.setAlpha(alpha);
return false;
}
});
}
}
Xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >
<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="fill_parent"
android:layout_height="240dp"
android:gravity="center" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="3dp"
android:background="#0f0" />
</LinearLayout>
效果图