[英]Bitmap image displaying VERY top left corner, not scaling
我试图在Canvas
上显示图像,但是当我调用drawBitmap
方法时,仅获得了非常左上角的图像,该图像无法缩放到屏幕的高度和宽度。 这是涉及自定义视图的代码:
private class ImageOverlay extends View {
public ImageOverlay(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Bitmap image = BitmapFactory.decodeFile(getPath(baseImage));
Paint paint = new Paint();
paint.setAlpha(200);
canvas.drawBitmap(image, 0, 0, paint);
Log.v(TAG, "Drew picture");
super.onDraw(canvas);
}
private String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor!=null)
{
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else return null;
}
baseImage
是传递给我的活动的Uri
对象,它不为 null! 这是实际的图像:
这是Canvas
图片:
但是,当我调用drawBitmap方法时,仅获得了非常左上角的图像,图像无法缩放到屏幕的高度和宽度。
您要告诉您的程序这样做: canvas.drawBitmap(image, 0, 0, paint);
因为(0,0)是左上角。
如果需要较小的图像,则应缩小图像,可能是Bitmap small = Bitmap.createScaledBitmap(image, width, height, false);
使用Canvas.drawBitmap()
版本执行所需的缩放,例如this 。
顺便说一句,您解压缩的位图显然比您需要的要大得多,它将占用大量RAM。 我建议使用BitmapFactory.Options.inSampleSize
减少它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.