Android 自定义Animation,使用Camera对View组件作三维变换

Android 自定义Animation,使用Camera对View组件作三维变换_第1张图片

layout 布局


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="shortcut.song.com.myapplication.ListViewTweenActivity">

    <ListView
        android:id="@+id/id_list_3d"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/books"/>
LinearLayout>

自定义的Animation

package shortcut.song.com.myapplication;

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

/**
 * Created by Administrator on 2017/8/1 0001.
 */

public class MyAnimation extends Animation {

    private float centerX;
    private float centerY;
    //定义动画的持续时间
    private int duration;
    private Camera camera = new Camera();

    public MyAnimation(float x, float y, int duration)
    {
        this.centerX = x;
        this.centerY = y;
        this.duration = duration;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        setDuration(duration);//设置动画的持续时间
        setFillAfter(true); //设置动画结束后效果
        setInterpolator(new LinearInterpolator());
    }

    /*
    *   interpolatedTime:代表了抽象的动画持续时间,不管动画实际持续多长时间,interpolatedTime参数总是从0(动画开始时)变化到1(动画结束时)
    *   Transformation:代表了对目标组件所做的改变
    *
     */
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        camera.save();
        //根据interpolateTime时间来控制X,Y,Z上的偏移
        camera.translate(100.0f - 100.0f * interpolatedTime, 150.0f * interpolatedTime - 150, 80.0f - 80.0f * interpolatedTime );
        //根据interpolateTime时间在Y轴上旋转不同角度
        camera.rotateY(360 * (interpolatedTime));
        //根据interpolateTime时间在X轴上旋转不同角度
        camera.rotateX(360 * (interpolatedTime));
        //获取Transformation的Matrix参数
        Matrix matrix = t.getMatrix();
        camera.getMatrix(matrix);
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
        camera.restore();
    }
}
package shortcut.song.com.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ListView;

public class ListViewTweenActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_tween);
        ListView listView = (ListView)findViewById(R.id.id_list_3d);

        WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics  metrics = new DisplayMetrics();
        display.getMetrics(metrics);

        //设置对ListView组件应用动画
        listView.startAnimation(new MyAnimation(metrics.xdpi / 2, metrics.ydpi / 2, 3500));

    }
}

运行效果:

Android 自定义Animation,使用Camera对View组件作三维变换_第2张图片

你可能感兴趣的:(Android)