Android自定义TextView(一)实现圆角、反光效果

  • 声明属性
    为了方便使用,可以直接在xml资源文件中定义圆角和反光,需要声明属性,首先在res\values中定义attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomButton">
        <!-- 是否反光 -->
        <attr name="reflectLight" format="boolean"/>
        <!-- 背景 -->
        <attr name="borderWidth" format="dimension"/>
        <attr name="borderColor" format="color"/>
        <attr name="radius" format="dimension"/>
        <attr name="bgColor" format="color"/>
    </declare-styleable>
</resources>

这里的CustomButton是下面创建的

  • 重写AppCompatTextView
    话不多说直接上代码
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.cycoding.cyvideo.R;

/**
 * @author Tomas
 */
public class CustomButton extends AppCompatTextView {

    private boolean reflectLight = false;
    private int borderWidth = 0;
    private int borderColor = Color.WHITE;
    private int radius = 0;
    private int bgColor = Color.WHITE;

    private Paint mPaint;


    public CustomButton(@NonNull Context context) {
        this(context, null);
    }

    public CustomButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        if (attrs != null) {
            TypedArray attrArray = getContext().obtainStyledAttributes(attrs, R.styleable.CustomButton);
            reflectLight = attrArray.getBoolean(R.styleable.CustomButton_reflectLight, reflectLight);
            borderWidth = attrArray.getDimensionPixelSize(R.styleable.CustomButton_borderWidth, borderWidth);
            borderColor = attrArray.getColor(R.styleable.CustomButton_borderColor, borderColor);
            radius = attrArray.getDimensionPixelSize(R.styleable.CustomButton_radius, radius);
            bgColor = attrArray.getColor(R.styleable.CustomButton_bgColor, bgColor);
            attrArray.recycle();

            // 背景
            GradientDrawable gd = new GradientDrawable();
            gd.setColor(bgColor);
            gd.setCornerRadius(radius);
            if (borderWidth > 0) {
                gd.setStroke(borderWidth, borderColor);
            }
            this.setBackground(gd);
        }
    }

    public void setBackgroundColor(@ColorInt int color) {
        GradientDrawable gd = (GradientDrawable) getBackground();
        gd.setColor(color);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        if (reflectLight) {
            int r = getMeasuredHeight() / 12;
            int l = getMeasuredWidth() / 5;
            if (r > 0 && l > 0) {
                mPaint = getPaint();
                mPaint.setColor(Color.WHITE);
                mPaint.setAntiAlias(true);
                mPaint.setStyle(Paint.Style.FILL);
                canvas.drawCircle(2 * r, 2 * r,r,mPaint);
                canvas.drawRoundRect(new RectF(4 * r, r, l + 4 * r, 3 * r), r, r, mPaint);
            }
        }
    }
}

这里的l代表后面圆角矩形的长度和圆角值,同时因为矩形和原型等宽,所以r也是圆的半径。(然后具体的l和r是占多少比例的长宽自己去代码中调节,当然也可以通过多声明一个属性来标识)

  • 调用方式
            <com.cycoding.common.base.view.CustomButton
                android:id="@+id/tvLogin"
                android:layout_width="160pt"
                android:layout_height="42pt"
                android:gravity="center"
                android:text="@string/login"
                android:textColor="#FFFFFF"
                android:textSize="18pt"
                app:bgColor="@color/pink"
                app:radius="6pt"
                app:reflectLight="true" />

可以看到这里写了背景颜色和圆角,reflectLight设置为true表示让按钮具有反光效果

  • 效果
    在这里插入图片描述
    这是关于自定义View的初次尝试,也并没有用到复杂的东西,写的不好见谅。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值