Android - 按钮上的 setBackgroundResource

标签 android image button gradient

我有一些按钮,我用 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/

相关文章:

javascript - jquery显示/隐藏按钮标题

css - gtk+ 使用 css 更改按钮

android - 触摸外部时取消 DisplayAlert

java - 使用 Java 裁剪/修剪带有空白空间的 JPG 文件

javascript - 在Javascript中同步加载Base64编码的图像

javascript - 检查 Canvas 段的透明度

java - setOnClickListener 提示我应该使用 setOnItemClickListener

android - Intent 服务启动后防止更改应用程序呈现 Android 的顺序

android - 在 android 的 ScrollView 中回收 View

android - Google 电视应用程序的按钮大小