android實(shí)現(xiàn)上下滾動(dòng)的TextView
一 說(shuō)明
這里重要應(yīng)用類 AutoTextView,這是一個(gè)自定義的類,繼承至TextSwitcher,下面臨 AutoTextView類做簡(jiǎn)要說(shuō)明:
1. 該類應(yīng)用的重點(diǎn),在于設(shè)置兩個(gè)動(dòng)畫, setInAnimation(...) 和 setOutAnimation(...),分離是文字進(jìn)入的動(dòng)畫和文字退出的動(dòng)畫;
2. 類中定義了一個(gè)外部類-Rotate3dAnimation,重要靠該類實(shí)現(xiàn)文字進(jìn)出動(dòng)畫,該外部類繼承至Animation。說(shuō)來(lái)偶合,這個(gè)恰好是在apiDemo中看到了,自定義Animation我還是第一次應(yīng)用,動(dòng)畫邏輯均在void applyTransformation(float interpolatedTime, Transformation t)中實(shí)現(xiàn),代碼相當(dāng)鋒利,我在原來(lái)的基礎(chǔ)上,更改了一下,實(shí)現(xiàn)了上述的效果,
二 代碼部分:
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分離構(gòu)成向下翻頁(yè)的進(jìn)出動(dòng)畫
private Rotate3dAnimation mInUp;
private Rotate3dAnimation mOutUp;
//mInDown,mOutDown分離構(gòu)成向下翻頁(yè)的進(jìn)出動(dòng)畫
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將執(zhí)行inAnimation,
//setOutAnimation()后,B將執(zhí)行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;
}
//定義動(dòng)作,向下滾動(dòng)翻頁(yè)
public void previous(){
if(getInAnimation() != mInDown){
setInAnimation(mInDown);
}
if(getOutAnimation() != mOutDown){
setOutAnimation(mOutDown);
}
}
//定義動(dòng)作,向上滾動(dòng)翻頁(yè)
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="http://schemas.android.com/apk/res/android"
xmlns:auto3d="http://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>
代碼中沒(méi)寫太多注釋,不過(guò)結(jié)構(gòu)還算清晰,應(yīng)該不難看懂!
三 小結(jié)
我認(rèn)為該控件實(shí)現(xiàn)的難點(diǎn)在于 動(dòng)畫文件的編寫,即Rotate3dAnimation中applyTransformation(...)方法的實(shí)現(xiàn),通過(guò)控制camara在Y方向上挪動(dòng)和在X方向上的旋轉(zhuǎn),從而造成上下翻滾的視覺(jué)感,然后將該值轉(zhuǎn)換到matrix上,從而改變了參數(shù)(..,Transformation t).有興致的朋友可以直接改寫該方法,便可失掉不同動(dòng)畫效果的TextSwitcher.
- android TextView設(shè)置中文字體加粗實(shí)現(xiàn)方法
- Android TextView設(shè)置背景色與邊框的方法詳解
- Android編程開(kāi)發(fā)之TextView文字顯示和修改方法(附TextView屬性介紹)
- Android TextView字體顏色設(shè)置方法小結(jié)
- android TextView多行文本(超過(guò)3行)使用ellipsize屬性無(wú)效問(wèn)題的解決方法
- android TextView不用ScrollViewe也可以滾動(dòng)的方法
- Android設(shè)置TextView顯示指定個(gè)數(shù)字符,超過(guò)部分顯示...(省略號(hào))的方法
- Android中TextView顯示圓圈背景或設(shè)置圓角的方法
- Android中TextView實(shí)現(xiàn)分段顯示不同顏色的字符串
- Android?妙用TextView實(shí)現(xiàn)左邊文字,右邊圖片
相關(guān)文章
Android實(shí)現(xiàn)圖片轉(zhuǎn)高斯模糊以及高斯模糊布局
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片轉(zhuǎn)高斯模糊的方法,以及高斯模糊布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android中使用Matrix控制圖形變換和制作倒影效果的方法
這篇文章主要介紹了Android中使用Matrix控制圖形變換和制作倒影效果的方法,用Matrix來(lái)作矩陣變化十分強(qiáng)大,文中的制作倒影的例子便是一個(gè)十分巧妙的運(yùn)用,需要的朋友可以參考下2016-04-04Android實(shí)現(xiàn)手勢(shì)滑動(dòng)識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手勢(shì)滑動(dòng)識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android開(kāi)發(fā)實(shí)現(xiàn)webview中img標(biāo)簽加載本地圖片的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)webview中img標(biāo)簽加載本地圖片的方法,結(jié)合實(shí)例形式分析了webview加載本地圖片的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android自定義view實(shí)現(xiàn)帶header和footer的Layout
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)帶header和footer的Layout,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02Android開(kāi)發(fā)Jetpack組件ViewModel使用講解
這篇文章主要介紹了Android?Jetpack架構(gòu)組件?ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲(chǔ)和管理界面相關(guān)的數(shù)據(jù),感興趣可以來(lái)學(xué)習(xí)一下2022-08-08Android Shape屬性創(chuàng)建環(huán)形進(jìn)度條
這篇文章主要介紹了Android Shape屬性創(chuàng)建環(huán)形進(jìn)度條的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Android 出現(xiàn):java.lang.NoClassDefFoundError...錯(cuò)誤解決辦法
這篇文章主要介紹了Android 出現(xiàn):Android出現(xiàn):java.lang.NoClassDefFoundError: android/os/PersistableBundle錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03Android 重力傳感器在游戲開(kāi)發(fā)中的應(yīng)用
本文主要介紹Android 重力傳感器,這里整理了詳細(xì)的資料,并且詳細(xì)的說(shuō)明重力傳感器的使用方法,有興趣的小伙伴可以參考下2016-08-08Android中實(shí)現(xiàn)淘寶購(gòu)物車RecyclerView或LIstView的嵌套選擇的邏輯
這篇文章主要介紹了Android中實(shí)現(xiàn)淘寶購(gòu)物車RecyclerView或LIstView的嵌套選擇的邏輯,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12