滚动字幕(水平,垂直)
来源:互联网 发布:修改linux ip地址 编辑:程序博客网 时间:2024/05/17 16:12
水平,垂直的滚动字幕:(可设置属性:点击暂停,水平垂直,速度,次数,文字内容,文字颜色,文字大小等。。。)
注:
图片中黑色部分必须加上,红色部分换成项目的包名。
最重要的滚动类:ScrollTextView:
package com.example.test; import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.FontMetrics;import android.graphics.PixelFormat;import android.graphics.PorterDuff.Mode;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;import com.example.scrolltextview.R; public class ScrollTextView extends SurfaceView implements SurfaceHolder.Callback { private final String TAG = "ScrollTextView"; private SurfaceHolder holder; private Paint paint = null;// 画笔 private boolean bStop = false; // 停止滚动 private boolean clickEnable = false; // 可以点击 private boolean isHorizontal = true; // 水平|垂直 private int speed = 1; // 滚动速度 private String text = "";// 文本内容 private float textSize = 15f; // 字号 private int textColor = Color.BLACK; // 文字颜色 private int times = Integer.MAX_VALUE; // 滚动次数 private int viewWidth = 0;// 控件的长度 private int viewHeight = 0; // 控件的高度 private float textWidth = 0f;// 水平滚动时的文本长度 private float textHeight = 0f; // 垂直滚动时的文本高度 private float textX = 0f;// 文字的横坐标 private float textY = 0f;// 文字的纵坐标 private float viewWidth_plus_textLength = 0.0f;// 显示总长度 private int time = 0; // 已滚动次数 private ScheduledExecutorService scheduledExecutorService; // 执行滚动线程 public ScrollTextView(Context context) { super(context); } public ScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); holder = this.getHolder(); holder.addCallback(this); paint = new Paint(); TypedArray arr = getContext().obtainStyledAttributes(attrs, R.styleable.scroll); clickEnable = arr.getBoolean(R.styleable.scroll_clickEnable, clickEnable); isHorizontal = arr.getBoolean(R.styleable.scroll_isHorizontal, isHorizontal); speed = arr.getInteger(R.styleable.scroll_speed, speed); text = arr.getString(R.styleable.scroll_text); textColor = arr.getColor(R.styleable.scroll_textColor, textColor); textSize = arr.getDimension(R.styleable.scroll_textSize, textSize); times = arr.getInteger(R.styleable.scroll_times, times); time = times; paint.setColor(textColor); paint.setTextSize(textSize); /* * 下面两行代码配合draw()方法中的canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR); * 将画布填充为透明 */ setZOrderOnTop(true); getHolder().setFormat(PixelFormat.TRANSLUCENT); setFocusable(true); // 设置焦点 } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); viewWidth = MeasureSpec.getSize(widthMeasureSpec); viewHeight = MeasureSpec.getSize(heightMeasureSpec); if (isHorizontal) { // 水平滚动 textWidth = paint.measureText(text);// measure()方法获取text的长度 viewWidth_plus_textLength = viewWidth + textWidth; textY = (viewHeight - getFontHeight(textSize)) / 2 + getPaddingTop() - getPaddingBottom(); } else { // 垂直滚动 textHeight = getFontHeight(textSize) * text.length(); viewWidth_plus_textLength = viewHeight + textHeight; textX = (viewWidth - textSize) / 2 + getPaddingLeft() - getPaddingRight(); } } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override public void surfaceCreated(SurfaceHolder holder) { bStop = false; scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); scheduledExecutorService.scheduleAtFixedRate(new ScrollThread(), 1000, 10, TimeUnit.MILLISECONDS); Log.d(TAG, "ScrollTextView is created"); } @Override public void surfaceDestroyed(SurfaceHolder arg0) { // TODO Auto-generated method stub bStop = true; scheduledExecutorService.shutdown(); Log.d(TAG, "ScrollTextView is destroyed"); } // 获取字体高度 public int getFontHeight(float fontSize) { Paint paint = new Paint(); paint.setTextSize(fontSize); FontMetrics fm = paint.getFontMetrics(); return (int) Math.ceil(fm.descent - fm.ascent); } public void setTimes(int times) { if(times <= 0){ this.times = Integer.MAX_VALUE; }else{ this.times = times; time = times; } } public void setText(String text) { this.text = text; } public void setSpeed(int speed){ if(speed > 10 || speed < 0){ throw new IllegalArgumentException("Speed was invalid integer, it must between 0 and 10"); }else{ this.speed = speed; } } /** * 当屏幕被触摸时调用 */ @Override public boolean onTouchEvent(MotionEvent event) { if (!clickEnable) { return true; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: bStop = !bStop; if (!bStop && time == 0) { time = times; } break; } return true; } public synchronized void draw(float X, float Y) { Canvas canvas = holder.lockCanvas(); canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);// 通过清屏把画布填充为透明 if (isHorizontal) { // 水平滚动 canvas.drawText(text, X, Y, paint); } else { // 垂直滚动 for (int i = 0; i < text.length(); i++) { canvas.drawText(text.charAt(i) + "", X, Y + (i + 1) * getFontHeight(textSize), paint); } } holder.unlockCanvasAndPost(canvas); } class ScrollThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub while (!bStop) { if (isHorizontal) { draw(viewWidth - textX, textY); textX += speed;// 速度设置:1-10 if (textX > viewWidth_plus_textLength) { textX = 0; --time; } } else { draw(textX, viewHeight - textY); textY += speed; if (textY > viewWidth_plus_textLength) { textY = 0; --time; } } if (time <= 0) { bStop = true; } } } }}界面布局:activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:start="http://schemas.android.com/apk/res/com.example.scrolltextview" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.test.ScrollTextView android:id="@+id/hor" android:layout_width="match_parent" android:layout_height="100dp" android:layout_alignParentBottom="true" android:layout_marginBottom="-40dp" android:background="#000000" start:clickEnable="true" start:isHorizontal="true" start:speed="1" start:text="paulpaulpaul" start:textColor="#ffffff" start:textSize="30sp" start:times="3" /> <com.example.test.ScrollTextView android:layout_width="100dp" android:layout_height="match_parent" android:layout_above="@id/hor" android:layout_centerHorizontal="true" android:background="#ffffff" start:clickEnable="false" start:isHorizontal="false" start:speed="3" start:text="王鹏程王鹏程王鹏程" start:textColor="#000000" start:textSize="30sp" /></RelativeLayout>属性配置:attr.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="scroll"> <attr name="clickEnable" format="boolean" /> <attr name="isHorizontal" format="boolean" /> <attr name="speed" format="integer" /> <attr name="text" format="string" /> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="times" format="integer" /> </declare-styleable></resources>
demo源码下载地址:http://download.csdn.net/detail/u012604745/9414610点击打开链接
0 0
- 滚动字幕(水平,垂直)
- 水平滚动字幕的实现
- Qt 水平滚动字幕2
- [转载]Qt 水平滚动字幕
- 垂直水平滚动条
- 水平/垂直滚动联动
- marquee插件水平滚动字幕循环滚动
- javascript 水平文字垂直滚动
- 垂直、水平滚动的TextView
- android TextView 垂直自动滚动字幕实现
- android TextView 垂直自动滚动字幕实现
- android TextView 垂直自动滚动字幕实现
- Android 天气预报图文字幕垂直滚动效果
- Android 天气预报图文字幕垂直滚动效果
- android TextView 垂直自动滚动字幕实现
- Android 天气预报图文字幕垂直滚动效果
- Android 天气预报图文字幕垂直滚动效果
- UI-Label自动垂直滚动字幕
- Android AsyncTask详解,你真的了解AsyncTask?
- android源码中打包so到apk的方法
- vsftpd 虚拟用户 530 Login incorrect 错误
- 网站压力测试工具Webbench部分源码分析
- 最大公约数与最小公倍数(c语言)
- 滚动字幕(水平,垂直)
- 建造者模式(Builder Pattern)
- h5中 <!DOCTYPE html>标签问题,div里面如果装有一个img下边会有2px间隙
- MFC启动过程&单文档MFC程序的启动详细过程
- Android HandlerThread 完全解析
- 矩阵形象理解最小二乘解
- channel --通道
- RSA加密解密
- poj 3281 Dining(最大流,两种资源一种群体建图)