本文主要是介绍android 自定义速度表,自定义View-实现简易车速器(真的够简易),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
自定义View-实现简易车速器(真的够简易)
学习自定义View挺久了,好久没用都快忘了,这里实现一个简易的车速器算是一个回顾,项目比较简单,代码较少,但自定义View的流程基本都涉及到了.本文不是一篇讲解自定义View基础的文章,而是一个小的实战,如果想看讲解自定义View的文章,强烈推荐博客:http://blog.csdn.net/aigestudio,强烈=_=.
效果如下图:
接下来是代码部分:
attrs:
attrs文件包含自定义的属性,可以在布局文件或者代码中使用.
colors:
#3F51B5
#303F9F
#FF4081
@android:color/holo_red_dark
@android:color/holo_red_dark
@android:color/white
@android:color/holo_red_dark
@android:color/white
布局文件:
xmlns:tools="http://schemas.android.com/tools"
xmlns:SpeedometerView="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
tools:context="com.example.why.speedometerview.MainActivity">
android:background="@android:color/black"
android:id="@+id/speedometer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
SpeedometerView:outCircleWidth="5dp"
/>
接下来是Java代码:
SpeedometerView:
/**
* Created by why on 17-3-9.
* @author why
* 简易View实现类
*/
public class SpeedometerView extends View {
private static final String TAG = "SpeedometerView";
private Paint mInnerArcPaint; // 内部弧线画笔
private Paint mOutArcPaint; // 外部弧线画笔
private Paint mNotFullTenIndicatorPaint; // 非整10刻度画笔
private Paint mFullTenIndicatorPaint; // 整10加粗刻度画笔
private Paint mNumberPaint; // 数字画笔
private Paint mIndicatorPathPaint; // 刻度指针画笔,计可旋转的线条
private int mInnerArcLineWidth; // 内部弧线线条宽度
private int mOutArcLineWidth; // 外部弧线线条宽度
private int mNormalIndicatorWidth; // 刻度线条宽度
private int mFullTenScaleWidth; // 整10加粗刻度线条宽度
private int mFullTenScaleLen ; // 非整10加粗线条长度
private int mNotFullScaleLen; // 白色刻度线条长度
private int mNumberWidth; // 数字线条刻度
private int mIndicatorPathLineWidth;//
private float mNumberTextSize; // 数字字体大小
private int mCenterCircleRadius; // 内圆半径
private int mOutCircleRadius; // 外圆半径
private RectF mOutRectF; // 外弧线的基准矩形
private RectF mCenterRectF; //内弧线基准矩形
// 这两个值是当view的width和height属性为wrap_content时,属性不起作用配置的默认值,详情间onMeasure();
private int mViewDefaultWidth; // 默认的view的宽度
private int mViewDefaultHeight; // 默认的view的高度,
private int[] mWindowSize = new int[2]; // 保存屏幕的宽/高
private Path mIndicatorPath; // 刻度指针,用path实现
private int mScaleIndicatorLen; // 刻度指针的长度
private int mIndicatorX; // 刻度指针指尖的X坐标
private int mIndicatorY; // 刻度指针指尖的Y坐标
private int mOutArcColor = Color.RED; // 外部弧线默认颜色
private int mNumberColor =Color.RED; // 刻度数字默认颜色
private int mInnerArcColor = Color.WHITE; //内部弧线默认颜色
private int mFullTenIndicatorColor = Color.RED; // 整10刻度颜色
private int mNotFullTenIndicatorColor = Color.WHITE; // 非整10刻度颜色
public SpeedometerView(Context context) {
super(context);
init(context,null,0);
}
public SpeedometerView(Context context, AttributeSet attrs) {
super(context,attrs);
init(context,attrs,0);</
这篇关于android 自定义速度表,自定义View-实现简易车速器(真的够简易)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!