如何动画图像在Android中从其位置查看到点击位置?

时间:2017-10-24 07:01:03

标签: java android animation

我的布局中有 3 Textview 1 Imageview 。通过此代码,我可以更改Imageview的位置,将其移动到用户点击的每个Textview之上。是否有任何方法可以将animation从其位置移动到点击了Textview

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

        DisplayMetrics screenSize = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(screenSize);
        screenWidth = screenSize.widthPixels;

        final ImageView greenBar = findViewById(R.id.green_bar);
        greenBar.getLayoutParams().width = screenWidth / 3;
        greenBar.setX(screenWidth * 2 /3);

        TextView leftText = findViewById(R.id.text_left);
        TextView rightText = findViewById(R.id.text_right);
        TextView middleText = findViewById(R.id.text_middle);

        leftText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                greenBar.setX(0);
            }
        });

        rightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                greenBar.setX(screenWidth * 2 /3);
            }
        });

        middleText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                greenBar.setX(screenWidth /3);
            }
        });
    }

更新:我知道我应该使用这些构造函数,但我不知道如何设置这些值!

TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

0 个答案:

没有答案