Android - Button上的setBackgroundResource

时间:2012-03-30 09:45:28

标签: android image button gradient

我有一些按钮,我用setBackgroundResource(R.drawable.cal_box_center);设置背景,我遇到的问题是我的背景是一个渐变,有这种条带效果(烦人)我已经阅读了以便删除这个你需要设置Bitmap.Config.ARGB_8888。我查看了API,这样做的方法是使用decodeStream等,但是如何使用setBackgroundResource并仍将Config设置为ARGB_8888?

提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以使用此代码段:

// create button
Button btn = new Button(getApplicationContext());

//decode the resource(You can also use decodeStream and other decode method of
//BitmapFactory)
Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.cal_box_center);

//create another copy of your bitmap and specify Config
Bitmap newBtm = btm.copy(Bitmap.Config.ARGB_8888, true);

//use your newBtm to create a BitmapDrawable
BitmapDrawable btmDrwble = new BitmapDrawable(newBtm);

// finally set the drawable as your button's background
btn.setBackgroundDrawable(btmDrwble);