編輯:關於 Android 程式設計
想讓 App 裡的文字像跑馬燈一樣上下翻滾嗎?本文帶你動手做一個簡單的計時器,搭配自訂的 TextSwitcher,實現文字上下翻動的立體動畫效果。
核心思路是繼承 TextSwitcher,並為切換過程設計進出兩組 3D 翻轉動畫。以下為完整程式碼:
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, 16); 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(16); t.setMaxLines(1); t.setTextColor(mContext.getResources().getColor(R.color.textColor)); 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); } } 關鍵在於透過 Camera 的 rotateX 與 translate 組合,模擬出文字沿著 X 軸翻轉進場與退場的立體感;previous() 與 next() 則負責切換向上或向下的翻頁方向。
Demo 下載:https://xiazai.jb51.net/201611/yuanma/AndroidTextView(jb51.net).rar
以上就是本次分享的全部內容,希望對你的開發有所幫助。
Design Support Library 实战(九):FloatingActionButton 与 Behavior
先看 FloatingActionButton 和 ImageButton 的直观差别:左边是 ImageButton,右边是 FloatingActionButton。
硅谷商城源码解析(四):分类页面的实现思路与核心技术
一、前言 上一篇我们梳理了硅谷商城首页的关键技术点,这一篇把镜头转向分类页。分类页看起来朴素,其实集中了不少实战中高频出现的技巧:标签与分类栏的联动切换、屏幕自适应、左右
Android x86 應用調試方法與常用工具指南
Android 開發中,調試能力是解決問題的關鍵。本文整理 Android x86 平台上常用的調試工具與方法,涵蓋 SDK 工具、NDK 原生調試、圖形性能分析以及系統
Android 中的 Divider:從 ListView 到自訂分割線的實戰整理
分割線不只是畫一條線在 Android 介面開發裡,Divider 常被用來分隔列表項目、區塊或選單。看似簡單的一條線,實際上牽涉到邊距、列表高度計算、RecyclerV