`
huakewoniu
  • 浏览: 47630 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

ViewAnimation

阅读更多

view animation 的实现

Understanding View Animation
When a view is displayed on a presentation surface in Android, it goes through a
transformation matrix. In graphics applications, you use transformation matrices to
transform a view in some way. The process involves taking the input set of pixel
coordinates and color combinations and translating them into a new set of pixel
coordinates and color combinations. At the end of a transformation, you will see an
altered picture in terms of size, position, orientation, or color.
You can achieve all of these transformations mathematically by taking the input set of
coordinates and multiplying them in some manner using a transformation matrix to arrive
at a new set of coordinates. By changing the transformation matrix, you can impact how
a view will look. A matrix that doesn’t change the view when you multiply it is called an
identity matrix. You typically start with an identity matrix and apply a series of
transformations involving size, position, and orientation. You then take the final matrix
and use that matrix to draw the view.

view Animation 本质是view利用一个Animation的类(或是子类)实现动画的效果。而Animation是通过修改view的
transformation matrices,来达到控制view的行为的目的。
本例是先实现一个ViewAnimation的类,重写了Animation的一些必要的方法,来实现一个ListView 的动画效果。
当然这个Animation必须现在ListView上注册才行,利用 listView.startAnimation(new ViewAnimation())就行了。
/**
 *
 */
package hust.ophoneclub.ViewAnimation;

import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;

/**
 * @author chenhao
 *
 */
public class ViewAnimation extends Animation {

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        final Matrix matirx = t.getMatrix();
        matirx.setScale(interpolatedTime, interpolatedTime);
        super.applyTransformation(interpolatedTime, t);
    }

    @Override
    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
//        Initialize this animation with the dimensions of the object being
//        animated as well as the objects parents. (This is to support animation
//        sizes being specifed relative to these dimensions.)
        super.initialize(width, height, parentWidth, parentHeight);
        setDuration(2500);  //动画持续的时间
        setFillAfter(true); //动画最终的状态
        setInterpolator(new LinearInterpolator());//定义动画的布局形式
    }
}

下面是展示这个动画的Activity。
package hust.ophoneclub.ViewAnimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ViewAnimationActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupListItem();
        setupButton();
    }

    /**
     *
     */
    private void setupButton() {
        Button btn = (Button)findViewById(R.id.button_id);
        btn.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                animateListView();
            }
        });
    }

    /**
     *
     */
    private void setupListItem() {
        String[] listItem = new String[]{
                "item 1", "item 2", "item 3",
                "item 4", "item 5", "item 6"
        };
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1, listItem);
        getListVIew().setAdapter(arrayAdapter);
    }

    /**
     * @return
     */
    private ListView getListVIew() {
        ListView listView = (ListView)findViewById(R.id.list_view_id);
        return listView;
    }
   
    private void animateListView() {
     //在listView上注册这个Animation
        getListVIew().startAnimation(new ViewAnimation());
    }
}

下面是这个Activity的布局文件
<?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"
    >
    <Button
    android:id = "@+id/button_id"
    android:layout_width = "fill_parent"
    android:layout_height ="wrap_content"
    android:text = "Start animation"
    />
<ListView
 android:id = "@+id/list_view_id"
    android:layout_width="fill_parent"
    android:persistentDrawingCache = "scrolling|animation"
    android:layout_height="fill_parent"
   
    />
</LinearLayout>

实现的效果如下。

首先运行程序

 

点击 “StartAnimation”之后开始动画。 这个listView从左上角开始一直增大,直到回到初始状态。

分享到:
评论

相关推荐

    博客《Android动画之二:View Animation》附带源码 ViewAnimationDemo

    **Android 动画详解:深入理解View Animation** 在Android开发中,动画是提升用户体验的关键因素之一,能够使应用更加生动和吸引人。本篇将详细探讨Android中的View Animation,它是Android早期提供的动画机制,...

    View Animation Demo 源代码

    "View Animation Demo 源代码"是一个典型的Android项目,展示了如何利用视图动画(View Animation)来实现各种动态效果,如旋转、缩放等。这个源代码提供了很好的学习资源,帮助开发者理解和实践Android中的基本动画...

    Android Animation示例(View Animation, Drawable Animation)

    View Animation Only animate View objects If animate non-view objects, you have to implement your own code takes less time to setup and requires less code to write Drawable Animation Load drawable ...

    android动画之帧动画(drawable animation)和补间动画(view animation)

    本文将深入探讨两种主要的动画类型:帧动画(Drawable Animation)和补间动画(View Animation),并以一个简单的“太阳地球月亮”模型为例,来阐述这两种动画的实现原理和应用场景。 ### 帧动画 (Drawable ...

    安卓Android源码——View中添加Animation.zip

    本资源包"安卓Android源码——View中添加Animation.zip"主要关注如何在Android的View组件中应用动画效果。接下来,我们将深入探讨Android视图动画的实现原理及其在实际开发中的应用。 一、Android动画类型 1. **帧...

    Android使用View Animation实现动画加载界面

    Android View Animation实现动画加载界面 Android View Animation是Android系统中的一种动画实现方式,通过使用View Animation,可以实现不同的动画效果,例如旋转、缩放、平移等。下面是使用View Animation实现...

    Android源码——View中添加Animation.zip

    这个压缩包“Android源码——View中添加Animation.zip”包含了关于在Android的View中如何添加动画的详细资源,包括一个图片示例(1-1209122202530-L.png)、源码说明文本(源码说明.txt)、以及一个链接到更多源码的...

    Android代码-View中添加Animation.zip

    `View`中的动画主要分为两种类型:属性动画(Property Animation)和视图动画(View Animation)。本压缩包"Android代码-View中添加Animation.zip"可能包含了如何在Android应用的`View`中添加这两种动画的相关代码...

    SurfaceView中添加Animation

    主要有两种类型的动画:Property Animation(API 11+)和View Animation(API 1+)。在这个例子中,我们可能使用的是View Animation,因为SurfaceView通常需要兼容低版本Android设备。 1. **定义Animation** 创建...

    android-view-animation抖动shake

    "android-view-animation抖动shake"这个话题主要涉及到了Android视图动画中的shake效果,这是一种常见的错误提示或吸引用户注意力的动画方式,比如当用户输入错误时,可以令编辑框(EditText)进行抖动来提示。...

    AndroidAnimation

    Android Tween Animation分为两种主要类型:Property Animation和View Animation。Property Animation是Android 3.0(API Level 11)引入的新特性,它可以对对象的任何属性进行动画处理,而不仅仅是View。然而,View...

    Android View中添加Animation.zip

    本资料“Android View中添加Animation.zip”显然聚焦于如何在Android的View组件上应用各种动画效果。以下是关于Android View动画的详细阐述: 1. **基本概念** - **Animation**:Android中的动画主要分为两种类型...

    animation动画

    本篇将详细探讨Android中的动画技术,包括属性动画(Property Animation)、视图动画(View Animation)以及过渡动画(Transition Animation)。 **属性动画(Property Animation)** 属性动画系统是Android 3.0...

    Animation动画实例源代码

    Android动画主要包括两种类型:属性动画(Property Animation)和视图动画(View Animation)。视图动画是早期Android版本中的动画系统,主要通过改变视图的位置、大小、透明度等属性来模拟动画效果,但并不真正改变...

    [Android][Animation动画]

    本文将深入探讨Android中的动画技术,包括`Animation`、`PropertyAnimation`、`ViewAnimation`以及`DrawableAnim`。 首先,我们来了解Android的基础动画系统,即`Animation`类。`Animation`类是所有动画的基类,它...

    Android View中添加Animation.rar

    本资源"Android View中添加Animation.rar"主要探讨如何在Android的View组件中应用动画。通过学习这些内容,开发者可以更好地掌握Android动画机制,为应用程序增添动态美感。 首先,Android提供了两种主要的动画类型...

    安卓Animation实现APP引导用户点击动画

    Android Animation主要包括三种类型的动画:Property Animation、View Animation和Transition Animation。在本示例中,我们主要关注的是View Animation,它是早期版本(API Level 8及以下)中的动画机制,主要通过...

    andorid animation

    Android提供了多种动画机制,包括属性动画(Property Animation)、视图动画(View Animation)和框架动画(Frame Animation)。这篇博客文章“andorid animation”可能深入探讨了这些主题,并结合源码分析和实用...

    Android程序研发源码Android View中添加Animation.zip

    在Android程序开发中,View动画(Animation)是一个关键部分,用于增强用户界面的交互性和视觉效果。本资源“Android程序研发源码Android View中添加Animation.zip”提供了关于如何在Android View上实现动画的源代码...

Global site tag (gtag.js) - Google Analytics