Android TextView豎直滾動文字廣告效果
阿新 • • 發佈:2019-01-07
專案需要TextView單行豎直滾動文字廣告效果,很簡單的功能在網上找了很多沒有想要的效果。
開始找到的http://www.cnblogs.com/vaiyanzi/archive/2011/12/06/2277791.html,歌詞效果,對程式碼處理一下變成單行後,發現沒有的動態動態滾動的效果,只有文字的瞬間切換。
後來又找到了http://blog.csdn.net/ville_zeng/article/details/8953140,3d滾動效果。我只想要一個簡單的滾動就這麼難嗎,好吧接著改程式碼吧。還是很感謝博主的,最後還是在這篇文章的基礎上修改的。直接上程式碼。
1、自定義TextView:
2、佈局檔案:package com.cx.textviewverticalrolling; import android.content.Context; 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 = 10; private Context mContext; //mInUp,mOutUp分別構成向下翻頁的進出動畫 private Animation mInUp; private Animation mOutUp; //mInDown,mOutDown分別構成向下翻頁的進出動畫 private Animation mInDown; private Animation 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 mContext = context; init(); } private void init() { // TODO Auto-generated method stub setFactory(this); mInUp = createAnim(0, 0 , true, true); mOutUp = createAnim(0, 0, false, true); mInDown = createAnim(90, 0 , true , false); mOutDown = createAnim(0, -90, false, false); setInAnimation(mInUp); setOutAnimation(mOutUp); } private Animation createAnim(float start, float end, boolean turnIn, boolean turnUp){ final Animation rotation = new Rotate3dAnimation(start, end, turnIn, turnUp); rotation.setDuration(1000); 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_VERTICAL); t.setTextSize(mHeight); t.setMaxLines(1); return t; } //定義動作,向下滾動翻頁 public void previous(){ if(getInAnimation() != mInDown){ setInAnimation(mInDown); } if(getOutAnimation() != mOutDown){ setOutAnimation(mOutDown); } } //定義動作,向上滾動翻頁 public void next(){ if(getOutAnimation() != mOutUp){ setOutAnimation(mOutUp); } if(getInAnimation() != mInUp){ setInAnimation(mInUp); } } 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(); 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); } } }
3、主介面新增資料,設定定時器。<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000" > <com.cx.textviewverticalrolling.AutoTextView android:id="@+id/autoTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="#fff" > </com.cx.textviewverticalrolling.AutoTextView> </RelativeLayout>
原始碼下載package com.cx.textviewverticalrolling; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Handler; public class MainActivity extends Activity { private Handler handler = new Handler(); private AutoTextView autoTextView; private int count = 0; private List<String> arrList = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getData(); autoTextView = (AutoTextView) findViewById(R.id.autoTextView); handler.postDelayed(runnable, 0); } private void getData() { // TODO Auto-generated method stub for (int i = 0; i < 10; i++) { arrList.add("豎直滾動TextView-資料" + i); } } Runnable runnable = new Runnable() { @Override public void run() { // handler自帶方法實現定時器 try { handler.postDelayed(this, 3000); autoTextView.next(); autoTextView.setText(arrList.get(count % arrList.size())); count++; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }