import android.app.activity;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.matrix;
import android.graphics.drawable.bitmapdrawable;
import android.os.bundle;
import android.view.viewgroup.layoutparams;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.imageview.scaletype;
public class bitmaptest extends activity {
public void oncreate(bundle icicle) {
super.oncreate(icicle);
settitle("eoeandroid教程: 縮放和旋轉圖檔 -by:iceskysl");
linearlayout linlayout = new linearlayout(this);
// 加載需要操作的圖檔,這裡是eoeandroid的logo圖檔
bitmap bitmaporg = bitmapfactory.decoderesource(getresources(),
r.drawable.eoe_android);
//擷取這個圖檔的寬和高
int width = bitmaporg.getwidth();
int height = bitmaporg.getheight();
//定義預轉換成的圖檔的寬度和高度
int newwidth = 200;
int newheight = 200;
//計算縮放率,新尺寸除原始尺寸
float scalewidth = ((float) newwidth) / width;
float scaleheight = ((float) newheight) / height;
// 建立操作圖檔用的matrix對象
matrix matrix = new matrix();
// 縮放圖檔動作
matrix.postscale(scalewidth, scaleheight);
//旋轉圖檔 動作
matrix.postrotate(45);
// 建立新的圖檔
bitmap resizedbitmap = bitmap.createbitmap(bitmaporg, 0, 0,
width, height, matrix, true);
//将上面建立的bitmap轉換成drawable對象,使得其可以使用在imageview, imagebutton中
bitmapdrawable bmd = new bitmapdrawable(resizedbitmap);
//建立一個imageview
imageview imageview = new imageview(this);
// 設定imageview的圖檔為上面轉換的圖檔
imageview.setimagedrawable(bmd);
//将圖檔居中顯示
imageview.setscaletype(scaletype.center);
//将imageview添加到布局模闆中
linlayout.addview(imageview,
new linearlayout.layoutparams(
layoutparams.fill_parent, layoutparams.fill_parent
)
);
// 設定為本activity的模闆
setcontentview(linlayout);
}
}