編輯:關於 Android 程式設計
一、說明
這裡主要用到自訂類別 AutoTextView,它繼承自 TextSwitcher。以下先對 AutoTextView 做個簡要說明:
1. 這個類別的重點在於設定兩個動畫——setInAnimation(...) 與 setOutAnimation(...),分別是文字進入的動畫和文字退出的動畫;
2. 類別中定義了一個內部類 Rotate3dAnimation,主要依靠它來實現文字進出動畫,這個內部類繼承自 Animation。說來巧合,這恰好是在 apiDemo 中看到的,自訂 Animation 我還是第一次使用,動畫邏輯都在 void applyTransformation(float interpolatedTime, Transformation t) 中實現。我在原來的基礎上稍作修改,便實現了上述效果。
二、程式碼部分:
1. AutoTextView.java
複製程式碼 程式碼如下:
package com.example.animtextview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class AutoTextView extends TextSwitcher implements
ViewSwitcher.ViewFactory {
private float mHeight;
private Context mContext;
//mInUp,mOutUp分離構成向下翻頁的進出動畫
private Rotate3dAnimation mInUp;
private Rotate3dAnimation mOutUp;
//mInDown,mOutDown分離構成向下翻頁的進出動畫
private Rotate3dAnimation mInDown;
private Rotate3dAnimation mOutDown;
public AutoTextView(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
public AutoTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.auto3d);
mHeight = a.getDimension(R.styleable.auto3d_textSize, 36);
a.recycle();
mContext = context;
init();
}
private void init() {
// TODO Auto-generated method stub
setFactory(this);
mInUp = createAnim(-90, 0 , true, true);
mOutUp = createAnim(0, 90, false, true);
mInDown = createAnim(90, 0 , true , false);
mOutDown = createAnim(0, -90, false, false);
//TextSwitcher重要用於文件切換,比如 從文字A 切換到 文字 B,
//setInAnimation()後,A將執行inAnimation,
//setOutAnimation()後,B將執行OutAnimation
setInAnimation(mInUp);
setOutAnimation(mOutUp);
}
private Rotate3dAnimation createAnim(float start, float end, boolean turnIn, boolean turnUp){
final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, turnIn, turnUp);
rotation.setDuration(800);
rotation.setFillAfter(false);
rotation.setInterpolator(new AccelerateInterpolator());
return rotation;
}
//這裡返回的TextView,就是我們看到的View
@Override
public View makeView() {
// TODO Auto-generated method stub
TextView t = new TextView(mContext);
t.setGravity(Gravity.CENTER);
t.setTextSize(mHeight);
t.setMaxLines(2);
return t;
}
//定義動作,向下滾動翻頁
public void previous(){
if(getInAnimation() != mInDown){
setInAnimation(mInDown);
}
if(getOutAnimation() != mOutDown){
setOutAnimation(mOutDown);
}
}
//定義動作,向上滾動翻頁
public void next(){
if(getInAnimation() != mInUp){
setInAnimation(mInUp);
}
if(getOutAnimation() != mOutUp){
setOutAnimation(mOutUp);
}
}
class Rotate3dAnimation extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private float mCenterX;
private float mCenterY;
private final boolean mTurnIn;
private final boolean mTurnUp;
private Camera mCamera;
public Rotate3dAnimation(float fromDegrees, float toDegrees, boolean turnIn, boolean turnUp) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mTurnIn = turnIn;
mTurnUp = turnUp;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
mCenterY = getHeight() / 2;
mCenterX = getWidth() / 2;
}
@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 int derection = mTurnUp ? 1: -1;
final Matrix matrix = t.getMatrix();
camera.save();
if (mTurnIn) {
camera.translate(0.0f, derection *mCenterY * (interpolatedTime - 1.0f), 0.0f);
} else {
camera.translate(0.0f, derection *mCenterY * (interpolatedTime), 0.0f);
}
camera.rotateX(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
}
2. MainActivity.java
複製程式碼 程式碼如下:
package com.example.animtextview;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button mBtnNext;
private Button mBtnPrev;
private AutoTextView mTextView02;
private static int sCount = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// TODO Auto-generated method stub
mBtnNext = (Button) findViewById(R.id.next);
mBtnPrev = (Button) findViewById(R.id.prev);
mTextView02 = (AutoTextView) findViewById(R.id.switcher02);
mTextView02.setText("Hello world!");
mBtnPrev.setOnClickListener(this);
mBtnNext.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.next:
mTextView02.next();
sCount++;
break;
case R.id.prev:
mTextView02.previous();
sCount--;
break;
}
mTextView02.setText(sCount%2==0 ?
sCount+"AAFirstAA" :
sCount+"BBBBBBB");
System.out.println("getH: ["+mTextView02.getHeight()+"]");
}
}
3. activity_main.xml
複製程式碼 程式碼如下:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:auto3d="https://schemas.android.com/apk/res/com.example.animtextview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/next" />
<Button
android:id="@+id/prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/prev" />
</RelativeLayout>
<com.example.animtextview.AutoTextView
android:id="@+id/switcher02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
auto3d:textSize="30sp" />
</LinearLayout>
程式碼裡沒寫太多註解,不過結構還算清晰,應該不難看懂!
三、小結
我認為這個控制項實現的難點在於動畫的編寫,也就是 Rotate3dAnimation 中 applyTransformation(...) 方法的實現——透過控制 camera 在 Y 方向上移動、在 X 方向上旋轉,營造出上下翻滾的視覺感,再把這個值轉換到 matrix 上,從而改變 Transformation 參數。有興趣的朋友可以直接改寫這個方法,就能得到不同動畫效果的 TextSwitcher。
Android 事件攔截機制入門:從模擬公司組織理解 Touch 事件
前言 事件攔截機制是 Android 初學者常感到困惑的主題。其實只要理解事件如何從上層容器往下傳,以及何時被攔截、何時被消費,就能掌握大部分 Touch 事件流程。 什
Android一鍵切換應用主題顏色(二):讓圖標跟隨變色
上一篇我們聊了一鍵切換應用主題顏色,今天接著往下講:如何讓應用裡的一些圖標也跟隨主題色一起變化。比如首頁固定的展示圖標、單選框與複選框的圖標,還有一些功能按鈕的圖標等等。
RecyclerView完全指南:從核心概念到點擊、頭尾與自動加載
簡介 RecyclerView是Android中非常核心的列表元件。相較於ListView,它提供了更靈活的佈局能力、更清晰的元件分工,以及更完善的動畫與效能支援。 官方
Android 自定義 View 實作隨機圖片驗證碼
自定義 View 的基本流程 製作隨機圖片驗證碼,不只是為了功能,也是整理自定義 View 開發流程的好機會。一般步驟包含定義屬性、在構造方法中讀取屬性、重寫 onMea