5
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());

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

4

4 回答 4

16

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

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

然后只需使用以下功能设置位图

button.setBackground(bdrawable);
于 2013-09-23T07:37:03.650 回答
3

您还需要检查当前的 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));
}
于 2015-05-29T06:38:42.107 回答
0

I always and will always recommended you strongly using Image Button.

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

I hope that helps you, Shaun.

于 2013-09-23T06:16:37.067 回答
0

Hey First learn about Handling bitmaps: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

You should not process bitmaps on the UI thread at all. Once you have attained Bitmap use ImageView's method setImageBitmap(Bitmap bitmap).

To fetch images through network, You can also use libraries like picasso, Universal Image Loader, Volley to attain what you are wanting.

于 2013-09-23T06:16:45.910 回答