android圖片縮小和放大Matrix

public class ex04_22 extends Activity{
private ImageView mImageView;
private Button btn1,btn2;
private TextView mTextView;
private AbsoluteLayout layout1;
private Bitmap bmp;
private int id=0;
private int displayWidth,displayHeight;
private float scaleWidth=1,scaleHeight=1;
private final static String filename="/data/data/ex04_22.lcs/ex04_22_2.png";
@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(this.getResources(),R.drawable.ex04_22_1);
layout1=(AbsoluteLayout)findViewById(R.id.layout1);
mImageView=(ImageView)findViewById(R.id.myImageView);
btn1=(Button)findViewById(R.id.myButton1);
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
small();
}
});
btn2=(Button)findViewById(R.id.myButton2);
btn2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
big();
}
});
}
private void small(){
//獲得Bitmap的高和寬
int bmpWidth=bmp.getWidth();
int bmpHeight=bmp.getHeight();
//設置縮小比例
double scale=0.8;
//計算出這次要縮小的比例
scaleWidth=(float)(scaleWidth*scale);
scaleHeight=(float)(scaleHeight*scale);
//產生resize後的Bitmap對象
Matrix matrix=new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
if(id==0){
layout1.removeView(mImageView);
}
else{
layout1.removeView((ImageView)findViewById(id));
}
id++;
ImageView imageView=new ImageView(this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);
btn2.setEnabled(true);
}
private void big(){
//獲得Bitmap的高和寬
int bmpWidth=bmp.getWidth();
int bmpHeight=bmp.getHeight();
//設置縮小比例
double scale=1.25;
//計算出這次要縮小的比例
scaleWidth=(float)(scaleWidth*scale);
scaleHeight=(float)(scaleHeight*scale);
//產生resize後的Bitmap對象
Matrix matrix=new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
if(id==0){
layout1.removeView(mImageView);
}
else{
layout1.removeView((ImageView)findViewById(id));
}
id++;
ImageView imageView=new ImageView(this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);
if(scaleWidth*scale*bmpWidth>displayWidth||scaleHeight*scale*scaleHeight>displayHeight){
btn2.setEnabled(false);
}
}
}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="@+id/myImageView"
android:layout_width="200px"
android:layout_height="150px"
android:src="@drawable/ex04_22_1"
android:layout_x="0px"
android:layout_y="0px"
>
</ImageView>
<Button
android:id="@+id/myButton1"
android:layout_width="90px"
android:layout_height="60px"
android:text="縮小"
android:textSize="18sp"
android:layout_x="20px"
android:layout_y="372px"
>
</Button>
<Button
android:id="@+id/myButton2"
android:layout_width="90px"
android:layout_height="60px"
android:text="放大"
android:textSize="18sp"
android:layout_x="210px"
android:layout_y="372px"
>
</Button>
</AbsoluteLayout>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章