android中位图的大小
我有一个位图:android中位图的大小,android,canvas,bitmap,Android,Canvas,Bitmap,我有一个位图: private Bitmap bitmap; public newStar(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star_bez_nog);
private Bitmap bitmap;
public newStar(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star_bez_nog);
canvas.drawBitmap(bitmap, 100, 100, null);
}
}
如何更改此位图的大小以及在活动中绘制的位图的大小?指定宽度和高度并调用函数
Bitmap bm = ReduceSizeBitmap(imagefile, 150, 150);
现在调用以下函数
Bitmap ReduceSizeBitmap(String file, int width, int height){
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
if (heightRatio > 1 || widthRatio > 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
return bitmap;
}
其他参考资料及
Use the below code::
Bitmap b = returnBitmap(mIcon_val,150,150);////where mIcon_val is bitmap to resize
private Bitmap returnBitmap(Bitmap mIcon_val,int width,int height){
Matrix matrix = new Matrix();
if(width==0)
width = mIcon_val.getWidth();
if(height==0)
height = mIcon_val.getHeight();
matrix.postScale((float)width/mIcon_val.getWidth(), (float)height/mIcon_val.getHeight());
Bitmap resizedBitmap = Bitmap.createBitmap(mIcon_val, 0, 0, mIcon_val.getWidth(), mIcon_val.getHeight(), matrix, true);
return resizedBitmap
}