这是我的第一个问题,我希望我做得对。我需要在特定的时间间隔内改变一些按钮的颜色。
我决定用Colorfilter来做这件事,因为setBackground方法使Button看起来很难看。
问题是:如果我在可运行的ColorFilter中设置了,那么它就是不工作的。
但是:在ColorFilter中设置onCreate或click方法是working。并且:在Runnable中使用BackgroundColor设置setBackgroundColor是working。
我忘了提到,如果我在带有Android4.1的仿真器上运行,而不是在2.3.3中运行,这一切都很好。
有什么想法吗?这是代码
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
int counter = 0;
Button b = null;
final Handler handler = new Handler();
final Runnable doit = new Runnable() {
public void run() {
if ( counter % 2 == 0) {
b = (Button) findViewById(R.id.button1);
b.setBackgroundColor(0xffff0000); // working
//b.getBackground().setColorFilter(0xFFD2691E, PorterDuff.Mode.MULTIPLY); // doesnt work
}
else {
//b.getBackground().clearColorFilter();
b.setBackgroundColor(0xff00ff00);
}
counter++;
}
};
public void click(View view) {
// configure timer 1
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
handler.post(doit);
}
};
timer.schedule(task, 1111, 1111);
}
}发布于 2014-09-18 07:35:45
我对图像视图也有同样的问题。我找到了一个解决方案,您必须调用-methode:
ImageView img =(ImageView) findViewById(R.id.myimageview);
img.getDrawable().clearColorFilter();
img.invalidate();https://stackoverflow.com/questions/12478704
复制相似问题