繁体   English   中英

Android:使用setY()为视图设置动画

[英]Android: animate a view with setY()

我想制作一个简单的视图动画,例如一个简单的textview。 我想使用翻译动画视图。

现在我的要求是,我想制作一个方法,例如slide(View v, float position) 它将采用视图进行动画处理和定位,直到应将其设置为动画为止。 我将在代码中的所需位置调用该方法。

为此,我尝试了一些方法。 我做了MyTranslateAnimation类,如下所示。

public class MyTranslateAnimation extends Animation {

private View mView;
private final float position;

public MyTranslateAnimation(View view, float position){

    mView = view;
    this.position = position;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    mView.setY(position);
    mView.requestLayout();
}

}

然后,我在MainActivity.java创建了一个textview并设置了onTouchListener ,然后创建了此方法slide()来完成上述任务。

下面是代码: onCreate():

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

    _root = (ViewGroup)findViewById(R.id.root);

    _view = new TextView(this);
    _view.setText("TextView!!!!!!!!");

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 50);
    layoutParams.leftMargin = 50;
    layoutParams.topMargin = 50;
    layoutParams.bottomMargin = -250;
    layoutParams.rightMargin = -250;
    _view.setLayoutParams(layoutParams);

    _view.setOnTouchListener(this);
    _root.addView(_view);
}

slide():

private void slide(View view, float position){
    Animation animation = new MyTranslateAnimation(view, position);
    animation.setInterpolator(new DecelerateInterpolator());
    animation.setDuration(200);
    animation.start();
}

如下所示,我使用了slide()方法:

public boolean onTouch(View view, MotionEvent event) {
    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
            _xDelta = X - lParams.leftMargin;
            _yDelta = Y - lParams.topMargin;
            break;
        case MotionEvent.ACTION_UP:
            slide(view, 100);
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            break;
        case MotionEvent.ACTION_POINTER_UP:
            break;
        case MotionEvent.ACTION_MOVE:
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
            layoutParams.leftMargin = X - _xDelta;
            layoutParams.topMargin = Y - _yDelta;
            layoutParams.rightMargin = -250;
            layoutParams.bottomMargin = -250;
            view.setLayoutParams(layoutParams);
            break;
    }
    _root.invalidate();
    return true;
}

我也不想为此使用nineoldandroid库。 对此,我们将给予任何帮助。

slide():

public void slide(float position){
    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(overflow.get(), "translationY", position);
    objectAnimator.setInterpolator(new DecelerateInterpolator());
    objectAnimator.setDuration(200);
    objectAnimator.start();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM