Android自定义view,titlebar的返回按钮

1、效果图:

这里写图片描述

2、代码:

attr.xml

<declare-styleable name="ArrowView">
        <attr name="av_color" format="color|reference" />
        <attr name="av_background_color" format="color|reference" />
        <attr name="av_stroke_width" format="dimension" />

        <attr name="av_direction">
            <enum name="left" value="0" />
            <enum name="top" value="1" />
            <enum name="right" value="2" />
            <enum name="bottom" value="3" />
        </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 ArrowView extends View {
    Paint mPaint;
    int color;
    int backgroundColor;
    int direction;
    int strokeWidth;
    Path path = new Path();

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

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

    public ArrowView(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.ArrowView);
        color = array.getColor(R.styleable.ArrowView_av_color, Color.WHITE);
        backgroundColor = array.getColor(R.styleable.ArrowView_av_background_color, Color.TRANSPARENT);
        strokeWidth = array.getDimensionPixelSize(R.styleable.ArrowView_av_stroke_width, dp2);
        direction = array.getInt(R.styleable.ArrowView_av_direction, 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);
        canvas.drawRect(0, 0, width, height, mPaint);

        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(strokeWidth);
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();

        float temp;
        float x;
        float y;
        switch (direction){
            case 0://left
                temp = height / 2 - paddingTop;
                x = paddingLeft + temp;
                y = paddingTop;
                path.moveTo(x, y);

                x = paddingLeft;
                y = height / 2;
                path.lineTo(x, y);

                temp = height / 2 - paddingBottom;
                x = paddingLeft + temp;
                y = height - paddingBottom;
                path.lineTo(x, y);
                break;
            case 1://top
                temp = width / 2 - paddingLeft;
                x = paddingLeft;
                y = paddingTop + temp;
                path.moveTo(x, y);

                x = width / 2;
                y = paddingTop;
                path.lineTo(x, y);

                temp = width / 2 - paddingRight;
                x = width - paddingRight;
                y = paddingTop + temp;
                path.lineTo(x, y);
                break;
            case 2://right
                temp = height / 2 - paddingTop;
                x = width - paddingRight - temp;
                y = paddingTop;
                path.moveTo(x, y);

                x = width - paddingRight;
                y = height / 2;
                path.lineTo(x, y);

                temp = height / 2 - paddingBottom;
                x = width - paddingRight - temp;
                y = height - paddingBottom;
                path.lineTo(x, y);
                break;
            case 3://bottom
                temp = width / 2 - paddingLeft;
                x = paddingLeft;
                y = height - paddingBottom - temp;
                path.moveTo(x, y);

                x = width / 2;
                y = height - paddingBottom;
                path.lineTo(x, y);

                temp = width / 2 - paddingRight;
                x = width - paddingRight;
                y = height - paddingBottom - temp;
                path.lineTo(x, y);
                break;
        }
        canvas.drawPath(path, mPaint);
    }
}

3、使用:

title_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/title_bar_height"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <exam.org.jsc.password.view.ArrowView
        android:id="@+id/btn_back"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:padding="8dp"
        app:av_stroke_width="1dp"
        app:av_color="#c9c9c9"
        app:av_direction="left" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Title"
        android:textColor="@android:color/white"
        android:textSize="@dimen/title"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        andrrd:layut_alignParentStart="true" />

</RelativeLayout>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值