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有不足的地方请在评论里回复(或者联系我)。
QQ:1006368252