Android Not Redrawing after View.layout(l,t,r,b)

2019-05-22 15:22发布

I am attempting to place several images at specific points on my layout using ImageButton.layout(l,t,r,b). After they are arranged, I would like to execute an animation on each ImageButton. The trouble is, the animation starts before the positions of the ImageButtons has changed on the screen, causing the animation to look wrong.

How can I fix this? Here is a bit of code...

private void arrangeButtons() 
{
  int temp = 0;
  for(ImageButton imageButtonButton :imageButtonList) {
    imageButtonButton.setCurrentPositionIndex(temp);
    int l = PositionsLeft[temp];
    int t = PositionsTop[temp];
    int r = PositionsRight[temp];
    int b = positionsBottom[temp];
    currentButton.layout(l,t,r,b);
    temp++;
  }
  ViewGroup vg = (ViewGroup)findViewById(R.id.imageListFrame);
  vg.invalidate();
  vg.refreshDrawableState();
}
public static void rotateButtons() {
  for(ImageButton imageButton : imageButtonList) {
    RotateAnimation rotation = new RotateAnimation(0, 45, 25, 25);
    imageButton.setAnimation(rotation);
    imageButton.startAnimation(rotations);
  }
}
public void onCreate(Bundle savedInstanceState) {
  setContentView(R.layout.main);
  arrangeButtons();
  rotateButtons();
}

Any thoughts? Thanks!

1条回答
爷、活的狠高调
2楼-- · 2019-05-22 15:39

Try calling requestLayout() after currentButton.layout(l,t,r,b); or better yet use setLayoutParams to change the position of the view on the screen.

查看更多
登录 后发表回答