package androidstudy.matrix;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class maxtrix extends Activity {
private ImageView mImageView;
private Button mButton1;
private Button mButton2;
private RelativeLayout layout1;
private Bitmap bmp;
private int id = 0;
private int displayWidth;
private int displayHeight;
private float scaleWidth = 1;
private float scaleHeight = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得屏幕分辨率大小
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayWidth = dm.widthPixels;
displayHeight = dm.heightPixels - 80;
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.q2);
mImageView = (ImageView) findViewById(R.id.imageView1);
layout1 = (RelativeLayout) findViewById(R.id.layout1);
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
small();
}
});
mButton2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
big();
}
});
}
public void small() {
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
double scale = 0.8;
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,
matrix, true);
if (id == 0) {// 第一次就删除原来的Imageview
layout1.removeView(mImageView);
} else {// 不是第一次删除上一次产生的ImageView
layout1.removeView((ImageView) findViewById(id));
}
id++;
ImageView imageView = new ImageView(maxtrix.this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);
// 图片最大的时候。放大按钮disable,缩小的时候设为enable
mButton2.setEnabled(true);
}
public void big() {
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
double scale = 1.25;
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,
matrix, true);
if (id == 0) {// 第一次就删除原来的Imageview
layout1.removeView(mImageView);
} else {// 不是第一次删除上一次产生的ImageView
layout1.removeView((ImageView) findViewById(id));
}
id++;
ImageView imageView = new ImageView(maxtrix.this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);
// 如果在放大图片就超出屏幕,就把Button disable
if (scaleWidth * scale * bmpWidth > displayWidth
|| scaleHeight * scale * bmpHeight > displayHeight)
mButton2.setEnabled(false);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button1"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
/>
<Button
android:id="@+id/button2"
android:layout_toRightOf="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:text="Button"
/>
</RelativeLayout>