麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > Android > 正文

Android動畫之3D翻轉效果實現函數分析

2020-04-11 12:33:42
字體:
來源:轉載
供稿:網友


Android中的翻轉動畫效果的實現,首先看一下運行效果如上圖所示.
Android中并沒有提供直接做3D翻轉的動畫,所以關于3D翻轉的動畫效果需要我們自己實現,那么我們首先來分析一下Animation 和 Transformation。

Animation動畫的主要接口,其中主要定義了動畫的一些屬性比如開始時間,持續時間,是否重復播放等等。而Transformation中則包含一個矩陣和alpha值,矩陣是用來做平移,旋轉和縮放動畫的,而alpha值是用來做alpha動畫的,要實現3D旋轉動畫我們需要繼承自Animation類來實現,我們需要重載getTransformation和applyTransformation,在getTransformation中Animation會根據動畫的屬性來產生一系列的差值點,然后將這些差值點傳給applyTransformation,這個函數將根據這些點來生成不同的Transformation。下面是具體實現:

復制代碼 代碼如下:

package com.example.textviewtest;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Rotate3dAnimation extends Animation {
// 開始角度
private final float mFromDegrees;
// 結束角度
private final float mToDegrees;
// 中心點
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
// 是否需要扭曲
private final boolean mReverse;
// 攝像頭
private Camera mCamera;
public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX,
float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
// 生成Transformation
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
// 生成中間角度
float degrees = fromDegrees
+ ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
// 取得變換后的矩陣
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}

其中包括了旋轉的開始和結束角度,中心點、是否扭曲、和一個Camera,這里我們主要分析applyTransformation函數,其中第一個參數就是通過getTransformation函數傳遞的差指點,然后我們根據這個差值通過線性差值算法計算出一個中間角度degrees,Camera類是用來實現繞Y軸旋轉后透視投影的,因此我們首先通過t.getMatrix()取得當前的矩陣,然后通過camera.translate來對矩陣進行平移變換操作,camera.rotateY進行旋轉。這樣我們就可以很輕松的實現3D旋轉效果了。

下面是布局文件main.xml:
復制代碼 代碼如下:

View Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_screen_bg"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/next_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:drawableTop="@drawable/qiangpiao_dropdown"
android:text="下一個" />
<TextView
android:id="@+id/tv"
android:layout_width="300dip"
android:layout_height="300dip"
android:layout_gravity="center"
android:background="@drawable/call_show_frame_safe"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>

MainActivity的代碼如下
復制代碼 代碼如下:

View Code
package com.example.textviewtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
private Button btn;
private int count = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
tv.setText(String.valueOf(count));
btn = (Button) findViewById(R.id.next_btn);
applyRotation(0, 90);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
applyRotation(0, 90);
}
});
}
private void applyRotation(float start, float end) {
// 計算中心點
final float centerX = tv.getWidth() / 2.0f;
final float centerY = tv.getHeight() / 2.0f;
final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end,
centerX, centerY, 310.0f, true);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
// 設置監聽
rotation.setAnimationListener(new DisplayNextView());
tv.startAnimation(rotation);
}
private final class DisplayNextView implements Animation.AnimationListener {
public void onAnimationStart(Animation animation) {
}
// 動畫結束
public void onAnimationEnd(Animation animation) {
tv.post(new SwapViews());
}
public void onAnimationRepeat(Animation animation) {
}
}
private final class SwapViews implements Runnable {
public void run() {
final float centerX = tv.getWidth() / 2.0f;
final float centerY = tv.getHeight() / 2.0f;
Rotate3dAnimation rotation = null;
tv.requestFocus();
rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f,
false);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
// 開始動畫
tv.startAnimation(rotation);
tv.setText(String.valueOf(count++));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

看懂了嗎?呵呵。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成年免费视频黄网站在线观看 | 国产成人精品免高潮在线观看 | 一本色道精品久久一区二区三区 | 久久亚洲成人 | 精品久久久久久综合日本 | 成人在线观看免费视频 | 精品成人国产在线观看男人呻吟 | 九九热精品视频在线免费观看 | 男女无遮挡羞羞视频 | 97黄色网 | 天海翼无删减av三级在线观看 | 免费观看视频91 | 国产高潮好爽好大受不了了 | 免费国产一级特黄久久 | 91精品国产乱码久久久久 | 精品国产一区二区三区成人影院 | 青草av.久久免费一区 | 亚洲91在线| 日本在线国产 | 精品成人av一区二区在线播放 | 欧美性色黄大片www 操碰网 | 毛片在哪看 | 久久96国产精品久久久 | 91久久国产综合久久91猫猫 | 国产精品免费小视频 | 13一14毛片免费看 | 中文在线观看www | 操皮视频 | 97se亚洲综合在线韩国专区福利 | 超久久| www.99av| 黄色小视频免费在线观看 | 免费观看三级毛片 | 欧美成人一二三区 | 久久最新网址 | 91精品国产91久久久久久 | 爱高潮www亚洲精品 欧美黄色一级片视频 | 在线成人免费网站 | 国产精品免费观在线 | 国产精品视频久 | 久久久久久久久成人 |