Android自定义view,CloseView,关闭按钮

1、效果图:

这里写图片描述

2、代码:

attr.xml

<declare-styleable name="CloseView">
        <attr name="cv_color" format="color|reference" />
        <attr name="cv_background_color" format="color|reference" />
        <attr name="cv_stroke_width" format="dimension" />

        <attr name="cv_shape">
            <enum name="circle" value="0" />
            <enum name="rectangle" value="1" />
        </attr>
    </declare-styleable>

view源码:

package exam.org.jsc.password.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import exam.org.jsc.password.R;

/**
 * Created by Justin Qin on 3/25/2017.
 */

public class CloseView extends View {
    Paint mPaint;
    int color;
    int backgroundColor;
    int shape;
    int strokeWidth;

    public CloseView(Context context) {
        this(context, null);
    }

    public CloseView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CloseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        int dp2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, context.getResources().getDisplayMetrics());

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CloseView);
        color = array.getColor(R.styleable.CloseView_cv_color, Color.WHITE);
        backgroundColor = array.getColor(R.styleable.CloseView_cv_background_color, Color.TRANSPARENT);
        strokeWidth = array.getDimensionPixelSize(R.styleable.CloseView_cv_stroke_width, dp2);
        shape = array.getInt(R.styleable.CloseView_cv_shape, 0);
        array.recycle();
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();

        mPaint.setColor(backgroundColor);
        mPaint.setStyle(Paint.Style.FILL);
        switch (shape){
            case 0://circle
                canvas.drawCircle(width / 2, height / 2, width > height ? height / 2 : width / 2, mPaint);
                break;
            case 1://rectangle
                canvas.drawRect(0, 0, width, height, mPaint);
                break;
        }

        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(strokeWidth);
        canvas.drawLine(getPaddingLeft(), getPaddingTop(), width - getPaddingRight(), height - getPaddingBottom(), mPaint);
        canvas.drawLine(width - getPaddingRight(), getPaddingTop(), getPaddingLeft(), height - getPaddingBottom(), mPaint);
    }
}

3、使用:

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="exam.org.jsc.password.activity.AboutActivity">

    <include layout="@layout/title_bar" />

    <exam.org.jsc.password.view.CloseView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="80dp"
        android:padding="12dp"
        app:cv_background_color="@color/colorAccent"
        app:cv_shape="circle"
        app:cv_stroke_width="2dp" />

</LinearLayout>

谢谢您的惠顾,code有不足的地方请在评论里回复(或者联系我)。
QQ1006368252

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值