Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

place a bitmap as background of a view

Tags:

android

I have a view. I want to place a bitmap image as its background image. I was not able to do that.

//My code is below

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.grape);
part1 = new View(this); 
Bitmap map2 = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
canvas.drawBitmap( bitmapOrg,new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), new Rect(0, 0, targetWidth, targetHeight), null);

part1.setBackgroundResource(map2); //i couldn't place map2 as bgresource to my view part1

can anyone help?

like image 547
James Avatar asked Oct 03 '10 15:10

James


People also ask

What process will you employ to apply bitmap to the background?

set bitmap to background of ImageView with imageView. setImageBitmap method.

How do I create a bitmap in canvas?

To create a Bitmap from a resource, use the BitmapFactory method decodeResource(): Bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.


1 Answers

According to the API docs, setBackgroundResource expects an int.

If you definitely need a bitmap, you can use setBackgroundDrawable instead and wrap your bitmap in a BitmapDrawable.

like image 68
JRL Avatar answered Sep 28 '22 08:09

JRL