0
我有一个ImageButton
,其中源图像从网络动态加载。我的目标是根据按钮的状态(即state_pressed,state_focused ...)在图像上设置不同的颜色过滤器。问题是我不能使用Selector
,因为我的图像是动态加载的,不是可绘制的资源。 有没有人可以给我一个线索,我如何识别使用代码的状态并模仿选择器的工作?模仿Android的ImageButton状态代码选择器
我有一个ImageButton
,其中源图像从网络动态加载。我的目标是根据按钮的状态(即state_pressed,state_focused ...)在图像上设置不同的颜色过滤器。问题是我不能使用Selector
,因为我的图像是动态加载的,不是可绘制的资源。 有没有人可以给我一个线索,我如何识别使用代码的状态并模仿选择器的工作?模仿Android的ImageButton状态代码选择器
可以编程设置状态由kcoppock在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);
而当你有动态图像概括,而不是如果'r.getDrawable(...)'使用'Drawable.createFromPath(pathname)按' 。 – Ridcully
@antwe:这似乎是一个好开始,但我该如何设置彩色滤光片?在同一个drawable上调用'setColorFilter'似乎没有效果。 –
@Ridcully:我忘了谢谢你的评论,它完成了答案。所以谢谢 :) –