如何使continously在运行Android TransitionDrawable动画? [英] How to make continously running TransitionDrawable animation in Android?

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

问题描述

我试图做一个动画的标志。它由两个静态图像。

我会喜欢,实现了交叉淡入效果。

我的使用TransitionDrawable的做到了,设置crossFadeEnabled,一切看起来不错。

的事情是,我需要在社交圈运行。怎样才可以实现?

 <过渡的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
  <项目机器人:可绘制=@可绘制/ image_expand>
  <项目机器人:可绘制=@可绘制/ image_collapse>
< /转换>

资源RES = mContext.getResources();
TransitionDrawable过渡=(TransitionDrawable)res.getDrawable(R.drawable.expand_collapse);
ImageView的形象=(ImageView的)findViewById(R.id.toggle_image);
image.setImageDrawable(过渡);
 

这是谷歌这样的code的完美运行。 最importantn的是,在需要的Andr​​oid 1.6下工作。

解决方案

我设法转型,通过绘制从drawable1到drawable2过渡后反转方向的处理方法的工作。在我的XML的:

< XML版本=1.0编码=UTF-8? > <过渡的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>     <项目机器人:可绘制=@可绘制/ background_animation1/>     <项目机器人:可绘制=@可绘制/ background_animation2/> < /转换>

在可绘有梯度:

< XML版本=1.0编码=UTF-8? > <形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android     机器人:形状=矩形>      <边角安卓topLeftRadius =10dp机器人:topRightRadius =10dp机器人:bottomLeftRadius =10dp机器人:bottomRightRadius =10dp/>          <梯度         机器人:startColor =#d200ff         机器人:centerColor =#4e00ff         机器人:endColor =#006cff/> < /形状>

< XML版本=1.0编码=UTF-8? > <形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android     机器人:形状=矩形>     <大小机器人:宽=900dp机器人:身高=500dp/>     <边角安卓topLeftRadius =10dp机器人:topRightRadius =10dp机器人:bottomLeftRadius =10dp机器人:bottomRightRadius =10dp/>     <梯度         机器人:startColor =#006cff         机器人:centerColor =#FF6600         机器人:endColor =#d200ff/> < /形状>

在我的初始化code:

反=(TransitionDrawable)getResources()。getDrawable(R.drawable.transition) ; trans.setCrossFadeEnabled(真正的); backgroundimage.setImageDrawable(反式); .... handlechange();

而在处理code中的最重要的位;请注意,在全球范围内运行的标志。我得到了姜饼和奇巧这种运行;

无效handlechange1() { 处理器手=新的处理程序(); hand.postDelayed(新的Runnable() { @覆盖 公共无效的run() { 更改(); } 私人无效的变化() { 如果(旗) { trans.startTransition(8000); 标志= FALSE; } 其他 { trans.reverseTransition(8000); 标志=真正的; } handlechange1(); } },8000); }

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.

解决方案

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

	}

这篇关于如何使continously在运行Android TransitionDrawable动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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