0

I want feathers appear on button when the button is clicked.

the algorithm: on click, add feather image to the root layout, choose drawable image

the button is in the middle of screen. the code is this. the problem is that feather appears somewhere above, not on the actual button!

public class MainActivity extends Activity {

private Rect mDisplaySize = new Rect();
private RelativeLayout mRootLayout;
private ArrayList<View> mAllImageViews = new ArrayList<View>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRootLayout = (RelativeLayout) findViewById(R.id.main);

    final ImageButton jumping_cat = (ImageButton)findViewById(R.id.cat2);

    jumping_cat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Drawable d = getResources().getDrawable(R.drawable.feather);
            LayoutInflater inflate = LayoutInflater.from(MainActivity.this);
            ImageView feather = (ImageView) inflate.inflate(R.layout.ani_image_view, null);

            feather.setImageDrawable(d);
            mRootLayout.addView(feather);

            mAllImageViews.add(feather);

            LayoutParams animationLayout = (LayoutParams) feather.getLayoutParams();
            animationLayout.setMargins(0, (int)(-150*mScale), 0, 0);

            animationLayout.width = (int) (60*mScale);
            animationLayout.height = (int) (60*mScale);

            feather.setTop(1320);
            feather.setLeft(1600);

            startAnimation(feather);

        }
    });
}

public void startAnimation(final ImageView aniView) {

    aniView.setPivotX(aniView.getWidth()/2);
    aniView.setPivotY(aniView.getHeight()/2);

    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        int angle = 50 + (int)(Math.random() * 101);
        int movex = 2550 ;
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float value = ( (Float) (animation.getAnimatedValue())).floatValue();

        }
    });

    animator.start();
}

}

These 2 lines: feather.setTop, setLeft do not change the original coordinates.

2
  • 1
    Have you tried setX and setY? May 26, 2015 at 12:18
  • @user2520215, omg, it does work
    – ERJAN
    May 26, 2015 at 12:23

1 Answer 1

1

Have you tries using setX and setY instead. These should work.

1
  • keep in mind that setX and setY are available since api level 11
    – Blackbelt
    May 26, 2015 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.