仿带噪点和横线的验证码自定义View

本文介绍了如何自定义一个验证码样式的View,包括在attrs.xml中定义属性,构造方法中获取属性,重写onDraw()和onMeasure()方法以支持wrap_content。详细讲解了drawRect()方法的参数以及MeasureSpec的三种测量模式,最后展示了如何在onDraw()方法中实现噪点和横线的绘制,以达到仿验证码效果。
摘要由CSDN通过智能技术生成

1.自定义view的步骤:

a.在values/attrs.xml文件中创建自定义的属性;
b.自定义view,在view的构造方法中获取到自定义的属性;
c.重写onDraw()方法;
d.重写onMeasure()方法。

在values/attrs.xml文件中创建自定义的属性

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <attr name="texttitle" format="string"/>
    <attr name="textcolor" format="color"/>
    <attr name="textsize" format="dimension"/>


    <declare-styleable name="MyTextView">
        <attr name="textsize"/>
        <attr name="texttitle"/>
        <attr name="textcolor"/>
    </declare-styleable>
</resources>

设置了一个自定义的textview,设置了字体,颜色和大小的属性。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:keke="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.best.keke.mycustondemo.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello World!"
        keke:textcolor=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值