Animation使用方法(二)

本文介绍了一种使用Android动画实现列表项渐显效果的方法。通过自定义动画控制器,实现了列表视图中每一项以淡入的形式出现。该教程适用于Android开发者学习动画效果的应用。
效果图同:Animation使用方法(一)的效果图
工程结构图:
[img]
[img]http://dl.iteye.com/upload/attachment/617390/6bce39dc-fea9-3c9f-ab1c-d87cd2ac3b85.png[/img]
[/img]

可以看到,比Animation使用方法(一)的工程结构少了一个:list_controller.xml
布局文件的
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@id/android:list"
/>
</LinearLayout>


user.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/name"
android:layout_width="180dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:textColor="#fff"
android:textSize="10pt"
android:singleLine="true"
/>
<TextView
android:id="@+id/age"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
android:textColor="#fff"
android:gravity="right"
android:textSize="10pt"
/>
</LinearLayout>


/res/anim/alpha.xml
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
></alpha>
</set>


AnimationDemo4Activity
package cxt.demo;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.Animation.AnimationListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class AnimationDemo4Activity extends ListActivity {
private ArrayList<HashMap<String,Object>> list = null;
private ListView listView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = getListView();
list = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> m1 = new HashMap<String, Object>();
HashMap<String,Object> m2 = new HashMap<String, Object>();
HashMap<String,Object> m3 = new HashMap<String, Object>();
m1.put("image", R.drawable.z11);
m1.put("name", "Jack");
m1.put("age","63");
m2.put("image", R.drawable.z22);
m2.put("name", "Bob");
m2.put("age","15");
m3.put("image", R.drawable.z33);
m3.put("name", "Theron");
m3.put("age","25");
list.add(m1);
list.add(m2);
list.add(m3);
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"image","name","age"}, new int[]{R.id.image,R.id.name,R.id.age});
setListAdapter(adapter);

Animation animation = AnimationUtils.loadAnimation(AnimationDemo4Activity.this, R.anim.alpha);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(controller);
}

private class AnimationImpl implements AnimationListener{

@Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub

}

}

}


本文转自:http://theron.blog.51cto.com/2383825/656690
只可用于学习。
<think>好的,我现在需要帮助用户查找DOTween Animation使用技巧和最佳实践,包括示例代码。首先,我应该回顾用户提供的引用内容,看看有哪些相关的信息可以利用。 用户给出了五个引用,其中引用[1]提到了基本的动画实例代码,比如DOMove和DORotate,还有DOTween的应用。引用[2]提到了OnComplete和OnUpdate事件的使用,这对于处理动画完成后的操作和每帧更新很有用。引用[4]介绍了DOTween的基本信息,说明它是一个强大的动画引擎。引用[5]提到了蒙皮网格的优化,可能和性能优化相关,但用户的问题是关于DOTween的,可能需要结合性能方面的最佳实践。引用[3]虽然关于第三方插件的引入,但用户的问题不涉及这部分,可以暂时忽略。 接下来,我需要整理DOTween的使用技巧和最佳实践。根据已有的知识,DOTween的使用技巧包括链式调用、动画序列、缓动函数的选择、对象池管理、性能优化、事件回调等。最佳实践可能涉及避免内存泄漏、合理使用Kill方法、动画暂停与恢复、组件复用等。此外,示例代码需要覆盖基本动画、序列动画、事件处理等场景。 用户可能需要具体的代码示例,所以应该提供几个典型的例子,比如移动、旋转、颜色渐变等,同时结合事件处理。另外,性能优化方面,比如使用对象池、避免频繁创建动画、合理设置AutoKill等,都是重要的点。还要注意引用中提到的OnComplete和OnUpdate方法,应该包含在示例中,以展示如何在这些事件中添加逻辑。 需要确保回答结构清晰,分点列出技巧、实践和代码,每个部分有明确的标题。同时,引用的内容要自然地融入回答中,比如在提到事件处理时引用[2],在介绍DOTween时引用[4]。注意检查LaTeX格式是否符合要求,但用户的问题中可能不涉及数学公式,所以可能不需要使用。不过,用户提到如果有行内数学表达式需要用$...$,但这里可能不适用,所以可能不需要处理。 最后,生成相关问题,考虑用户可能想了解的扩展内容,比如性能优化、复杂动画实现、与其他插件的比较等。确保问题与DOTween的高级使用相关,帮助用户进一步探索。</think># DOTween Animation使用技巧与最佳实践 ## 一、核心使用技巧 1. **链式调用优化代码** 通过`.Append()`和`.Join()`实现动画组合: ```csharp Sequence mySequence = DOTween.Sequence(); mySequence.Append(transform.DOMoveX(5, 1)) .Join(transform.DORotate(new Vector3(0, 180, 0), 1)) .AppendInterval(0.5f); ``` 2. **缓动函数选择** 使用`SetEase()`提升动画效果: ```csharp transform.DOMoveY(3, 1).SetEase(Ease.OutBounce); ``` 3. **对象池管理** 复用动画对象提升性能: ```csharp Tween tween = transform.DOScale(Vector3.one * 2, 1).SetAutoKill(false); tween.Rewind(); // 复用动画对象 ``` ## 、最佳实践方案 1. **动画生命周期控制** ```csharp // 明确控制动画销毁 Tween myTween = transform.DOMoveX(10, 3); myTween.OnComplete(() => Destroy(gameObject)); ``` 2. **性能优化策略** - 使用`SetUpdate(true)`实现独立时间轴动画 - 禁用物理模拟对象动画的timeScale影响: ```csharp transform.DOMoveX(5, 1).SetUpdate(UpdateType.Fixed); ``` 3. **UI动画规范** ```csharp // 使用专用UI方法避免布局失效 RectTransform rt = GetComponent<RectTransform>(); rt.DOAnchorPosX(100, 1).SetEase(Ease.OutBack); ``` ## 三、典型代码示例 ### 1. 复合动画场景 ```csharp DOTween.Sequence() .Append(transform.DOMove(new Vector3(0,5,0), 1)) .Join(GetComponent<Renderer>().material.DOColor(Color.red, 1)) .AppendCallback(() => Debug.Log("第一阶段完成")) .Append(transform.DORotate(Vector3.up * 360, 2, RotateMode.FastBeyond360)) .SetLoops(-1, LoopType.Yoyo); ``` ### 2. 事件驱动动画 ```csharp transform.DOMoveX(10, 3) .OnStart(() => Debug.Log("动画开始")) .OnUpdate(() => UpdateProgressBar()) .OnComplete(() => ShowCompletionEffect()) [^2]; ``` ### 3. 路径动画 ```csharp DOTween.Path(() => transform.position, x => transform.position = x, new[] { new Vector3(0,0,0), new Vector3(2,2,0), new Vector3(5,0,0) }, 3, PathType.CatmullRom) .SetOptions(true); ``` ## 四、高级优化建议 1. 使用`DOTween.SetTweensCapacity(200, 50);`预设容量 2. 对频繁使用的动画启用`SetRecyclable(true)` 3. 使用`DOTween.To()`实现自定义动画: ```csharp DOTween.To(() => myValue, x => myValue = x, 10, 1); ``` 4. 批量控制动画: ```csharp // 暂停所有带"Enemy"标签的动画 DOTween.PauseAll("Enemy"); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值