单击时如何使图像显示在屏幕的特定部分?

时间:2015-05-26 12:14:47

标签: android onclick imageview

单击按钮时,我希望按钮上出现羽毛。

算法: 单击,将羽毛图像添加到根布局,选择可绘制图像

按钮位于屏幕中间。代码是这样的。问题是羽毛出现在上面的某个地方,而不是实际的按钮!

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

}

这两行:feather.setTop,setLeft不会改变原始坐标。

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用setX和setY。这些应该有效。