周末下雨,拿这个时间来学习了下动画,对Android4.0.3主菜单里面实现的那个摆动挺好奇的,学习了下,大体的效果已经实现了,这篇文章小马就写下具体的实现,代码中间小马试了很多东西,也加了很多注释,希望大家不要嫌啰嗦,多试了下多学点动画,吼吼,不多说废话,老样子,先看效果,再看分解的代码,分解效果图如下:
先贴下文件目标结构,方便查看在文件中是如何引用动画资源的,截图如下:
![]()
1 View内容渐变效果图一:
![]()
2 View内容渐变效果图二:
![]()
3 View动画刚开始时效果图如下:
![]()
4 View动画播放到一半时效果图如下:
![]()
5 View动画播放结束时效果图如下:
![]()
好了,大体的效果看完了,下面小马来分解下代码,大家不用担心看不懂,小马会在最后将此DEMO源码放在附件中,供大家学习交流:
一:先看下主布局文件,里面没什么重要的东西,但为了完整还是贴下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <Button
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="播放ListView动画"
- />
- <ListView
- android:id="@+id/listview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layoutAnimation="@anim/list_layout_controller"
- android:persistentDrawingCache="animation|scrolling" />
- </LinearLayout>
二:下面来看下主控制类代码,如下:
- package com.xiaoma.listviewanimation.demo;
-
- import android.app.Activity;
- import android.graphics.Camera;
- import android.graphics.Matrix;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.AccelerateDecelerateInterpolator;
- import android.view.animation.AccelerateInterpolator;
- import android.view.animation.Animation;
- import android.view.animation.AnticipateInterpolator;
- import android.view.animation.AnticipateOvershootInterpolator;
- import android.view.animation.BounceInterpolator;
- import android.view.animation.CycleInterpolator;
- import android.view.animation.DecelerateInterpolator;
- import android.view.animation.LinearInterpolator;
- import android.view.animation.OvershootInterpolator;
- import android.view.animation.Transformation;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.ListView;
-
-
-
-
-
-
-
-
-
-
-
- public class ListViewAnimationDemoActivity extends Activity implements
- OnClickListener {
-
- private ListView listview;
- private Button btn;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
-
-
-
-
- private void init() {
- btn = (Button) findViewById(R.id.button);
- btn.setOnClickListener(this);
- String items[] = { "还记得", "那一年", "那一天", "有个人", "爱过我",
- "洗刷刷", "爱拉拉", "哇吼吼", "咔酷伊", "咔哇伊", "哦吼吼", "小马果"};
- listview = (ListView) findViewById(R.id.listview);
-
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- getApplicationContext(), android.R.layout.simple_list_item_1,
- items);
- listview.setAdapter(adapter);
- }
-
-
-
-
- @Override
- public void onClick(View v) {
-
- if (v.getId() == R.id.button) {
-
- startPlayAnimation();
- }
- }
-
-
-
-
- private void startPlayAnimation() {
-
- listview.startAnimation(new ListViewAnimationChange());
- }
-
-
-
-
-
-
-
-
- private class ListViewAnimationChange extends Animation {
-
- @Override
- public void initialize(int width, int height, int parentWidth,
- int parentHeight) {
-
-
- setDuration(5000);
-
- setFillAfter(true);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- setInterpolator(new AnticipateOvershootInterpolator());
-
- super.initialize(width, height, parentWidth, parentHeight);
- }
-
-
-
-
-
-
-
- @Override
- protected void applyTransformation(float interpolatedTime,
- Transformation t) {
- super.applyTransformation(interpolatedTime, t);
-
-
-
-
-
-
-
-
-
-
-
-
-
- Camera camera = new Camera();
-
- final Matrix matrix = t.getMatrix();
- camera.save();
-
-
-
-
- camera.translate(0.0f, 0.0f, (1300 - 1300.0f * interpolatedTime));
- camera.rotateY(360 * interpolatedTime);
- camera.getMatrix(matrix);
-
- matrix.preTranslate(-50f, -50f);
-
- matrix.postTranslate(50f, 50f);
- camera.restore();
-
-
- }
- }
- }
这个地方小马提醒下,在主控制类中,最重要的是那具自定义的动画类,它控制着VIEW的所有动画,如:插值器...2D渲染等,具体的看代码及注释部分的代码即可:
四:下面来看下用来关联View控件与动画的动画控制器代码:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 动画控制器,这个文件只是把要添加的动画与我们的动画资源关联起来
- 下面这个属性来控制控制动画的方向,如:比底部到顶部,或别的...
- 我们在主布局文件中控件的属性里面使用这个地方的动画控制器
- android:animationOrder="reverse"
- -->
- <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
- android:delay="30%"
- android:animationOrder="reverse"
- android:animation="@anim/scale"
- />
五:一个简单的透明度变化的文件,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- alpha
- 这个地方说几句,android:interpolator 大家自行换换别的试下
- -->
- <alpha xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/bounce_interpolator"
- android:fromAlpha="0.0"
- android:toAlpha="1.0"
- android:duration="1000"
- />
我已经在anim文件夹下创建了几个动画文件,大家可以尝试着更改下动画控制XML文件里面的动画文件引用来查看不同的效果哦,更好的效果,也可以再再往里面添加属性完成即可,最后,还是一样的,如果觉得小马文章写得不清楚的,有好建议的,可直接指点批评,有问题我们共同交流进步,谢谢啦,觉得看不爽的,小马也把源码放附件里面了,有需要学习的朋友可下载下,交流交流。。吼吼。。加油