zoukankan      html  css  js  c++  java
  • 继承SurfaceView实现滚动字幕组件

     和大家一起分享下代码,有问题或不好的地方请指教,不胜感激!

      1 package com.farben.ams.widget;
    2
    3 import android.content.Context;
    4 import android.graphics.Canvas;
    5 import android.graphics.Color;
    6 import android.graphics.Paint;
    7 import android.graphics.PixelFormat;
    8 import android.graphics.PorterDuff.Mode;
    9 import android.graphics.Typeface;
    10 import android.util.Log;
    11 import android.view.SurfaceHolder;
    12 import android.view.SurfaceHolder.Callback;
    13 import android.view.SurfaceView;
    14
    15 /**
    16 * 字幕区
    17 * @package com.farben.ams.widget
    18 * @author Keynes Cao
    19 * @create 2011-12-5 下午3:21:25 *
    20 */
    21 public class TextSurfaceView extends SurfaceView implements Callback, Runnable {
    22
    23 /**
    24 *是否滚动
    25 */
    26 private boolean isMove = false;
    27 /**
    28 * 移动方向
    29 */
    30 private int orientation = 1;
    31 /**
    32 * 向左移动
    33 */
    34 public final static int MOVE_LEFT = 0;
    35 /**
    36 * 向右移动
    37 */
    38 public final static int MOVE_RIGHT = 1;
    39 /**
    40 * 移动速度 1.5s 移动一次
    41 */
    42 private long speed = 1500;
    43 /**
    44 *字幕内容
    45 */
    46 private String content;
    47
    48 /**
    49 * 字幕背景色
    50 * */
    51 private String bgColor = "#E7E7E7";
    52
    53 /**
    54 * 字幕透明度 默认:60
    55 */
    56 private int bgalpha = 60;
    57
    58 /**
    59 * 字体颜色  默认:白色 (#FFFFFF)
    60 */
    61 private String fontColor = "#FFFFFF";
    62
    63 /**
    64 * 字体透明度 默认:不透明(255)
    65 */
    66 private int fontAlpha = 255;
    67
    68 /**
    69 * 字体大小  默认:20
    70 */
    71 private float fontSize = 20f;
    72 /**
    73 * 容器
    74 */
    75 private SurfaceHolder mSurfaceHolder;
    76 /**
    77 * 线程控制
    78 */
    79 private boolean loop = true;
    80 /**
    81 * 内容滚动位置起始坐标
    82 */
    83 private float x=0;
    84
    85 /**
    86 * @param context
    87 * <see>默认滚动</see>
    88 */
    89 public TextSurfaceView(Context context) {
    90 super(context);
    91 mSurfaceHolder = getHolder();
    92 mSurfaceHolder.addCallback(this);
    93 //设置画布背景不为黑色 继承Sureface时这样处理才能透明
    94 setZOrderOnTop(true);
    95 mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
    96 //背景色
    97 setBackgroundColor(Color.parseColor(bgColor));
    98 //设置透明
    99 getBackground().setAlpha(bgalpha);
    100 }
    101 /**
    102 * @param context
    103 * @param move
    104 * <see>是否滚动</see>
    105 */
    106 public TextSurfaceView(Context context,boolean move) {
    107 this(context);
    108 this.isMove = move;
    109 setLoop(isMove());
    110 }
    111 public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}
    112
    113 public void surfaceCreated(SurfaceHolder holder) {
    114
    115 Log.d("WIDTH:",""+getWidth());
    116 if(isMove){//滚动效果
    117 if(orientation == MOVE_LEFT){
    118 x = getWidth();
    119 }else{
    120 x = -(content.length()*10);
    121 }
    122 new Thread(this).start();
    123 }else{//不滚动只画一次
    124 draw();
    125 }
    126 }
    127
    128 public void surfaceDestroyed(SurfaceHolder holder) {
    129 loop = false;
    130 }
    131 /**
    132 * 画图
    133 */
    134 private void draw(){
    135 //锁定画布
    136 Canvas canvas = mSurfaceHolder.lockCanvas();
    137 if(mSurfaceHolder == null || canvas == null){
    138 return;
    139 }
    140 Paint paint = new Paint();
    141 //清屏
    142 canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR);
    143 //锯齿
    144 paint.setAntiAlias(true);
    145 //字体
    146 paint.setTypeface(Typeface.SANS_SERIF);
    147 //字体大小
    148 paint.setTextSize(fontSize);
    149 //字体颜色
    150 paint.setColor(Color.parseColor(fontColor));
    151 //字体透明度
    152 paint.setAlpha(fontAlpha);
    153 //画文字
    154 canvas.drawText(content,x,(getHeight()/2+5), paint);
    155 //解锁显示
    156 mSurfaceHolder.unlockCanvasAndPost(canvas);
    157 //滚动效果
    158 if(isMove){
    159 //内容所占像素
    160 float conlen = paint.measureText(content);
    161 //组件宽度
    162 int w = getWidth();
    163 //方向
    164 if(orientation == MOVE_LEFT){//向左
    165 if(x< -conlen){
    166 x = w;
    167 }else{
    168 x -= 2;
    169 }
    170 }else if(orientation == MOVE_RIGHT){//向右
    171 if(x >= w){
    172 x = -conlen;
    173 }else{
    174 x+=2;
    175 }
    176 }
    177 }
    178 }
    179 public void run(){
    180 while(loop){
    181 synchronized (mSurfaceHolder) {
    182 draw();
    183 }
    184 try{
    185 Thread.sleep(speed);
    186 }catch(InterruptedException ex){
    187 Log.e("TextSurfaceView",ex.getMessage()+"\n"+ex);
    188 }
    189 }
    190 content = null;
    191 }
    192 /******************************set get method***********************************/
    193
    194 private int getOrientation() {
    195 return orientation;
    196 }
    197
    198 /**
    199 * @param orientation
    200 * <li>可以选择类静态变量</li>
    201 * <li>1.MOVE_RIGHT 向右 (默认)</li>
    202 * <li>2.MOVE_LEFT 向左</li>
    203 */
    204 public void setOrientation(int orientation) {
    205 this.orientation = orientation;
    206 }
    207
    208 private long getSpeed() {
    209 return speed;
    210 }
    211
    212 /**
    213 * @param speed
    214 * <li>速度以毫秒计算两次移动之间的时间间隔</li>
    215 * <li>默认为 1500 毫秒</li>
    216 */
    217 public void setSpeed(long speed) {
    218 this.speed = speed;
    219 }
    220 public boolean isMove() {
    221 return isMove;
    222 }
    223 /**
    224 * @param isMove
    225 * <see>默认滚动</see>
    226 */
    227 public void setMove(boolean isMove) {
    228 this.isMove = isMove;
    229 }
    230 public void setLoop(boolean loop) {
    231 this.loop = loop;
    232 }
    233 public void setContent(String content) {
    234 this.content = content;
    235 }
    236 public void setBgColor(String bgColor) {
    237 this.bgColor = bgColor;
    238 }
    239 public void setBgalpha(int bgalpha) {
    240 this.bgalpha = bgalpha;
    241 }
    242 public void setFontColor(String fontColor) {
    243 this.fontColor = fontColor;
    244 }
    245 public void setFontAlpha(int fontAlpha) {
    246 this.fontAlpha = fontAlpha;
    247 }
    248 public void setFontSize(float fontSize) {
    249 this.fontSize = fontSize;
    250 }
    251
    252 }
     
  • 相关阅读:
    WordPress怎么禁用古滕堡编辑器(Gutenberg)
    .Net Framework 4.0安装cmd命令
    Windows服务器一键要打开多个软件或一键关闭多个软件
    Photosho 切换英文版教程
    解决宝塔面板无法停止Tomcat方法
    只要系统镜像就能制作可启动安装系统U盘
    [网摘]分享一段流量劫持JS代码
    Script http跳https
    linux系统 标准目录及其内容
    centos7 永久修改主机名
  • 原文地址:https://www.cnblogs.com/coacaio/p/2283993.html
Copyright © 2011-2022 走看看