Android 自定义UI-垂直方向的SeekBar

简介:

 系统自带的SeekBar样式是水平的,如果需求一个垂直方向的效果就需要自定义了。原理很简单,即定义一个类继承于SeekBar,并在OnDraw方法里面旋转一下视图。

代码如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package android.widget;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.util.AttributeSet;  
  6. import android.util.Log;  
  7. import android.view.MotionEvent;  
  8.   
  9. public class VerticalSeekBar extends SeekBar {  
  10.   
  11.     public VerticalSeekBar(Context context) {  
  12.         super(context);  
  13.     }  
  14.   
  15.     public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {  
  16.         super(context, attrs, defStyle);  
  17.     }  
  18.   
  19.     public VerticalSeekBar(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.     }  
  22.   
  23.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  24.         super.onSizeChanged(h, w, oldh, oldw);  
  25.     }  
  26.   
  27.     @Override  
  28.     protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  29.         super.onMeasure(heightMeasureSpec, widthMeasureSpec);  
  30.         setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());  
  31.     }  
  32.   
  33.     protected void onDraw(Canvas c) {  
  34.         //将SeekBar转转90度  
  35.         c.rotate(-90);  
  36.         //将旋转后的视图移动回来  
  37.         c.translate(-getHeight(),0);  
  38.         Log.i("getHeight()",getHeight()+"");  
  39.         super.onDraw(c);  
  40.     }  
  41.   
  42.     @Override  
  43.     public boolean onTouchEvent(MotionEvent event) {  
  44.         if (!isEnabled()) {  
  45.             return false;  
  46.         }  
  47.   
  48.         switch (event.getAction()) {  
  49.             case MotionEvent.ACTION_DOWN:  
  50.             case MotionEvent.ACTION_MOVE:  
  51.             case MotionEvent.ACTION_UP:  
  52.                 int i=0;  
  53.                 //获取滑动的距离  
  54.                 i=getMax() - (int) (getMax() * event.getY() / getHeight());  
  55.                 //设置进度  
  56.                 setProgress(i);  
  57.                 Log.i("Progress",getProgress()+"");  
  58.                 //每次拖动SeekBar都会调用  
  59.                 onSizeChanged(getWidth(), getHeight(), 00);  
  60.                 Log.i("getWidth()",getWidth()+"");  
  61.                 Log.i("getHeight()",getHeight()+"");  
  62.                 break;  
  63.   
  64.             case MotionEvent.ACTION_CANCEL:  
  65.                 break;  
  66.         }  
  67.         return true;  
  68.     }  
  69.       
  70. }  
  xml布局文件:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:gravity="center"  
  6.     android:background="@android:color/white"  
  7.     android:orientation="horizontal" >  
  8.   
  9.     <android.widget.VerticalSeekBar_Reverse  
  10.         android:id="@+id/seekbar_reverse"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="450dip"  
  13.         android:layout_marginRight="30dip" />  
  14.   
  15.     <TextView  
  16.         android:id="@+id/reverse_sb_progresstext"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_below="@+id/seekbar_reverse"  
  20.         android:gravity="center" />  
  21.   
  22.     <android.widget.VerticalSeekBar  
  23.         android:id="@+id/vertical_Seekbar"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="450dip"  
  26.         android:layout_toRightOf="@+id/seekbar_reverse" />  
  27.   
  28.     <TextView  
  29.         android:id="@+id/vertical_sb_progresstext"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:layout_below="@+id/vertical_Seekbar"  
  33.         android:layout_toRightOf="@+id/seekbar_reverse"  
  34.         android:gravity="center" />  
  35.   
  36. </RelativeLayout>  
  
  代码下载
  推荐博文: Android Canvas编程:对rotate()和translate()两个方法的研究
相关文章
|
16天前
|
消息中间件 安全 数据处理
Android为什么不能在子线程更新UI
Android为什么不能在子线程更新UI
23 0
|
16天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
48 1
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
41 1
|
3月前
|
开发工具 Android开发 开发者
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
31 4
|
3月前
|
API Android开发 开发者
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
31 2
|
3月前
|
数据可视化 Android开发 容器
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
92 5
|
5天前
|
编解码 Android开发 UED
安卓UI/UX设计原则:打造引人入胜的用户体验
【4月更文挑战第13天】本文探讨了安卓UI/UX设计的关键原则,包括一致性、简洁性、反馈、清晰性、效率和适应性。一致性要求视觉和行为保持一致,利用系统UI;简洁性减少用户行动,简化导航;反馈需即时且明确;清晰性强调表达清晰,布局有序;效率关注性能优化和任务简化;适应性涉及多设备适配和用户多样性。遵循这些原则,可创建出色应用,提供无缝用户体验。设计应持续迭代,适应技术发展和用户需求。
|
11天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
20天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
1月前
|
XML API Android开发
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
26 4