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从左上角开始一直增大,直到回到初始状态。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值