是否有可能有一个动画绘制? [英] Is it possible to have an animated drawable?

查看:180
本文介绍了是否有可能有一个动画绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能创造一个可绘制具有某种动画,无论是逐帧动画,旋转,等等,这被定义为一个的xml抽拉,并且可以重新由单个可绘制对象psented $ P $而不必处理与code中的动画?

Is it possible to create a drawable that has some sort of animation, whether it is a frame by frame animation, rotation, etc, that is defined as a xml drawable and can be represented by a single Drawable object without having to deal with the animation in code?

如何我想使用它: 我有一个列表,在此列表中的每个项目可能在某个时候有事情发生吧。虽然它发生,我想有一个旋转的过程动画类似于一个不确定的进度。因为还可能有几个这样的在屏幕上我想,如果他们都有着相同的绘制对象,他们只需要它的一个实例在内存中,其动画将被同步,所以你就不必在不同的点了一堆纺纱对象在纺纱动画。

How I am thinking to use it: I have a list and each item in this list may at sometime have something happening to it. While it is happening, I would like to have a spinning progress animation similar to a indeterminate ProgressBar. Since there may also be several of these on screen I thought that if they all shared the same Drawable they would only need one instance of it in memory and their animations would be synced so you wouldn't have a bunch of spinning objects in various points in the spinning animation.

我不看重这种方法。我只是试图想显示多个纺纱进度的动画,最好的最有效的方式让他们一起同步,使他们在外观上是一致的。

I'm not attached to this approach. I'm just trying to think of the most efficient way to display several spinning progress animations and ideally have them synced together so they are consistent in appearance.

感谢

为了响应Sybiam的回答:

我试图实现一个RotateDrawable但它不转动。

I have tried implementing a RotateDrawable but it is not rotating.

下面是我的xml的绘制迄今:

Here is my xml for the drawable so far:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
 android:drawable="@drawable/my_drawable_to_rotate"
 android:fromDegrees="0" 
 android:toDegrees="360"
 android:pivotX="50%"
 android:pivotY="50%"
 android:duration="800"
 android:visible="true" />

我已使用可拉伸作为ImageView的的src和背景尝试和两​​种方式仅产生一个非旋转的图像。

I have tried using that drawable as the src and background of a ImageView and both ways only produced a non-rotating image.

有一些已经开始图像旋转?

Is there something that has to start the image rotation?

推荐答案

是的!的(无证)键,我发现通过阅读进度 code是你必须调用 Drawable.setLevel()的OnDraw()为了让&LT;转&GT; 的事情有任何影响。该进度工作这样的事情(额外不重要code省略):

Yes! The (undocumented) key, which I discovered by reading the ProgressBar code is that you have to call Drawable.setLevel() in onDraw() in order for the <rotate> thing to have any effect. The ProgressBar works something like this (extra unimportant code omitted):

提拉XML:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
             android:drawable="@drawable/spinner_48_outer_holo"
             android:pivotX="50%"
             android:pivotY="50%"
             android:fromDegrees="0"
             android:toDegrees="1080" />
    </item>
    <item>
        <rotate
             android:drawable="@drawable/spinner_48_inner_holo"
             android:pivotX="50%"
             android:pivotY="50%"
             android:fromDegrees="720"
             android:toDegrees="0" />
    </item>
</layer-list>

的OnDraw()

    Drawable d = getDrawable();
    if (d != null)
    {
        // Translate canvas so a indeterminate circular progress bar with
        // padding rotates properly in its animation
        canvas.save();
        canvas.translate(getPaddingLeft(), getPaddingTop());

        long time = getDrawingTime();

        // I'm not sure about the +1.
        float prog = (float)(time % ANIM_PERIOD+1) / (float)ANIM_PERIOD; 
        int level = (int)(MAX_LEVEL * prog);
        d.setLevel(level);
        d.draw(canvas);

        canvas.restore();

        ViewCompat.postInvalidateOnAnimation(this);
    }

MAX_LEVEL 是一个常数,并且始终10000(根据文档)。 ANIM_PERIOD 为您的动画以毫秒为单位的时间。

MAX_LEVEL is a constant, and is always 10000 (according to the docs). ANIM_PERIOD is the period of your animation in milliseconds.

不幸的是,因为你需要修改的OnDraw()你不能只是把这个绘制在的ImageView 的ImageView 永远不会改变的绘制水平。然而,你可能能够从修改绘制水平外的的的ImageView 的。 进度(AB)使用 AlphaAnimation 设定水平。所以,你会做这样的事情:

Unfortunately since you need to modify onDraw() you can't just put this drawable in an ImageView since ImageView never changes the drawable level. However you may be able to change the drawable level from outside the ImageView's. ProgressBar (ab)uses an AlphaAnimation to set the level. So you'd do something like this:

mMyImageView.setImageDrawable(myDrawable);

ObjectAnimator anim = ObjectAnimator.ofInt(myDrawable, "level", 0, MAX_LEVEL);
anim.setRepeatCount(ObjectAnimator.INFINITE);
anim.start();

这可能工作,但我还没有测试它。

It might work but I haven't tested it.

其实是有一个 ImageView.setImageLevel()方法,所以它可能是简单的:

There is actually an ImageView.setImageLevel() method so it might be as simple as:

ObjectAnimator anim = ObjectAnimator.ofInt(myImageVew, "ImageLevel", 0, MAX_LEVEL);
anim.setRepeatCount(ObjectAnimator.INFINITE);
anim.start();

这篇关于是否有可能有一个动画绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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