Reputation: 55247
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:
Upvotes: 1
Views: 1244
Reputation: 6081
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.
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);
Upvotes: 2
Reputation: 38727
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.
Upvotes: 1