项目:具有圆形效果的自定义View

一、继承View并重写onDraw方法

public class CircleView extends View{
private static final int COLOR = Color.RED;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int mWidth = 0;
private int mHeight = 0; public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public CircleView(Context context) {
super(context);
init();
} public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
} private void init(){
mPaint.setColor(COLOR);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//获取当前View的宽/高
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
//获取半径
int radium = Math.min(mWidth,mHeight)/2;
//画圆
canvas.drawCircle(mWidth/2,mHeight/2,radium,mPaint);
} }

CircleView

在xml中测试margin发现可以用,说明margin是由父容器控制的(想起measureChildMarginLayout源码)

但是wrap_content和padding都不生效。

二、让wrap_content生效

根据上一章View的工作原理:①、重写onMeasure方法  ②、给CircleView设定一个固定的宽高

//设定wrap_content时候的宽度
private static final int AT_WIDTH = 30;
private static final int AT_HEIGHT = 30; //重写onMeasure()方法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//获取子View的范围
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//判断当属性为wrap_content的时候
if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST){
setMeasuredDimension(AT_WIDTH,AT_HEIGHT);
}
else if (widthMode == MeasureSpec.AT_MOST){
setMeasuredDimension(AT_WIDTH,heightSize);
}
else if (heightMode == MeasureSpec.AT_MOST){
setMeasuredDimension(widthSize,AT_HEIGHT);
}
else {
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}
}

三、解决无法padding的问题

原理:只需要在onDraw中,获取padding的参数就可以了

//重写onDraw方法
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//获取padding
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
//获取当前View的宽/高 减去padding
mWidth = getMeasuredWidth() - paddingLeft - paddingRight;
mHeight = getMeasuredHeight() - paddingTop - paddingBottom;
//获取半径
int radium = Math.min(mWidth,mHeight)/2;
//画圆
canvas.drawCircle(paddingLeft+mWidth/2,paddingTop - mHeight/2,radium,mPaint);
}

CircleView

四、自定义属性

步骤:①、在values目录中创建xml文件名,文件名必须以attr_开头。②、内容的编写:<declare-styleadable>标签中:name代表自定义属性(该为CircleView类)

<attr>标签中 name代表之后使用的属性名(circle_color),format代表格式(color)

<resources>
<declare-styleable name="CircleView">
<attr name="color_circle" format="color"/>
</declare-styleable>
</resources>

attr_circleview

步骤③、在布局文件中使用自定义属性  必须在schemas声明:xmlns:app="http://schemas.android.com/apk/res-auto" 其中app名字可以随便替换。

但是circleView中自定义属性名的前缀必须是和这里一致(一般习惯使用app)

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
//这一段必须要加 app名字可以替换
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.maikefengchao.circleview.MainActivity"> <com.maikefengchao.circleview.CircleView
android:layout_width="80dp"
android:layout_height="80dp"
//前缀与添加的声明前缀一致
app:color_circle="#9999"/>
</LinearLayout>

步骤④:在CircleView中获取自定义属性参数

//在构造方法中使用
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//加载自定义属性集合CircleView
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CircleView);
//解析集合中的circle_circle,设置默认颜色
mColor = a.getColor(R.styleable.CircleView_color_circle,Color.RED);
init();
}

全部代码:(P209 ①)

自定义View 一 (继承VIew重写onDraw方法)的更多相关文章

  1. Django admin 继承user表后密码为明文,继承UserAdmin,重写其方法

    Django用户继承AbstractUser后密码为明文 其实本不应该有这个问题,却花了我很久的时间,因为还是初学阶段. 造成这个原因是因为在admin注册的生活没有指定Admin 在app的admi ...

  2. 向集合中添加自定义类型--建议在自定义类型的时候要重写equals方法

    package com.bjpowernode.t01list; import java.util.ArrayList; /* * 向集合中添加自定义类型 */public class TestLis ...

  3. Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法

    Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法 摘自:https://blog.csdn.net/qq_31635851/article/details/ ...

  4. C#中在比较自定义对象的时候要重写Equals方法

    using System;using System.Collections.Generic;using System.Text; namespace Equal{    using System; c ...

  5. 自定义视图(继承View)

    前言 Android提供了丰富的控件,但是有时候还是不能满足自己的需求,这时候就需要自定义视图了,自定义视图分为几种,一种为继承为View的,一种为继承于ViewGroup的.继承于View的需要我们 ...

  6. java继承 子类重写父类方法

    package com.addd; //多态 public class Sld { private String name = "zhangsan"; public Sld() { ...

  7. 自定义View(二)--继承自ViewGroup

    自定义View包括很多种,上一次随笔中的那一种是完全继承自View,这次写的这个小Demo是继承自ViewGroup的,主要是将自定义View继承自ViewGroup的这个流程来梳理一下,这次的Dem ...

  8. Android单独继承View类来实现自定义控件

    一个单独继承view类来实现自定义控件,在该方法中,需要重写ondraw方法来绘制自己所需要的控件,下面也以一个简单的例子来说明如何实现自定义控件.该方法可以实现所需要的所有的自定义控件. 属性文件中 ...

  9. Android-自定义控件-继承View与ViewGroup的初步理解

    继承View需要走的流程是: 1.构造实例化, public ChildView(Context context, @Nullable AttributeSet attrs) 2.测量自身的高和宽on ...

随机推荐

  1. 操作数据表中的记录——SELECT (where表达式、GROUP BY、HAVING、LIMIT)

    原文链接:http://www.ifyao.com/2015/01/26/%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E8%A1%A8%E4%B8%AD%E7%9A%84 ...

  2. Hibernate事务传播性

    事务的几种传播特性 1. PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务.如果没有事务则开启 比如说:在UserManager中addUser里开启了,那么在addLog ...

  3. POJ 1020 Anniversary Cake(DFS)

    Anniversary Cake Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit St ...

  4. mysql 性能分析套件

    #!/usr/local/python3./bin/python3. #!coding:utf- #################################### #目地:用于诊断mysql性 ...

  5. Uubntu scrot 的简单使用

    scrot 是一个使用 imlib2 库截取屏幕和保存图像的的工具. 1:安装 #apt-get install scrot 2:查看帮助 #scrot -help -v, --version显示版本 ...

  6. 2015第10周日CSS—3

    CSS各种居中方法 CSS的居中有水平居中和垂直居中,这两种居中又分为行内元素居中和块级元素居中,不同的居中用不同方法. 水平居中 1.行内元素水平居中(文本,图片) 给父层设置 text-align ...

  7. jQuery UI 之 EasyUI 快速入门

    jQuery EasyUI 基础 转载自(http://www.shouce.ren/api/view/a/3350) jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用户界面 ...

  8. UESTC_冬马党 CDOJ 882

    冬马党 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Statu ...

  9. 剑指offer-面试题14.调整数组顺序使奇数位于偶数的前面

    题目:输入一个整数数组,实现一个函数来调整该数组中的数字的顺序, 使得所有的奇数位于数组的前半部门,所有的偶数位于数组的后半部门. 这一题一看其实是很简单的,当然在不考虑效率的情况可以这样考虑 我们将 ...

  10. sroHOBOorz来自HOBO的高精类

    /* bigint() bigint(long long) bigint(bigint) bigint(char*) bigint(string) +, +=, ++ -, -=, -- *, *= ...