位图图像显示非常左上角,未缩放
我正在尝试在 Canvas
< 上显示图像/a>,但是当我调用 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
是传递给我的 Activity 的 Uri
对象,并且它NOT null!这是实际图像:
这是 Canvas
图像:
I am trying to display an image on a Canvas
, but when I call the drawBitmap
method, I get only the VERY top left corner, the image doesn't scale to the the screen height and width. Here is my code that involves the custom view:
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
is a Uri
object that is passed to my activity and it is NOT null! Here is the actual image:
And here is the Canvas
image:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您告诉您的程序这样做:
canvas.drawBitmap(image, 0, 0, Paint);
因为(0,0)是左上角。
如果你需要较小的图像,你应该缩小它,一种可能性是
小位图 = Bitmap.createScaledBitmap(image, width, height, false);
You're telling your programm to do so:
canvas.drawBitmap(image, 0, 0, paint);
As (0,0) is the top left corner.
If you need a smaller image, you should downscale it, an possibility would be
Bitmap small = Bitmap.createScaledBitmap(image, width, height, false);
使用
Canvas.drawBitmap()
版本来执行所需的缩放,例如 这个。顺便说一句,解压缩后的位图显然比您需要的大得多,并且它将占用大量的 RAM。我建议使用
BitmapFactory.Options.inSampleSize
来减少它。Use a version of
Canvas.drawBitmap()
which performs the scaling you want, such as this one.Btw, your decompressed Bitmap is obviously much larger than you need it to be and it will be taking up a prodigious amount of RAM. I recommend using
BitmapFactory.Options.inSampleSize
to reduce it.