ImageView img1;
Button bt1, bt2;
int j=0;
Drawable d[] = { this.getResources().getDrawable(R.drawable.horse1),
this.getResources().getDrawable(R.drawable.horse2),
this.getResources().getDrawable(R.drawable.horse3) };
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
j=msg.arg1;
for(int i=0;i<3;i++){
if(i!=j){
img1.setBackgroundDrawable(d[j]);
}
handler.post(run);
}
}
};
Runnable run=new Runnable() {
@Override
public void run() {
Message msg=new Message();
msg.arg1=j;
handler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(j>2)
{j=0;}
else{
j++;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1=(ImageView) this.findViewById(R.id.imageView1);
bt1=(Button) this.findViewById(R.id.button1);
bt2=(Button) this.findViewById(R.id.button2);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handler.post(run);
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handler.removeCallbacks(run);
}
});
}