简体   繁体   中英

How to set bitmap image to Button Background Image

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?

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM