一、RingView

自己定义的view,构造器必须重写,至于重写哪个方法,參考例如以下:

①假设须要改变View绘制的图像,那么须要重写OnDraw方法。(这也是最经常使用的重写方式。)

②假设须要改变view的大小,那么须要重写OnMeasure方法。

③假设须要改变View的(在父控件的)位置,那么须要重写OnLayout方法。

④依据上面三种不同的须要你能够组合出多种重写方案,你懂的。

凝视信息代码中比較具体。

package com.example.customerviewdemo2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View; public class RingView extends View { private final Paint paint;
private final Context context; public RingView(Context context) {
this(context, null);
} public RingView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.paint = new Paint();
this.paint.setAntiAlias(true); //消除锯齿
this.paint.setStyle(Paint.Style.STROKE); //绘制空心圆
} @Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
int center = getWidth()/2;
int innerCircle = dip2px(context, 83); //设置内圆半径
int ringWidth = dip2px(context, 10); //设置圆环宽度 //绘制内圆
this.paint.setARGB(155, 167, 190, 206);
this.paint.setStrokeWidth(10);//设置内圆的厚度
canvas.drawCircle(center,center, innerCircle, this.paint);//以该圆为半径向内外扩展至厚度为10px //绘制圆环,设置圆环的颜色改动画笔的颜色
// this.paint.setARGB(255, 212 ,225, 233);
this.paint.setARGB(255, 255, 0, 0);
this.paint.setStrokeWidth(ringWidth);//设置圆环宽度
canvas.drawCircle(center,center, innerCircle+1+ringWidth/2, this.paint);//圆环宽度为中间圆 //绘制外圆
this.paint.setARGB(155, 167, 190, 206);
this.paint.setStrokeWidth(2);
canvas.drawCircle(center,center, innerCircle+ringWidth, this.paint); super.onDraw(canvas);
} /**
* 依据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}

二、引用该View的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <com.example.customerviewdemo2.RingView
android:layout_width="300dp"
android:layout_height="300dp" >
</com.example.customerviewdemo2.RingView> </RelativeLayout>

三、效果例如以下

自己定义View之绘制圆环的更多相关文章

  1. 图文剖析自己定义View的绘制(以自己定义滑动button为例)

    自己定义View一直是横在Android开发人员面前的一道坎. 一.View和ViewGroup的关系 从View和ViewGroup的关系来看.ViewGroup继承View. View的子类.多是 ...

  2. Android画图系列(二)——自己定义View绘制基本图形

    这个系列主要是介绍下Android自己定义View和Android画图机制.自己能力有限.假设在介绍过程中有什么错误.欢迎指正 前言 在上一篇Android画图系列(一)--自己定义View基础中我们 ...

  3. 安卓自己定义View进阶-Canvas之绘制基本形状

    Canvas之绘制基本形状 作者微博: @GcsSloop [本系列相关文章] 在上一篇自己定义View分类与流程中我们了解自己定义View相关的基本知识,只是,这些东西依然还是理论,并不能拿来(zh ...

  4. Android 自己定义View (四) 视频音量调控

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24529807 今天没事逛eoe,看见有人求助要做一个以下的效果,我看以下一哥们说 ...

  5. 自定义控件(View的绘制流程源码解析)

    参考声明:这里的一些流程图援引自http://a.codekk.com/detail/Android/lightSky/%E5%85%AC%E5%85%B1%E6%8A%80%E6%9C%AF%E7% ...

  6. 【转】Android中View的绘制过程 onMeasure方法简述 附有自定义View例子

    Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android framework将会处理绘制过程,Activity只需提供它的布局的根节点. 绘制过程从布 ...

  7. Android 中View的绘制机制源代码分析 三

    到眼下为止,measure过程已经解说完了,今天開始我们就来学习layout过程.只是在学习layout过程之前.大家有没有发现我换了编辑器,哈哈.最终下定决心从Html编辑器切换为markdown编 ...

  8. Android 它们的定义View (一)

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 非常Android入门程序员AndroidView.可能都是比較恐 ...

  9. Android 它们的定义View它BounceProgressBar

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/41246247 [Rocko's blog] 之前几天下载了非常久没用了的桌面版酷狗来用用的时候 ...

随机推荐

  1. Cocos2d-x 如何输出 Android用电话 腰带Tag的Log刊物

    于Cocos2d-x根据代码 #if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #define LOGAnroid( ...) #else if (CC_TAR ...

  2. Hibernate HQL详细说明

    1.  Hibernate HQL详细说明 1.1.  hql一个简短的引论 Hibernate它配备了一种非常强大的查询语言.这种语言看起来非常像SQL.但是不要 要对相位的语法结构似,HQL是很有 ...

  3. 【从翻译mos文章】不再用par file如果是,export or import 包含大写和小写表名称表

    不再用par file如果是,export or import 包含大写和小写表名称表 参考原始: How to Export or Import Case Sensitive Tables With ...

  4. ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性

    原文:ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性 深入讲解控件的属性持久化(一) 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开 ...

  5. jQuery 有条件排序

    尊重劳动成果,转载请注明出处(http://blog.csdn.net/sllailcp/article/details/41011173)... 点击button,表格里就会依照分数的高低.将学生信 ...

  6. POJ9384 迷宫(基金会BFS)

    本文来源于:http://blog.csdn.net/svitter 称号:让你从(0, 0)走到(4,4).而且输出路径. 输入数据:二位数组的迷宫:输出数据:路径: 题解:简单的BFS 注意: 1 ...

  7. ZOJ1463:Brackets Sequence(间隙DP)

    Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...

  8. ASP.NET MVC 5– 采用Wijmo MVC 5模板1创建应用程序分钟

    启用 采用ComponentOne Studio for ASP.NET Wijmo制作MVC5应用,首先要做的就是安装pid=4&from=MVC4DOC">Studio f ...

  9. 怎么样sourceforge开源项目发现,centos安装-同htop安装案例

    一个.htop什么? top是linux下经常使用的监控程序.htop相当于其加强版,颜色显示不同參数.且支持鼠标操作. 详细介绍參看此说明文档. watermark/2/text/aHR0cDovL ...

  10. jekyll博客安装

    摘要: 一直用Mac,换了新公司使用的电脑是windows,网上粗略的看了一下Jekyll的安装.简略的实现了一遍 首先安装Ruby "Ruby安装文件下载地址" 下载对应版本,我 ...