不同属性动画的多个ImageButtons

时间:2013-07-05 07:06:11

标签: java android

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

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;
}}

有人能告诉我为什么每个物体都在移动吗?

0 个答案:

没有答案