放置一个位图作为视图的背景 [英] place a bitmap as background of a view

查看:148
本文介绍了放置一个位图作为视图的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点。我希望把位图图像作为背景图像。我没能做到这一点。

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

谁能帮助?

推荐答案

根据API文档,<一个href="http://developer.android.com/reference/android/view/View.html#setBackgroundResource%28int%29"><$c$c>setBackgroundResource预计一个int。

According to the API docs, setBackgroundResource expects an int.

如果你肯定需要一个位图,你可以使用<一个href="http://developer.android.com/reference/android/view/View.html#setBackgroundDrawable%28android.graphics.drawable.Drawable%29"><$c$c>setBackgroundDrawable反而,敷在<一个位图href="http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html"><$c$c>BitmapDrawable.

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

这篇关于放置一个位图作为视图的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆