I want to change the location of a image view every short period. The location is random numbers. Here is my code:
rl = (RelativeLayout) findViewById(R.id.relativeLayout);
final ImageView i = (ImageView) findViewById(R.id.imageView1);
i.setImageResource(R.drawable.rat);
Timer t = new Timer();
final Handler h = new Handler();
final Runnable r = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
i.requestLayout();
rl.removeView(i);
Display display = getWindowManager().getDefaultDisplay();
int sw = display.getWidth();
int sh = display.getHeight();
Random w = new Random();
Random h = new Random();
int width = w.nextInt(sw);
int height = h.nextInt(sh);
tv.setText(String.format("%s",height));
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = width;
params.topMargin = height;
i.setLayoutParams(params);
i.requestLayout();
rl.addView(i,params);
}
};
TimerTask tt = new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(r);
}
};
t.schedule(tt, 0, delay);
This does not work for me, the image does not change. I need someone to help me solve this. Thanks.
.
stackoverflow.comm
No comments:
Post a Comment