<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DividerView">
<attr name="dvColor" format="color" />
<attr name="dvHeight" format="dimension" />
<attr name="dvThickness" format="dimension" />
</declare-styleable>
</resources>
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; import com.hbwhale.seedmoney.R; /** * Created by namh on 2016-03-26. */ public class DividerView extends View { private Paint _paint; private int _lineHeight; public DividerView(Context context, AttributeSet attrs) { super(context, attrs); int dashGap, lineHeight; int color; TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DividerView, 0, 0); try { lineThickness = a.getDimensionPixelSize(R.styleable.DividerView_dvThickness, 1); _lineHeight = a.getDimensionPixelSize(R.styleable.DividerView_dvHeight, getSuggestedMinimumHeight()); color = a.getColor(R.styleable.DividerView_dvColor, 0xff000000); } finally { a.recycle(); } _paint = new Paint(); _paint.setAntiAlias(true); _paint.setColor(color); _paint.setStyle(Paint.Style.STROKE); _paint.setStrokeWidth(lineThickness); // _paint.setPathEffect(new DashPathEffect(new float[] { lineLength, dashGap, }, 0)); } public DividerView(Context context) { this(context, null); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int height = getSuggestedMinimumHeight(); if(_lineHeight != height){ height = _lineHeight; } setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(height, heightMeasureSpec)); } @Override protected void onDraw(Canvas canvas) { float bottom = getHeight(); float hcenter = getWidth() * 0.5f; canvas.drawLine(hcenter, 0, hcenter, _lineHeight, _paint); // v-line canvas.drawLine(0, bottom, getWidth(), bottom, _paint); // h-line } }
<com.mytest.DividerView android:layout_width="match_parent" android:layout_height="wrap_content" app:dvHeight="15dp" />
<com.mytest.DividerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="5dp"
/>
SSISO Community