Android控件使用篇Android专题

自定义View系列(一):圆

2020-11-12  本文已影响0人  千夜零一

引言

  Android开发中的自定义View、自定义ViewGroup人云亦云,也逐渐成为了大家进阶路上的试炼场。简而言之,就是当系统控件无法满足我们的开发需求时,我们需要用代码自定义我们需要的View,并且给其手动设置各种属性,然后在布局文件中调用和设置属性值使用。
  自定义View,就是自定义单个控件,比如圆,比如矩形,比如环形,画出来的单个控件就称之为自定义View;而自定义ViewGroup顾名思义,就是成组的单个控件,也成为组件,比如TitleBar,其中包含多个单个控件。
  今天就开始自定义View系列的首篇:圆。


解析

  一般而言,自定义View都需要继承自View类,自定义View类MyCircle继承自View,添加其构造方法并重写其onMeasure()+onDraw()方法。Paint,为画笔,主要用于自定义控件中绘制;Canvas,为画布,主要用于自定义控件中构造图形。
  此外,所有的自定义控件属性都需要自己定义和实现方能在布局文件中设置使用。一般属性的定义都放在styles.xml中,在自定义View的构造方法中调用并设置实现。


用法

第一步:创建MyCircle类

自定义View类MyCircle继承自View,添加其构造方法并重写其onMeasure()+onDraw()方法。

/**
 * @data on 2020/11/11 3:32 PM
 * @auther armStrong
 * @describe 自定义实心圆
 */
public class MyCircle extends View {
    private Context context;
    private Paint paint = new Paint();;
    //圆心
    private int center_x;
    private int center_y;
    //半径
    private int r;
    //圆的颜色
    private int circleColor;
    //背景色
    private int bgColor;
    //padding
    private int paddingLeft;
    private int paddingRight;
    private int paddingTop;
    private int paddingBottom;

    public MyCircle(Context context) {
        super(context);
    }

    public MyCircle(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        //获取属性值
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyCircle);
        circleColor = typedArray.getColor(R.styleable.MyCircle_circle_color,Color.BLACK);
        bgColor = typedArray.getColor(R.styleable.MyCircle_background_color,Color.WHITE);
        //回收
        typedArray.recycle();
        //设置画笔
        paint.setAntiAlias(true); //抗锯齿
        paint.setStyle(Paint.Style.FILL); //实心圆
        //paint.setColor(context.getResources().getColor(R.color.white)); //从绝对路径取颜色才可以,直接使用R.color.white会颜色错乱,得不到指定的颜色
        paint.setColor(circleColor);
        //设置padding
        paddingTop = getPaddingTop();
        paddingLeft = getPaddingLeft();
        paddingRight = getPaddingRight();
        paddingBottom = getPaddingBottom();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获得圆心
        center_x = getMeasuredWidth() / 2 ;
        center_y = getMeasuredHeight() / 2 ;
        //取小的为半径
        r = Math.min((getMeasuredWidth()-paddingLeft-paddingRight) / 2, (getMeasuredHeight()-paddingTop-paddingBottom) / 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置画布
        canvas.drawColor(bgColor);
        canvas.drawCircle(center_x, center_y, r, paint);
    }
}

第二步:在styles.xml文件中创建自定义View的自定义属性

比如:我创建了两个:一个为指定圆的填充颜色,一个为圆外的背景色。

<resources>
    <declare-styleable name="MyCircle">
        <attr name="circle_color" format="color"/>
        <attr name="background_color" format="color"/>
    </declare-styleable>
</resources>

第三步:在布局文件中使用自定义View:MyCircle

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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=".show.Show0"
    tools:ignore="MissingConstraints">

    <com.example.diyview.view.MyCircle
        android:layout_margin="5dp"
        android:layout_width="100dp"
        android:padding="10dp"
        app:background_color="@color/gray"
        app:circle_color="@color/blue"
        android:layout_height="100dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

大功告成!

上一篇下一篇

猜你喜欢

热点阅读