【问题标题】:Android: animate a view with setY()Android:使用 setY() 为视图设置动画
【发布时间】:2015-11-23 20:00:34
【问题描述】:

我想为一个简单的视图制作动画,例如一个简单的文本视图。我想使用 translate 动画视图。

现在我的要求是,我想做一个方法,例如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 库。 非常感谢您对此提供任何帮助。

【问题讨论】:

  • 你的代码有什么问题?如果您提出问题可能会有所帮助
  • 所以?你的代码没有运行吗?或者它正在运行但没有动画?或其他任何东西
  • @fractalwrench @SrujanBarai,感谢您的回复。实际上这段代码有很多问题。但是经过一些研究,我了解了ObjectAnimator,所以我使用它并更新了slide() 方法,之后它就起作用了。我将其发布为答案。

标签: android android-animation translate-animation


【解决方案1】:

slide():

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

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    相关资源
    最近更新 更多