android版本不同导致图像缩放出错
yuli 2011-01-13 08:34:33 我的主要代码如下
程序在2.2下运行正常
但在1.6 和2.1下运行缩放时出错,不知道为什么
class DrawView extends View {
Bitmap bmpOrg= null;
public DrawView(Context context) {
super(context);
// TODO Auto-generated constructor stub
//加载图像
bmpOrg=((BitmapDrawable) getResources().getDrawable(R.drawable.imagetest)).getBitmap();
}
public void zoomImage(int newWidth,int newHeight) {
bmpOrg=((BitmapDrawable) getResources().getDrawable(R.drawable.bjmap)).getBitmap();
int bmpW,bmpH;
bmpW=bmpOrg.getWidth();
bmpH=bmpOrg.getHeight();
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = (float)(newWidth)/(float)(bmpW);
float scaleHeight = (float)(newHeight)/(float)(bmpH);
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
bmpOrg = Bitmap.createBitmap(bmpOrg, 0, 0, bmpW, bmpH,matrix, true);
invalidate();
}