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

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

问题描述

我写了一个自定义的查看。现在我想,当用户触摸它做一点点的自定义动画。

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

当我说的习惯,我的意思是我基​​本上是想渲染每一帧自己,和不可以用predefined动画像描述的<一个href="http://www.has$c$c.com/2010/09/playing-around-with-the-android-animation-framework/">here.

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?

推荐答案

最灵活(和pretty的简单)的方法来创建自定义动画是延长动画

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

在一般的:

  1. 您的动画使用设置期限 setDuration()方法。
  2. 可选设置插补器为您的动画使用 setInterpolator()(对于exapmle您可以使用 LinearInterpolator AccelerateInterpolator 等)
  3. 覆盖 applyTransformation 方法。在这里,我们感兴趣的 interpolatedTime 变量的动画进度0.0和1.0之间,并重新present而变化。
  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.

下面是一个例子(我使用这个类来改变ofsset我的位图位图本身在拉得出法):

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

}

你也可以修改查看通过使用转换#getMatrix()

更新

在情况下,如果您使用的是Android的动画框架(或兼容的实现 - NineOldAndroids )你可以声明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天全站免登陆