0

我正在做不同图像按钮的属性动画。我的代码完美适用于单个图像按钮,但每当我添加更多按钮或图像时,一切都会开始动画。这是我的代码

public class MainActivity extends Activity {


// ImageButton jackfruit = (ImageButton) findViewById(R.id.btnjackfruit);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button but = (Button) findViewById(R.id.btn);
    but.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ball();

        }


        private void ball() {
             ImageButton ball = (ImageButton) findViewById(R.id.btnball);

             ball.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                  //ImageButton b = (ImageButton) findViewById(R.id.btnball);
                  ImageView banan = (ImageView) findViewById(R.id.banana);
                  banan.setVisibility(View.VISIBLE);

                } // onClick

        });ObjectAnimator horizontalAnimator = ObjectAnimator.ofInt(new ImageButtonAnimatorHelper(ball), "marginLeft", 0, 600);

            horizontalAnimator.setDuration(2000);
            horizontalAnimator.setRepeatCount(ValueAnimator.INFINITE);
            horizontalAnimator.setRepeatMode(ValueAnimator.REVERSE);
            horizontalAnimator.setInterpolator(new LinearInterpolator());

            horizontalAnimator.start();
        } class ImageButtonAnimatorHelper {ImageButton ballButton;
private ImageButtonAnimatorHelper(ImageButton imagebutton) {ballButton = imagebutton;  } 
         public void setMarginLeft(int margin) {
                     ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) ballButton.getLayoutParams();

                    params.leftMargin = margin;

                    ballButton.setLayoutParams(params);
                } // setMarginLeft
        }}  // ImageButtonAnimatorHelper

        ); 
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}

谁能说出为什么我的每个物体都在移动?

4

0 回答 0