android 自定义速度表,自定义View-实现简易车速器(真的够简易)

自定义View-实现简易车速器(真的够简易)

学习自定义View挺久了,好久没用都快忘了,这里实现一个简易的车速器算是一个回顾,项目比较简单,代码较少,但自定义View的流程基本都涉及到了.本文不是一篇讲解自定义View基础的文章,而是一个小的实战,如果想看讲解自定义View的文章,强烈推荐博客:http://blog.csdn.net/aigestudio,强烈=_=.

效果如下图:

bVLDob?w=394&h=694

接下来是代码部分:

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);</

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值