如何在Android中制作连续运行的TransitionDrawable动画?

发布于 2024-11-05 15:51:32 字数 677 浏览 1 评论 0原文

我正在尝试制作一个动画徽标。它由两个静态图像组成。

我想实现交叉淡入淡出效果。

我已经使用 TransitionDrawable 完成了它,设置了 crossFadeEnabled,一切看起来都不错。

问题是我需要绕圈跑。如何才能实现呢?

<transition xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/image_expand">
  <item android:drawable="@drawable/image_collapse">
</transition>

Resources res = mContext.getResources();
TransitionDrawable transition = (TransitionDrawable) res.getDrawable(R.drawable.expand_collapse);
ImageView image = (ImageView) findViewById(R.id.toggle_image);
image.setImageDrawable(transition);

这是来自谷歌的代码,运行完美。 最重要的是需要在Android 1.6下工作。

I'm trying to make an animated logo. It consists of two static images.

I would to like to achieve a cross-fading effect.

I've done it with the use of TransitionDrawable, set the crossFadeEnabled and everything looks nice.

The thing is that I need to be running in circle. How can it be achieved ?

<transition xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/image_expand">
  <item android:drawable="@drawable/image_collapse">
</transition>

Resources res = mContext.getResources();
TransitionDrawable transition = (TransitionDrawable) res.getDrawable(R.drawable.expand_collapse);
ImageView image = (ImageView) findViewById(R.id.toggle_image);
image.setImageDrawable(transition);

This the code from google which runs perfectly.
The most importantn thing is that in needs to work under Android 1.6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2024-11-12 15:51:32

我设法通过处理程序方法使转换可绘制对象正常工作,该方法在从可绘制对象 1 转换为可绘制对象 2 后反转方向。在我的 XML 中:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android"  >

    <item android:drawable="@drawable/background_animation1"/>
    <item android:drawable="@drawable/background_animation2"/>

</transition>

可绘制对象是渐变的:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
     <corners android:topLeftRadius = "10dp" android:topRightRadius="10dp" android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp"/>
    
    <gradient
        android:startColor="#d200ff" 
        android:centerColor="#4e00ff"
        android:endColor="#006cff"/>

</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <size android:width="900dp" android:height="500dp"/>
    <corners android:topLeftRadius = "10dp" android:topRightRadius="10dp" android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp"/>
    <gradient
        android:startColor="#006cff" 
        android:centerColor="#ff6600"
        android:endColor="#d200ff"/>

</shape>

在我的初始化代码中:

trans = (TransitionDrawable) getResources().getDrawable(R.drawable.transition);
		trans.setCrossFadeEnabled(true);
		backgroundimage.setImageDrawable(trans);

....

handlechange();

处理程序代码中最重要的部分;请注意全局运行的标志。我在 Gingerbread 和 Kitkat 上运行了这个;

void handlechange1()
	{
		Handler hand = new Handler();
		hand.postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				change();
			}
			private void change()
			{
				if (flag)
				{
					trans.startTransition(8000);
					flag = false;
				} else
				{
					trans.reverseTransition(8000);
					flag = true;
				}
				handlechange1();
			}
		}, 8000);

	}

I managed to get the transition drawable to work via a handler method that reverses direction after the transition from drawable1 to drawable2. In my XML's :

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android"  >

    <item android:drawable="@drawable/background_animation1"/>
    <item android:drawable="@drawable/background_animation2"/>

</transition>

The drawables are gradients :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
     <corners android:topLeftRadius = "10dp" android:topRightRadius="10dp" android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp"/>
    
    <gradient
        android:startColor="#d200ff" 
        android:centerColor="#4e00ff"
        android:endColor="#006cff"/>

</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <size android:width="900dp" android:height="500dp"/>
    <corners android:topLeftRadius = "10dp" android:topRightRadius="10dp" android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp"/>
    <gradient
        android:startColor="#006cff" 
        android:centerColor="#ff6600"
        android:endColor="#d200ff"/>

</shape>

In my initialisation code :

trans = (TransitionDrawable) getResources().getDrawable(R.drawable.transition);
		trans.setCrossFadeEnabled(true);
		backgroundimage.setImageDrawable(trans);

....

handlechange();

And the most important bit in the handler code; note the flag that is running globally. I got this running on Gingerbread and Kitkat;

void handlechange1()
	{
		Handler hand = new Handler();
		hand.postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				change();
			}
			private void change()
			{
				if (flag)
				{
					trans.startTransition(8000);
					flag = false;
				} else
				{
					trans.reverseTransition(8000);
					flag = true;
				}
				handlechange1();
			}
		}, 8000);

	}

桃酥萝莉 2024-11-12 15:51:32

并没有真正的内置方法可以做到这一点。 TransitionDrawable 没有设计无限循环动画功能。最简单的建议是在仅包含一个 DrawableView 上使用 Animation(alpha、scale、translate 等)如果可以的话。

一个相当简单的技巧是向保存 TransitionDrawable 的自定义 View 添加一个 Handler 和回调。创建View 时,可以将Handler 设置为您的转换间隔。 View 还将实现 Handler.Callback 并在其 handleMessage(Message) 方法内调用 reverseTransition(int) TransitionDrawable 上的 code>。

一个粗略的示例如下:

public class myView extends View implements Handler.Callback {

    private Handler mHandler = new Handler(this);

    private int mDelay = 1000; // Delay in milliseconds.

    private Runnable mEvent = new Runnable() {
        public void run() {
            mHandler.removeCallbacks(mEvent);
            mHandler.postDelayed(mEvent, mDelay);
            Message message = mHandler.obtainMessage();
            mHandler.sendMessage(message);
        }
    };

    public View(Context context) {
        super(context);
        // Set your background TransitionDrawable.
        setBackgroundDrawable(...);
    }

    public handleMessage(Message message) {
        TransitionDrawable drawable = (TransitionDrawable) getBackgroundDrawable();
        drawable.reverseTransition(mDelay);
    }

    public void start() {
        mHandler.post(mEvent);
    }

    public void stop() {
        mHandler.removeCallbacks(mEvent);
    }

}

调用 start() 开始连续转换,调用 stop() 停止它。这有点像创建自己的动画,但它可以在紧要关头发挥作用。

There is not really a built-in way to do this. TransitionDrawable is not designed with an infinite looping animation function. The easiest recommendation would be to use an Animation (alpha, scale, translate, etc.) on a View containing just one of your Drawables if you can.

A fairly easy hack would be to add a Handler and callback to your custom View holding your TransitionDrawable. When the View is created, the Handler can be set to your transition interval. The View would also implement Handler.Callback and inside its handleMessage(Message) method, it would call reverseTransition(int) on your TransitionDrawable.

A rough example is below:

public class myView extends View implements Handler.Callback {

    private Handler mHandler = new Handler(this);

    private int mDelay = 1000; // Delay in milliseconds.

    private Runnable mEvent = new Runnable() {
        public void run() {
            mHandler.removeCallbacks(mEvent);
            mHandler.postDelayed(mEvent, mDelay);
            Message message = mHandler.obtainMessage();
            mHandler.sendMessage(message);
        }
    };

    public View(Context context) {
        super(context);
        // Set your background TransitionDrawable.
        setBackgroundDrawable(...);
    }

    public handleMessage(Message message) {
        TransitionDrawable drawable = (TransitionDrawable) getBackgroundDrawable();
        drawable.reverseTransition(mDelay);
    }

    public void start() {
        mHandler.post(mEvent);
    }

    public void stop() {
        mHandler.removeCallbacks(mEvent);
    }

}

Call start() to start your continuous transition, stop() to stop it. It is a bit like creating your own animation, but it works in a pinch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文