編輯:關於 Android 程式設計
要做出「卡片沿著 Y 軸翻過去」這種帶有縱深的翻轉效果,關鍵是兩個 API:Animation 負責時間軸上的插值,Camera 負責在 Z 軸上做位移以製造透視縮放。兩者合起來,就是一個自訂的 Rotate3dAnimation。
繼承 Animation,覆寫 applyTransformation()。這個方法會在動畫進行的每一幀被呼叫,參數 interpolatedTime 是 0 到 1 的進度值。我們在裡面用 Camera 取得一個繞 Y 軸旋轉的 Matrix,塞進 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);
}
這是整段程式最容易被跳過、卻最關鍵的細節:
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
Matrix 的旋轉預設是繞「原點 (0,0)」進行的。如果直接套用,畫面左上角的原點會被釘住,整個 View 會像被釘在左上角那樣甩出去,而不是繞著自己的中心翻轉。
這兩行的做法是標準的「平移—操作—平移回來」:先用 preTranslate 把座標系搬動,使 View 的中心對齊到原點;接著讓 Camera 繞原點(也就是 View 的中心)旋轉;最後用 postTranslate 把座標系搬回原位。順序上,pre 系列會在既有 Matrix 之前串接,post 則在之後,這正是我們要的效果。
在 Activity 裡放兩個一樣大的 ImageView 到 FrameLayout 中,讓它們位置重疊。對最上層的做 0°→90° 的動畫;等它轉到側面看不見的瞬間,把兩個 View 的顯示狀態交換,再對後面的做 90°→180°。這樣接起來就是連續的 180° 翻轉。
// 第一段動畫結束後,趁着 90 度看不見的瞬間交換
private final class DisplayNextView implements Animation.AnimationListener {
public void onAnimationStart(Animation animation) { }
public void onAnimationEnd(Animation animation) {
mContainer.post(new SwapViews());
}
public void onAnimationRepeat(Animation animation) { }
}
private final class SwapViews implements Runnable {
@Override
public void run() {
mImageView1.setVisibility(View.GONE);
mImageView2.setVisibility(View.GONE);
mIndex++;
mStartAnimView = (0 == mIndex % 2) ? mImageView1 : mImageView2;
mStartAnimView.setVisibility(View.VISIBLE);
mStartAnimView.requestFocus();
Rotate3dAnimation rotation = new Rotate3dAnimation(
-90, 0, mCenterX, mCenterY, mDepthZ, false);
rotation.setDuration(mDuration);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
mStartAnimView.startAnimation(rotation);
}
}
注意 mContainer.post(new SwapViews()) 這一步:動畫回呼與 UI 更新必須在 UI 執行緒上進行,用 post 拋回 View 的訊息佇列是最直白的保證。另外兩段動畫用了不同的 Interpolator——前段 Accelerate、後段 Decelerate,一來一回摸起來才有「翻過去再稳稳停住」的手感。
// 觸發
@Override
public void onClick(View v) {
mCenterX = mContainer.getWidth() / 2;
mCenterY = mContainer.getHeight() / 2;
getDepthZ();
applyRotation(mStartAnimView, 0, 90);
}
private void applyRotation(View animView, float startAngle, float toAngle) {
Rotate3dAnimation rotation = new Rotate3dAnimation(
startAngle, toAngle, mCenterX, mCenterY, mDepthZ, true);
rotation.setDuration(mDuration);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView());
animView.startAnimation(rotation);
}
package com.nj1s.lib.anim;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
/**
* 讓 View 繞 Y 軸在兩個角度之間旋轉,並在 Z 軸上加入位移以強化立體感
*/
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();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float degrees = mFromDegrees + ((mToDegrees - mFromDegrees) * interpolatedTime);
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();
// 以 View 中心為軸心旋轉
matrix.preTranslate(-mCenterX, -mCenterY);
matrix.postTranslate(mCenterX, mCenterY);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Do 3d animation" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20px"
android:text="Input Depth on Z axis. [0, 300]" />
<EditText
android:id="@+id/edit_depthz"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="0" />
<FrameLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="20dp"
android:src="@drawable/f" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="20dp"
android:src="@drawable/s"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
那個可輸入的 Z 軸深度(0–300)就是透視強度的旋鈕:數值越大,翻轉時的「撲過來」感越強烈。順帶一提,若要在正式專案裡做卡片翻轉,Android 3.0 之後提供的 View.setRotationY() 搭配 setCameraDistance() 可以少寫很多程式;而若要更複雜的翻書效果,則得回到 Mesh 那套做法。
Android UI 實時預覽與高效編寫的實用技巧大全
很多初學者都聽前輩說過 Android Studio(下文簡稱 AS)的布局實時預覽很強大,但真正使用後卻常發現界面預覽效果不盡如人意: 其實這不全是 AS 的錯,
Firebase 上手 for Android:分析、用戶認證與實時數據庫
移動應用要做大一點,幾乎繞不開後端:用戶身份驗證、多設備數據同步、行為統計。可自建服務器意味著要懂運維、懂數據庫、懂接口設計——而多數獨立開發者的技能樹恰好缺這一支。 好
與其他 App 交互:Intent 的發送、接收與結果回傳
來源:https://developer.android.com/training/basics/intents/index.html 在 App 裡共享數據是再日常不過
Android 網絡編程(二):HttpClient 與 HttpURLConnection 實戰
前言 上一篇我們了解了 HTTP 協議原理,這一篇來談談 Apache 的 HttpClient 與 Java 的 HttpURLConnection。這兩者都是我們平常