2012年8月18日 星期六
android -- ImageView
Matrix - 放大和縮小
public class Cq_Matrix01Activity extends Activity{
/** Called when the activity is first created. */
private int bmpWidth;
private int bmpHeight;
private Bitmap bitmap;
//private LinearLayout linearLayout;
private ImageView mImageView;
private Button btnSmall;
private Button btnReset;
private Button btnLarge;
private float scale;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageView = (ImageView)findViewById(R.id.imageView1);
btnLarge = (Button)findViewById(R.id.btnEnlarge);
btnReset = (Button)findViewById(R.id.reset);
btnSmall = (Button)findViewById(R.id.btnShrink);
//透過 BitmapFactory.decodeResource 取得 bitmap
bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.yih911030_01_300);
//取得寬高
bmpWidth = bitmap.getWidth();
bmpHeight = bitmap.getHeight();
scale = 1;
loadImage();
//設定放大按鈕監聽器
btnLarge.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scale *= 1.3;
loadImage();
}
});
//設定原來大小監聽器
btnReset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scale = 1;
loadImage();
}
});
//設定縮小按鈕監聽器
btnSmall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scale *= 0.7;
loadImage();
}
});
}
private void loadImage(){
android.graphics.Matrix matrix = new android.graphics.Matrix();
matrix.postScale(scale, scale);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
mImageView.setImageBitmap(resizedBitmap);
}
}
***********************************
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="*** 圖示放大縮小 ***"
android:layout_gravity="center" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<Button
android:id="@+id/btnEnlarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="21sp"
android:text="放大" />
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:text="原尺寸" />
<Button
android:id="@+id/btnShrink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="縮小"
/>
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:layout_gravity="center"
/>
</LinearLayout>
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言