由code在Android的ImageButton的状态选择模仿 [英] Imitating Selector by code for ImageButton states in Android

查看:118
本文介绍了由code在Android的ImageButton的状态选择模仿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ImageButton ,其中源图像动态地从网络加载。我的目标是根据该按钮的状态下的图像上设定不同的彩色滤光片(即STATE_ pressed,state_focused ...)。问题是,我不能使用选择,因为我的形象是动态加载的,而不是一个可绘制资源。
没有任何一个可以给我一个线索我怎么可以用code认识国家和模仿选择的工作?

I have an ImageButton, where the source image is loaded dynamically from the network. My goal is to set different color filters on the image according to the button's state (i.e state_pressed, state_focused...). The problem is that I cant use Selector because my image is loaded dynamically and is not a drawable resource. Does any one can give me a clue how can I recognize the state using code and imitate the selector's work?

推荐答案

您可以通过编程设置状态由kcoppock在这个SO概述帖子

You can set the states programmatically as outlined by kcoppock in this SO post

Resources r = getResources();
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed}, r.getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused}, r.getDrawable(R.drawable.focused));
states.addState(new int[] {}, r.getDrawable(R.drawable.normal));

imageButton.setImageDrawable(states);

这篇关于由code在Android的ImageButton的状态选择模仿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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