使用imageView.setImageBitmap方法将位图设置为ImageView的背景

35 android android-imageview

我有一个ImageView.我imageView.setImageBitmap用来将我的图像设置为背景ImageView.但它将我的图像设置为ImageView源(即android:src),但它不会将我的图像设置为ImageView背景(即android:background).

当我使用imageView.setImageBitmap,它看起来像,好像我以前imageView.setImageResource没有imageView.setBackgroundResource.

如果我想使用将图像设置为背景,我该如何处理imageView.setImageBitmap.我知道我可以通过制作习惯来做到这一点ImageView.但是没有习惯可能ImageView吗?如果有可能,请告诉我怎么做.

Sid*_*mit 102

我已经测试过了,已经完成了

我想你得到了Bitmap

所以你必须将Bitmap转换为BitmapDrawable

喜欢

  BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap)
Run Code Online (Sandbox Code Playgroud)

然后用下面的函数设置位图

  imageView.setBackground(ob);
Run Code Online (Sandbox Code Playgroud)

通过这种方式你可以做到..

  • @Sandeep,那不是真的.它只是那里的构造函数已被弃用.但是有一个构造函数可用作`BitmapDrawable ob = new BitmapDrawable(getResources(),bitmap)` (12认同)
  • 我得到"呼叫要求API级别16(当前最小值为14)" (2认同)

Chi*_*hod 11

尝试使用以下代码将您自己的位图设置为背景.

Bitmap bitmap = ...;
ImageView imageView = ...;
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setBackground(drawable);
Run Code Online (Sandbox Code Playgroud)


Ani*_*mar 5

试试这个..

使用位图创建drawable并使用setBackgroundDrawable(Drawable d)方法进行imageview.

Drawable d = new BitmapDrawable(getResources(),bitmap);

imageview.setBackgroundDrawable(d);
Run Code Online (Sandbox Code Playgroud)