`
huakewoniu
  • 浏览: 46510 次
  • 性别: 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从左上角开始一直增大,直到回到初始状态。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics