【问题标题】:How to set bitmap image to Button Background Image如何将位图图像设置为按钮背景图像
【发布时间】:2013-09-27 23:11:44
【问题描述】:
gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);    
gridcell.setText("Day 1");    
URL url = new URL("http://172.16.4.29:81/pht/2013/9/18/3027_2013_9_18_12_14_56_b.JPG");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

如何将bitmap bmp 图像设置为按钮网格单元背景图像?

【问题讨论】:

标签: android grid cell bmp


【解决方案1】:

您可以使用以下代码来做到这一点..

BitmapDrawable bdrawable = new BitmapDrawable(context.getResources(),bitmap);

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

button.setBackground(bdrawable);

【讨论】:

  • BitmapDrawable(bitmap) 已弃用。
  • ImageButton#setBackgroundDrawable(Drawable) 也已弃用。 Sruit A.Suk 的答案更好。
  • @n8yn8 感谢您的更新。根据要求编辑答案。 :)
【解决方案2】:

您还需要检查当前的 Android 版本

Button bProfile; // your Button
Bitmap bitmap; // your bitmap

if(android.os.Build.VERSION.SDK_INT < 16) {
    bProfile.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
}
else {
    bProfile.setBackground(new BitmapDrawable(getResources(),bitmap));
}

【讨论】:

    【解决方案3】:

    嘿首先了解处理位图:http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

    您根本不应该在 UI 线程上处理位图。 获得位图后,使用ImageView 的方法 setImageBitmap(Bitmap bitmap)。

    要通过网络获取图像,您还可以使用 picasso、Universal Image Loader、Volley 等库来实现您想要的。

    【讨论】:

      【解决方案4】:

      我一直并且将一直强烈推荐您使用图像按钮。

      http://developer.android.com/reference/android/widget/ImageButton.html

      希望对你有所帮助,肖恩。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多