【问题标题】:Android - setBackgroundResource on ButtonAndroid - 按钮上的 setBackgroundResource
【发布时间】:2012-03-30 09:45:28
【问题描述】:
我有一些按钮,我用setBackgroundResource(R.drawable.cal_box_center); 设置背景,我遇到的问题是我的背景是具有这种条带效果(烦人)的渐变,我已经阅读了它以便删除这个你'将需要设置 Bitmap.Config.ARGB_8888。我查看了 API,这样做的方法是使用 decodeStream 等,但我如何使用 setBackgroundResource 并仍然将 Config 设置为 ARGB_8888?
提前致谢。
【问题讨论】:
标签:
android
image
button
gradient
【解决方案1】:
你可以使用这个代码sn-p:
// 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);