代码之家  ›  专栏  ›  技术社区  ›  aioobe

Android中的自定义动画

  •  24
  • aioobe  · 技术社区  · 14 年前

    我写了一个习惯 View . 现在我想在用户触摸它时做一点自定义动画。

    当我说自定义时,我的意思是我基本上想自己渲染每个帧,并且 使用“预定义”动画,如所述 here .

    实现这一点的正确方法是什么?

    5 回复  |  直到 14 年前
        1
  •  23
  •   Dmitry Zaytsev    10 年前

    创建自定义动画最灵活(也是最简单)的方法是扩展 Animation 班级。

    一般来说:

    1. 使用设置动画持续时间 setDuration() 方法。
    2. (可选)为动画设置插值器,使用 setInterpolator() (例如,您可以使用 LinearInterpolator AccelerateInterpolator 等)
    3. 覆盖 applyTransformation 方法。我们感兴趣的是 interpolatedTime 变量,在0.0和1.0之间变化并表示动画进度。

    下面是一个例子(我使用这个类来更改 Bitmap . 位图 它自己被吸引进来了 draw 方法):

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

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

    更新

    如果您使用的是Android动画框架(或兼容性实现- NineOldAndroids )您可以为您的自定义声明setter和getter 视图 属性并直接对其进行动画处理。下面是另一个例子:

    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();
        }
    
    }
    
        2
  •  3
  •   Cory Trese    14 年前
    Animation animation = new AnimationDrawable();
    animation.addFrame(getResources().getDrawable(R.drawable.exp1), 50);
    animation.addFrame(getResources().getDrawable(R.drawable.exp2), 50);
    animation.addFrame(getResources().getDrawable(R.drawable.exp3), 50);
    animation.addFrame(getResources().getDrawable(R.drawable.exp4), 50);
    animation.addFrame(getResources().getDrawable(R.drawable.exp5), 50);
    animation.addFrame(getResources().getDrawable(R.drawable.exp6), 50);
    

    这是用于在onCreate()中生成自定义逐帧动画的代码。

    之后,我需要启动动画,但必须在UI线程内这样做。因此我使用runnable。

    class Starter implements Runnable {
        public void run() {
            animation.stop();
            animation.start();
        }
    }
    

    我使用ImageView的.post()方法从onclick()启动该可运行文件:

    ((ImageView) findViewById(R.id.ImageToAnimateOnClicking)).post(new Starter());
    
        3
  •  1
  •   HaMMeReD    14 年前

    我假设您将每个帧创建为位图,然后直接将其传递给动画,而不是从资源中获取可绘制的内容。

    Bitmap bm = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_888);
    Canvas c = new Canvas(bm);
    .... Draw to bitmap
    animation.addFrame(bm,50)
    .... repeat for all frames you wish to add.
    
        4
  •  0
  •   Ajit Singh    8 年前

    有四种类型的动画可以添加到自定义视图中。

    1. alpha-动画元素的透明度
    2. 翻译-动画元素的位置
    3. scale-动画元素大小
    4. 旋转-动画元素旋转

    这是一个 blog post 这就详细解释了其中的每一个。

    完成动画创建后,只需使用下面的代码将自定义动画添加到视图中。

    findById(R.id.element).startAnimation(AnimationUtils.loadAnimation(this, R.anim.custom_animation));
    
        5
  •  -1
  •   Ian G. Clifton    14 年前

    除了在XML中定义多帧动画之外,还可以定义逐帧动画(存储在res/drawable中)。

    <animation-list
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:oneshot="true"
    >
        <item android:drawable="@drawable/frame1" android:duration="300" />
        <item android:drawable="@drawable/frame2" android:duration="300" />
        <item android:drawable="@drawable/frame3" android:duration="300" />
    </animation-list>
    

    通过BackgroundResource将动画设置为视图背景。

    如果你想做更复杂的事情,看看 Canvas 类。请参阅关于如何 draw with Canvas .