How to set bitmap image to Button Background Image

自闭症网瘾萝莉.ら 提交于 2019-12-10 15:48:25

问题


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

How can I set bitmap bmp image to button gridcells background Image?


回答1:


You can use following code to do that..

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

then just set bitmap with below function

button.setBackground(bdrawable);



回答2:


you need to check current version of Android also

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:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/18952764/how-to-set-bitmap-image-to-button-background-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!