package com.goach.lib;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class VerticalScrollTextView extends TextView implements Runnable{
//绘制歌词画笔
private Paint mContentPaint;
//绘制基线画笔
private Paint mLinePaint;
//绘制滑动进度背景画笔
private Paint mRectPaint;
//歌词数据
private List<Sentence> mDataList;
//行数
private int index = 0 ;
//当前view的宽
private float mX;
//当前view的高
private float mY;
//当前view垂直方向中线
private float middleY;
//行与行之间的间距
private final static int DY = 80 ;
//歌词文字大小
private int mTextSize = 35;
//歌词中间字体的大小
private int mBigTextSize = 45;
//当前是否按下
private boolean isTouch = false ;
//上一次触摸view的y轴坐标
private float mLastY;
//是否正在滑动
private boolean isMoving;
//记录上一次滑动的时间
private long lastMoveTime;
//滑动速度追踪类
private VelocityTracker mVelocityTracker;
//滑动最大速度
private int mMaximumVelocity;
//歌词是否为空
private boolean isEmpty;
public VerticalScrollTextView(Context context) {
this(context,null);
}
public VerticalScrollTextView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public VerticalScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取最大的滑动速度值
mMaximumVelocity = ViewConfiguration.get(context)
.getScaledMaximumFlingVelocity();
init();
}
private void init(){
setFocusable(true);
setClickable(true);
//歌词为空设置默认值
if(mDataList==null){
mDataList = new ArrayList<>();
Sentence sentence = new Sentence(0,"没有获取到歌词",0);
mDataList.add(sentence);
isEmpty = true ;
}
//初始化歌词画笔
mContentPaint = new Paint();
mContentPaint.setTextSize(mTextSize);
mContentPaint.setAntiAlias(true);
mContentPaint.setColor(Color.parseColor("#e5e2e2"));
//设置为serif字体
mContentPaint.setTypeface(Typeface.SERIF);
//设置字体为居中
mContentPaint.setTextAlign(Paint.Align.CENTER);
//初始化基线画笔
mLinePaint = new Paint();
mLinePaint.setAntiAlias(true);
mLinePaint.setStrokeWidth(1);
mLinePaint.setColor(Color.WHITE);
//进度背景颜色画笔
mRectPaint = new Paint();
mLinePaint.setAntiAlias(true);
mRectPaint.setColor(Color.parseColor("#66666666"));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//如果当前进度为-1,直接返回,不用绘制
if (index == -1)
return;
Sentence sentence = mDataList.get(index);
//绘制中间行的歌词,设置为高亮白色,大字体
mContentPaint.setColor(Color.WHITE);
mContentPaint.setTextSize(mBigTextSize);
canvas.drawText(sentence.getName(), mX/2, middleY, mContentPaint);
//当前为歌词不为空并且按下的情况下,绘制基线和进度
if(!isEmpty&&isTouch){
//获取中间行字体最高的位置
float baseLine = middleY-Math.abs(mContentPaint.ascent());
//绘制进度背景
canvas.drawRect(10.0f,baseLine-70,150.0f,baseLine,mRectPaint);
//绘制基线
canvas.drawLine(10.0f,baseLine,mX-10,baseLine,mLinePaint);
//设置进度字体大小
mContentPaint.setTextSize(mTextSize);
//绘制进度字体
canvas.drawText(String.valueOf(index),85,baseLine-35,mContentPaint);
}
//初始化isEmpty
isEmpty = false ;
//初始化歌词内容画笔
mContentPaint.setColor(Color.parseColor("#e5e2e2"));
mContentPaint.setTextSize(mTextSize);
//暂时保存中间线位置,来绘制中间线以上的行数字体
float tempY = middleY;
//绘制中间线以上的歌词
for (int i = index - 1; i >= 0; i--) {
tempY = tempY - DY;
if (tempY < 0) {
break;
}
Sentence preSentence = mDataList.get(i);
canvas.drawText(preSentence.getName(), mX/2, tempY, mContentPaint);
}
tempY = middleY;
//绘制中间线以下的歌词
for (int i = index + 1; i < mDataList.size(); i++) {
tempY = tempY + DY;
if (tempY > mY) {
break;
}
Sentence nexeSentence = mDataList.get(i);
canvas.drawText(nexeSentence.getName(), mX/2, tempY, mContentPaint);
}
//初始化isMoving,到这里表示滑动结束
isMoving = false ;
}
protected void onSizeChanged(int w, int h, int ow, int oh) {
super.onSizeChanged(w, h, ow, oh);
//获取view的宽和高
mX = w;
mY = h;
middleY = h * 0.5f;
}
public long updateIndex(int index) {
if (index == -1)
return -1;
this.index=index;
return index;
}
public List<Sentence> getDataList() {
return mDataList;
}
public void setDataList(List<Sentence> mDataList){
this.mDataList = mDataList ;
}
public void updateUI(){
new Thread(this).start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
isTouch =true;
mLastY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
//创建速度追踪器
initVelocityTrackerIfNotExists();
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
//获取当前速度。默认为100
float velocity = mVelocityTracker.getYVelocity()==0?100:mVelocityTracker.getYVelocity();
long currentTime = System.currentTimeMillis();
//设置一个固定值和速度结合决定滑动更新的快慢
if(!isMoving&¤tTime-lastMoveTime>20000/Math.abs(velocity)){
isMoving = true ;
lastMoveTime = System.currentTimeMillis();
float currentY = event.getY();
float mMoveY = currentY - mLastY;
//向上滑动-1向下滑动+1
int newIndex = mMoveY>0?index - 1:index+1;
//循环滚动
newIndex=newIndex<0?mDataList.size()-1:newIndex>=mDataList.size()?0:newIndex;
updateIndex(newIndex);
invalidate();
mLastY = currentY;
}
break;
case MotionEvent.ACTION_UP:
isTouch = false ;
recycleVelocityTracker();
break;
}
return super.onTouchEvent(event);
}
@Override
public void run() {
//自�
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ScrollerTextView.zip (59个子文件)
ScrollerTextView
gradlew.bat 2KB
gradlew 5KB
gradle.properties 872B
ScrollerTextView.iml 947B
.gradle
2.10
tasks
_app_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 19KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.lock 17B
localClassSetAnalysis.bin 29KB
taskArtifacts
fileHashes.bin 245KB
cache.properties.lock 17B
taskArtifacts.bin 103KB
fileSnapshots.bin 2.11MB
cache.properties 31B
outputFileStates.bin 22KB
app
proguard-rules.pro 657B
src
test
java
com
goach
lib
ExampleUnitTest.java 320B
androidTest
java
com
goach
lib
ApplicationTest.java 356B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
drawable-xhdpi
ic_launcher.png 5KB
bg.jpg 239KB
drawable
mipmap-hdpi
ic_launcher.png 3KB
values
colors.xml 214B
strings.xml 82B
styles.xml 394B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
layout
activity_main.xml 449B
values-w820dp
dimens.xml 364B
java
com
goach
lib
VerticalScrollTextView.java 9KB
VerticalScrollTextActivity.java 2KB
Sentence.java 739B
AndroidManifest.xml 686B
app.iml 10KB
.gitignore 8B
build.gradle 640B
libs
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 231B
.idea
gradle.xml 739B
runConfigurations.xml 564B
misc.xml 3KB
.name 16B
copyright
profiles_settings.xml 76B
modules.xml 369B
encodings.xml 159B
compiler.xml 686B
workspace.xml 146KB
libraries
appcompat_v7_23_3_0.xml 756B
support_vector_drawable_23_3_0.xml 635B
support_annotations_23_3_0.xml 485B
hamcrest_core_1_3.xml 526B
junit_4_12.xml 477B
support_v4_23_3_0.xml 898B
animated_vector_drawable_23_3_0.xml 640B
.gitignore 105B
local.properties 423B
settings.gradle 16B
build.gradle 521B
共 59 条
- 1
资源评论
- qq_292541632018-06-29不错,和简书的那个结合起来就更好了
iGoach
- 粉丝: 99
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- HCIP-Datacom笔记 (1).pdf
- yolov5,SSD 可能使用到的一些代码
- bbbbbbbbbbbbbbbbbb
- 安卓逆向学习笔记之Frida Stalker 还原OLLVM AES.docx
- 安卓逆向学习笔记之unicorn来trace还原OLLVM Base64.docx
- 最新版本私钥助记词碰撞器大富豪使用python进行制作通过接口的方式进行验证支持多币种多链多网络一分钟万次验证高出货率
- 介绍离散性制造行业的MES系统流程
- Arduino IDE压缩包版本,2024年4月26日,最新版本
- 基于IDEA-CCNL/Randeng-Pegasus-238M-Summary-Chines微调的中文文本摘要任务源码+数据集
- 自动驾驶-状态估计和定位之直方图滤波(Histogram+Filter)定位应用和源码.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功