我有一些按钮,我用 setBackgroundResource(R.drawable.cal_box_center);
设置了背景,我遇到的问题是我的背景是一个具有这种 strip 效果的渐变(烦人) 并且我已经读到,为了删除它,您需要设置 Bitmap.Config.ARGB_8888。我查看了 API,方法是使用 decodeStream 等,但我如何使用 setBackgroundResource 并将 Config 设置为 ARGB_8888?
提前致谢。
最佳答案
您可以使用此代码段:
// 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);
关于Android - 按钮上的 setBackgroundResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9940596/