android採用SurfaceView實現文字滾動效果
阿新 • • 發佈:2019-01-04
package com.ghyf.mplay.view; import java.util.ArrayList; import java.util.ListIterator; import com.ghyf.mplay.datatype.AlignMode; import com.ghyf.mplay.datatype.BackParam; import com.ghyf.mplay.datatype.FontParam; import com.ghyf.mplay.datatype.RGBA; import com.ghyf.mplay.datatype.ShowType; import com.ghyf.mplay.util.LogUtil; import com.ghyf.mplay.util.TypeFaceFactory; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.PorterDuff; import android.graphics.Rect; import android.graphics.Typeface; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; /** * * 顯示滾動文字的控制元件 * @author cuiran * @version 1.0.0 */ public class MScroll extends SurfaceView implements SurfaceHolder.Callback { private static final String TAG="MScroll"; private int module_type; // 模組型別 private int zOrder; // 模組的ZOrder private String file_version; //檔案版本 private String module_name; //控制元件名 private int module_uid;//控制元件標識 private int module_gid;//控制元件組標識 private int refresh_time; // 滾動重新整理的時間間隔 private int scroll_pixel; // 滾動重新整理的間距 private int scroll_space=200; // 滾動上一文字和下一文字間距 private boolean runFlag = true; protected SurfaceHolder holder; private Thread myThread; private int currentScrollX;// 當前滾動的位置 public int getCurrentScrollX() { return currentScrollX; } public void setCurrentScrollX(int currentScrollX) { this.currentScrollX = currentScrollX; } private String currentText = ""; private String nextText = ""; private BackParam backgroud; private ArrayList text_list;// 待顯示的文字列表 // 5 private AlignMode align;// 對齊方式 // 0 private ShowType scroll_type;// 滾動文字的顯示方式。 private FontParam font; public MScroll(Context context) { super(context); holder = this.getHolder(); holder.addCallback(this); holder.setFormat(PixelFormat.TRANSPARENT); // 頂層繪製SurfaceView設成透明 this.setZOrderOnTop(true); } boolean isAdd=true; /* 自定義執行緒 */ class MyRunnable implements Runnable { private Paint paint = null; private float textWidth; private ListIterator listIterator = null; public void run() { Canvas canvas = null; //整個空間的寬度 int mWidth=MScroll.this.getWidth();; while (runFlag) { try { if (paint == null) { initPaint(); textWidth = paint.measureText(currentText);// 得到文字的長度。 } currentScrollX -= scroll_pixel;// 滾動速度 canvas = holder.lockCanvas(new Rect(0, 0, MScroll.this .getWidth(), MScroll.this.getHeight())); // 獲取畫布 if (canvas != null) { // 退出時holder.lockCanvas()方法可能返回空,未免報空指標異常 // 清除畫布方法一 canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); canvas.drawText(currentText, 0, currentText.length(), currentScrollX, paint.getTextSize(), paint); holder.unlockCanvasAndPost(canvas);// 解鎖畫布,提交畫好的影象 } /** * 1、currentScrollX+文字的寬度+100<=mWidth */ if((currentScrollX+textWidth+scroll_space)<=mWidth){ currentText = currentText+" "+nextText; textWidth = paint.measureText(currentText);// 得到文字的長度。 findNext(); } if(isAdd){ currentScrollX = MScroll.this.getWidth(); isAdd=false; } /** * 2、滾動的文字結尾座標=寬度的一半 顯示 下一個文字 */ // if (currentScrollX <= -textWidth) { // 滾完一屏文字後接著下一個文字。 // currentScrollX = MScroll.this.getWidth(); // currentText = nextText; // textWidth = paint.measureText(currentText);// 得到文字的長度。 // findNext(); // // } Thread.sleep(getRefresh_time()); } catch (InterruptedException e) { LogUtil.e(TAG, "ScrollSurfaceView:繪製失敗...\r\n" + e); } catch (Exception e) { LogUtil.e(TAG, "ScrollSurfaceView:run...\r\n" + e); } } } /** * 初始化paint */ private void initPaint() { paint = new Paint(); listIterator = text_list.listIterator(); if (listIterator.hasNext()) { currentText = listIterator.next(); findNext(); } initFont(paint, font); } // 找到下一個文字。 void findNext() { if (listIterator.hasNext()) { nextText = listIterator.next(); } else { // 文字集合播完了就再播第一項。 isAdd=true; while (listIterator.hasPrevious()) { listIterator.previous(); } nextText = listIterator.next(); } } /** * 設定TextView的字型 * * @param paint * textview控制元件 * @param font * 字型 */ void initFont(Paint paint, FontParam font) { // paint.setTextAlign(Align.RIGHT); // 大小 paint.setTextSize(font.getSize()); // 字型 Typeface typeface = TypeFaceFactory.createTypefae(font.getName()); paint.setTypeface(typeface); // 顏色 RGBA rgba = font.getFaceColor(); int color = Color.argb(rgba.getAlpha(), rgba.getRed(), rgba.getGreen(), rgba.getBlue()); paint.setColor(color); // TODO 其它字型引數的設定 } } @Override /** * 當控制元件建立時自動執行的方法 */ public void surfaceCreated(SurfaceHolder holder) { // 啟動自定義執行緒 myThread = new Thread(new MyRunnable()); runFlag = true; myThread.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override /** * 當控制元件銷燬時自動執行的方法 */ public void surfaceDestroyed(SurfaceHolder holder) { // 終止自定義執行緒 runFlag = false; myThread.interrupt(); } public int getScroll_pixel() { return scroll_pixel; } public void setScroll_pixel(int scroll_pixel) { this.scroll_pixel = scroll_pixel; } public String getText() { return currentText; } public void setText(String text) { this.currentText = text; } public BackParam getBackgroud() { return backgroud; } public void setBackgroud(BackParam backgroud) { this.backgroud = backgroud; } public AlignMode getAlign() { return align; } public void setAlign(AlignMode align) { this.align = align; } public ShowType getScroll_type() { return scroll_type; } public void setScroll_type(ShowType scroll_type) { this.scroll_type = scroll_type; } public FontParam getFont() { return font; } public void setFont(FontParam font) { this.font = font; } public long getRefresh_time() { return refresh_time; } public void setRefresh_time(int refresh_time) { this.refresh_time = refresh_time; } public ArrayList getText_list() { return text_list; } public void setText_list(ArrayList text_list) { this.text_list = text_list; } public int getModule_type() { return module_type; } public void setModule_type(int module_type) { this.module_type = module_type; } public int getzOrder() { return zOrder; } public void setzOrder(int zOrder) { this.zOrder = zOrder; } public String getFile_version() { return file_version; } public void setFile_version(String file_version) { this.file_version = file_version; } public String getModule_name() { return module_name; } public void setModule_name(String module_name) { this.module_name = module_name; } public int getModule_uid() { return module_uid; } public void setModule_uid(int module_uid) { this.module_uid = module_uid; } public int getModule_gid() { return module_gid; } public void setModule_gid(int module_gid) { this.module_gid = module_gid; } }