布局文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity = "center">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/btn1_text"/>"
<Button
android:id = "@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2_text"/>
<Button
android:id = "@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn3"/>
</LinearLayout>
<ImageView
android:id = "@+id/imageview1"
android:layout_width = "fill_parent"
android:background = "#0000ff"
android:layout_height = "240px"
android:src = "@drawable/shuangta"
android:scaleType="fitCenter"/>
<ImageView
android:id = "@+id/imageview2"
android:layout_width="120dp"
android:layout_height = "120dp"
android:background = "#0000ff"
android:layout_marginTop = "10dp"/>
</LinearLayout>
代码:
<pre name="code" class="java">package com.example.imageviewtest2;
import android.os.Bundle;
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 image[] = new int[]{R.drawable.qiao,R.drawable.shuangta,R.drawable.lijiang,R.drawable.shui};
int currentImage = 2;
private int alpha = 255;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn1 = (Button)findViewById(R.id.btn1);
final Button btn2 = (Button)findViewById(R.id.btn2);
final ImageView image1 = (ImageView)findViewById(R.id.imageview1);
final ImageView image2= (ImageView)findViewById(R.id.imageview2);
final Button btn3 = (Button)findViewById(R.id.btn3);
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(currentImage >= 4)
{
currentImage = 0;
}
BitmapDrawable bitmapDrawable = (BitmapDrawable)image1.getDrawable();
if(!bitmapDrawable.getBitmap().isRecycled())
{
bitmapDrawable.getBitmap().recycle();
}
image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), image[currentImage]));
currentImage ++;
}
});
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0 == btn1)
{
alpha += 20;
}
if(arg0 == btn2)
{
alpha -= 20;
}
if(alpha >= 255)
{
alpha = 255;
}
if(alpha <= 0)
{
alpha = 0;
}
image1.setAlpha(alpha);
}
};
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);
image1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
BitmapDrawable bitmapDrawable = (BitmapDrawable)image1.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
double scale = bitmap.getWidth() / 320.0;
int x = (int)(arg1.getX()*scale);
int y = (int)(arg1.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;
}
});
}
@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;
}
}