自定义view实现一个游标效果(增强)

2024-05-27 20:38

本文主要是介绍自定义view实现一个游标效果(增强),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

继上一篇:自定义view实现一个游标效果,在此基础上新增支持功能:

当手指点击某个时间域时,也可以触发游标滑动到当前点击选择的时间域。


下面贴代码:

package com.baicells.omcserver.view;import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;import com.baicells.omcserver.R;import java.util.ArrayList;public class CursorView extends View {private int width;private int height;private int radius;private int strokeWidth;private int curTextColor;private int inCurTextColor;private ArrayList<String> cursorText;private Paint borderPaint;private Paint innerBgPaint;private Paint textPaint;private int current;private float offsetX;private RectF currentRectF;private boolean isInCurrent;private GestureDetector mDetector;private OnItemSelectListener mListener;public CursorView(Context context) {super(context);}public CursorView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);mDetector = new GestureDetector(context, mGestureListener);borderPaint = new Paint();borderPaint.setAntiAlias(true);borderPaint.setStyle(Paint.Style.FILL.STROKE);innerBgPaint = new Paint();innerBgPaint.setAntiAlias(true);innerBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);textPaint = new Paint();textPaint.setAntiAlias(true);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CursorView);int indexCount = typedArray.getIndexCount();for (int i = 0; i < indexCount; i++) {int index = typedArray.getIndex(i);switch (index) {case R.styleable.CursorView_current_bg_color:innerBgPaint.setColor(typedArray.getColor(index, Color.GREEN));break;case R.styleable.CursorView_current_text_color:curTextColor = typedArray.getColor(index, Color.WHITE);break;case R.styleable.CursorView_incurrent_text_color:inCurTextColor = typedArray.getColor(index, Color.BLACK);break;case R.styleable.CursorView_stroke_color:borderPaint.setColor(typedArray.getColor(index, Color.RED));break;case R.styleable.CursorView_textsize:textPaint.setTextSize(typedArray.getDimensionPixelSize(index, 20));break;case R.styleable.CursorView_stroke_width:strokeWidth = typedArray.getDimensionPixelSize(index, 3);break;}}typedArray.recycle();borderPaint.setStrokeWidth(strokeWidth);setLayerType(LAYER_TYPE_SOFTWARE, null);}public CursorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public void setOnItemSelectListener(OnItemSelectListener listener) {this.mListener = listener;}public void setCursorText(ArrayList<String> cursorText) {this.cursorText = cursorText;invalidate();}public void setCurrent(int current) {this.current = current;invalidate();}public int getCurrent() {return current;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);int modeWidth = MeasureSpec.getMode(widthMeasureSpec);int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);int modeHeight = MeasureSpec.getMode(heightMeasureSpec);width = 300;height = 20;if (modeWidth == MeasureSpec.EXACTLY) {width = sizeWidth;}if (modeHeight == MeasureSpec.EXACTLY) {height = sizeHeight;}setMeasuredDimension(width, height);radius = height / 2;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//画整个大背景RectF rect = new RectF(strokeWidth / 2, strokeWidth / 2, width - strokeWidth / 2, height - strokeWidth / 2);canvas.drawRoundRect(rect, radius, radius, borderPaint);if (cursorText != null) {int size = cursorText.size();float everyWidth = width * 1.0f / size;//画当前选中时间维度的背景RectF currBgRectF = getCurrBgRectF(everyWidth);currentRectF = currBgRectF;canvas.drawRoundRect(currBgRectF, radius, radius, innerBgPaint);//循环画时间维度文本for (int i = 0; i < size; i++) {textPaint.setShader(null);String text = cursorText.get(i);Paint.FontMetricsInt metricsInt = textPaint.getFontMetricsInt();float textwidth = textPaint.measureText(text);float preblank = (everyWidth - textwidth) / 2.0f;float x = everyWidth * i + preblank;float y = (height - metricsInt.bottom + metricsInt.top) / 2.0f - metricsInt.top;if (currBgRectF.left >= x && currBgRectF.left <= x + textwidth) {float split = (currBgRectF.left - x) / textwidth;LinearGradient shader = new LinearGradient(x, 0, x + textwidth,0, new int[]{inCurTextColor, curTextColor}, new float[]{split, split + 0.01f}, Shader.TileMode.CLAMP);textPaint.setShader(shader);}if (currBgRectF.right >= x && currBgRectF.right <= x + textwidth) {float split = (currBgRectF.right - x) / textwidth;LinearGradient shader = new LinearGradient(x, 0, x + textwidth,0, new int[]{curTextColor, inCurTextColor}, new float[]{split, split + 0.01f}, Shader.TileMode.CLAMP);textPaint.setShader(shader);}if (currBgRectF.left < x && currBgRectF.right > x + textwidth) {textPaint.setColor(curTextColor);} else {textPaint.setColor(inCurTextColor);}canvas.drawText(text, x, y, textPaint);}}}private RectF getCurrBgRectF(float everyWidth) {float left = everyWidth * current + offsetX;float right = everyWidth * (current + 1) + offsetX;if (left < 0) {left = 0;right = everyWidth;}if (right > width) {left = width - everyWidth;right = width;}return new RectF(left, 0, right, height);}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {ViewParent parent = null;while (true) {if (parent != null && parent instanceof ViewPager) {break;}if (parent != null) {parent = parent.getParent();} else {parent = getParent();}}switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_MOVE:case MotionEvent.ACTION_UP:parent.requestDisallowInterceptTouchEvent(true);break;default:parent.requestDisallowInterceptTouchEvent(false);break;}return super.dispatchTouchEvent(ev);}private float downX = 0;private float downY = 0;@Overridepublic boolean onTouchEvent(MotionEvent event) {float x = event.getX();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:downX = event.getX();downY = event.getY();isInCurrent = currentRectF.contains(downX, downY);break;case MotionEvent.ACTION_MOVE:if (isInCurrent) {offsetX = x - downX;invalidate();}break;case MotionEvent.ACTION_UP:if (isInCurrent) {final NextCurrent nextCurrent = computeScrollX(currentRectF.left);final float lastX = offsetX;ValueAnimator anim = ValueAnimator.ofFloat(0, nextCurrent.getX());anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float value = (float) animation.getAnimatedValue();offsetX = lastX + value;invalidate();float fraction = animation.getAnimatedFraction();if (fraction == 1.0f) {current = nextCurrent.getIndex();offsetX = 0;if (mListener != null) {mListener.onItemSelect(nextCurrent.getIndex());}}}});anim.setDuration(100);anim.start();}break;}mDetector.onTouchEvent(event);return true;}private NextCurrent getNeedScroll(float pointX) {if (cursorText != null) {NextCurrent next = new NextCurrent();int size = cursorText.size();float everyWidth = width * 1.0f / size;int index = 0;for (int i = 0; i < size; i++) {if (pointX > everyWidth * i && pointX < everyWidth * (i + 1)) {index = i;break;}}next.setIndex(index);next.setX(everyWidth * index - currentRectF.left);return next;}return null;}private NextCurrent computeScrollX(float left) {NextCurrent next = new NextCurrent();int index = 0;if (cursorText != null) {int size = cursorText.size();float everyWidth = width * 1.0f / size;float min = everyWidth + 1;for (int i = 0; i < size; i++) {float abs = Math.abs(left - everyWidth * i);if (abs < min) {min = abs;index = i;}}if (everyWidth * index > left) {next.setX(min);} else {next.setX(-min);}next.setIndex(index);}return next;}private GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {@Overridepublic boolean onSingleTapConfirmed(MotionEvent e) {if (!isInCurrent) {final NextCurrent needScroll = getNeedScroll(e.getX());ValueAnimator anim = ValueAnimator.ofFloat(0, needScroll.getX());anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {offsetX = (float) animation.getAnimatedValue();invalidate();float fraction = animation.getAnimatedFraction();if (fraction == 1.0f) {current = needScroll.getIndex();offsetX = 0;if (mListener != null) {mListener.onItemSelect(current);
}}}});anim.setDuration(300);anim.start();}return super.onSingleTapConfirmed(e);}};private class NextCurrent {private int index;private float x;public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}public float getX() {return x;}public void setX(float x) {this.x = x;}}public interface OnItemSelectListener {void onItemSelect(int item);}
}



这篇关于自定义view实现一个游标效果(增强)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1008539

相关文章

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU