Android中的自定义动画 [英] Custom animation in Android

查看:27
本文介绍了Android中的自定义动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个自定义View.现在我想在用户触摸它时做一些自定义动画.

I've written a custom View. Now I want to do a little custom animation when the user touches it.

当我说自定义时,我的意思是我基​​本上想自己渲染每一帧,并且使用预定义"动画,如此处.

When I say custom, I mean I basically want to render each frame myself, and not use a "predefined" animation like described here.

实现此目的的正确方法是什么?

What is the proper way of implementing this?

推荐答案

创建自定义动画最灵活(也非常简单)的方法是扩展 Animation 类.

Most flexible (and pretty easy) way to create custom animation is to extend Animation class.

一般来说:

  1. 使用 setDuration() 方法设置动画的持续时间.
  2. 可选择使用 setInterpolator() 为您的动画设置插值器(例如,您可以使用 LinearInterpolatorAccelerateInterpolator 等)
  3. 覆盖 applyTransformation 方法.在这里,我们对 interpolatedTime 变量感兴趣,该变量在 0.0 和 1.0 之间变化并代表您的动画进度.
  1. Set duration of your animation using setDuration() method.
  2. Optionally set the interpolator for your animation using setInterpolator() (for exapmle you can use LinearInterpolator or AccelerateInterpolator etc.)
  3. Override applyTransformation method. Here we interested in interpolatedTime variable which changes between 0.0 and 1.0 and represent the your animation progress.

这是一个例子(我使用这个类来改变我的 Bitmap 的偏移量.Bitmap 本身是在 draw 方法中绘制的):

Here is an example (I'm using this class to change ofsset of my Bitmap. Bitmap itself is drawn in draw method):

public class SlideAnimation extends Animation {

    private static final float SPEED = 0.5f;

    private float mStart;
    private float mEnd;

    public SlideAnimation(float fromX, float toX) {
        mStart = fromX;
        mEnd = toX;

        setInterpolator(new LinearInterpolator());

        float duration = Math.abs(mEnd - mStart) / SPEED;
        setDuration((long) duration);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);

        float offset = (mEnd - mStart) * interpolatedTime + mStart;
        mOffset = (int) offset;
        postInvalidate();
    }

}

您也可以使用Transformation#getMatrix()修改View.

更新

如果您使用的是 Android Animator 框架(或兼容性实现 - NineOldAndroids),您只需为您的自定义 View 属性声明 setter 和 getter 并直接对其进行动画处理.这是另一个例子:

In case if you're using Android Animator framework (or compatibility implementation - NineOldAndroids) you can just declare setter and getter for your custom View property and animate it directly. Here is an another example:

public class MyView extends View {

    private int propertyName = 50;

    /* your code */

    public int getPropertyName() {
        return propertyName;
    }

    public void setPropertyName(int propertyName) {
        this.propertyName = propertyName;
    }

    /*
    There is no need to declare method for your animation, you 
    can, of course, freely do it outside of this class. I'm including code
    here just for simplicity of answer.
    */
    public void animateProperty() {
        ObjectAnimator.ofInt(this, "propertyName", 123).start();
    }

}

这篇关于Android中的自定义动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆