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