2010-10-03 28 views
9

我有一个视图。我想放置一个位图图像作为它的背景图像。我无法做到这一点。将位图作为视图的背景

//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 

任何人都可以帮忙吗?

回答

18

根据API文档,setBackgroundResource需要一个int。

如果您确实需要位图,则可以使用setBackgroundDrawable代替,并将您的位图封装在BitmapDrawable中。

+0

我可以放置一个位图作为背景的视图 – James 2010-10-03 16:23:51

+0

只需直接调用'setBackgroundResource(R.drawable.grape)'。 – JRL 2010-10-03 16:27:45

+0

但实际上我需要在那里放置一个位图。有没有办法做到这一点。 – James 2010-10-03 16:37:49