当前位置: 首页 > news >正文

android 快速实现 垂直SeekBar(VerticalSeekBar)

1.话不多说上源码:

package com.example.widget;import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;/*** Class to create a vertical slider*/
public class VerticalSeekBar extends androidx.appcompat.widget.AppCompatSeekBar {public VerticalSeekBar(Context context) {super(context);}public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public VerticalSeekBar(Context context, AttributeSet attrs) {super(context, attrs);}protected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(h, w, oldh, oldw);}@Overrideprotected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(heightMeasureSpec, widthMeasureSpec);setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());}protected void onDraw(Canvas c) {c.rotate(-90);c.translate(-getHeight(), 0);super.onDraw(c);}@Overridepublic boolean onTouchEvent(MotionEvent event) {if (!isEnabled()) {return false;}switch (event.getAction()) {case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_MOVE:case MotionEvent.ACTION_UP:setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));onSizeChanged(getWidth(), getHeight(), 0, 0);break;case MotionEvent.ACTION_CANCEL:break;}return true;}
}

2.布局中使用:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/darker_gray"><com.example.widget.VerticalSeekBarandroid:id="@+id/sb_volume"android:layout_width="40dp"android:layout_height="180dp"/>
</FrameLayout>

http://www.lryc.cn/news/312695.html

相关文章:

  • 算法刷题day23:双指针
  • 学术论文GPT的源码解读与二次开发:从ChatPaper到gpt_academic
  • 报表生成器FastReport .Net用户指南:表达式(下)
  • JavaScript极速入门(1)
  • 鸿蒙Harmony应用开发—ArkTS声明式开发(通用属性:浮层)
  • Meta AI移动设备上部署LLM的新框架MobileLLM
  • 使用Tesseract-OCR对PDF等图片文件进行文字识别
  • 部署YOLOv8模型的实用常见场景
  • SpringBoot缓存
  • STC89C52串口通信详解
  • 基础算法|线性结构|前缀和学习
  • 设计模式之模版方法实践
  • sql中COALESCE函数详解
  • rust-analyzer报错“Failed to spawn one or more proc-macro servers,....“怎么解决?
  • Communications--9--一文读懂双机热备冗余原理
  • 可调恒定电流稳压器NSI50150ADT4G车规级LED驱动器 提供专业的汽车级照明解决方案
  • Unity中使用代码动态修改URP管线下的标准材质是否透明
  • 关于制作Python游戏全过程(汇总1)
  • 独立站营销新纪元:AI与大数据塑造个性化体验的未来
  • C语言项目实战——贪吃蛇
  • ArmSoM规划开发基于RK3576的开发套件
  • 视频剪辑如何提取伴奏?短视频剪辑有妙方
  • 【Web】浅浅地聊SnakeYaml反序列化两条常见利用链
  • 详解openGauss客户端工具gsql的高级用法
  • 开源工业软件:SCADA系统开源
  • 关于AI彩票预测算法的设想
  • 设计模式之策略模式实践
  • 讨论:解决哈希冲突的几种方法
  • 遥感分析时什么情况下需要做大气校正?
  • 设计模式学习笔记 - 设计原则 - 7.DRY 原则及提高代码复用性