2012-03-30 62 views
1

我有一些按钮,我用setBackgroundResource(R.drawable.cal_box_center);设置背景,我遇到的问题是我的背景是具有这种条纹效果(令人讨厌)的渐变,我读过它以便删除这个你需要设置Bitmap.Config.ARGB_8888。我看了一下API,做到这一点的方法是使用decodeStream等,但我怎样才能使用setBackgroundResource,并仍然将配置设置为ARGB_8888?Android - 按钮上的setBackgroundResource

在此先感谢。

回答

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

如果这篇文章可以帮助你,请注明这是一个答案。

谢谢。