Android的 - 在钮扣setBackgroundResource [英] Android - setBackgroundResource on Button

查看:219
本文介绍了Android的 - 在钮扣setBackgroundResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些按钮,我设置背景与 setBackgroundResource(R.drawable.cal_box_center); ,我遇到的问题是,我的背景是一个渐变哪些有这个频带效应(讨厌),我读过,为了消除这一点,你就需要设置Bitmap.Config.ARGB_8888。我看着API,并做到这一点的方法是使用德codeStream等,但我怎么可以使用setBackgroundResource,仍然设置为配置ARGB_8888?

I have some buttons which i set the background with setBackgroundResource(R.drawable.cal_box_center); and the problem I'm having is that my background is a gradient which has this banding effect (annoying) and I've read that in order to remove this you'll need to set the Bitmap.Config.ARGB_8888. I looked into the API and the way to do this is to use decodeStream etc, but how can i use setBackgroundResource and still set the Config to ARGB_8888?

先谢谢了。

推荐答案

您可以使用此code片断:

You can use this code snippet:

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

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

If this post helps you, please mark this as an answer.

感谢。

这篇关于Android的 - 在钮扣setBackgroundResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆