我的主要代码如下程序在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();
}
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();
}
以前做过的一个例子,2.1上运行正常,LZ对比看看有什么不一样。LZ这句不知道可以不可以,用bmpOrg创建位图又赋值给自己。
bmpOrg = Bitmap.createBitmap(bmpOrg, 0, 0, bmpW, bmpH,matrix, true);