android实现上下滚动的TextView

时间:2021-12-26 06:20:44

一 说明
    这里重要应用类 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="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>

 

    代码中没写太多注释,不过结构还算清晰,应该不难看懂!

三 小结
    我认为该控件实现的难点在于 动画文件的编写,即Rotate3dAnimation中applyTransformation(...)方法的实现,通过控制camara在Y方向上挪动和在X方向上的旋转,从而造成上下翻滚的视觉感,然后将该值转换到matrix上,从而改变了参数(..,Transformation t).有兴致的朋友可以直接改写该方法,便可失掉不同动画效果的TextSwitcher.