GridView绘制田字格,虚线

​
package com.tan.griddemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.GridView;

/**
 * @author 檀 2017-1-10
 * @des 
 */
public class MyGridView extends GridView {

	private Paint mPaint;
	private Paint mLinePaint;
	private Path mPath;

	public MyGridView(Context context) {
		super(context);
		init(context);
	}

	public MyGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MyGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setColor(Color.parseColor("#ff0000"));

		mLinePaint = new Paint();
		mLinePaint.reset();
		mLinePaint.setStyle(Paint.Style.STROKE);
		mLinePaint.setStrokeWidth(1);
		mLinePaint.setColor(Color.parseColor("#ff0000"));
		mLinePaint.setAntiAlias(true);
		DashPathEffect pathEffect = new DashPathEffect(new float[] { dp2px(4), dp2px(4)},1);
		mLinePaint.setPathEffect(pathEffect);

		mPath = new Path();
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		drawLine(canvas);
		// draw9line(canvas);
	}

	/**
	 * @des 绘制田字格
	 * @param canvas
	 */
	private void draw9line(Canvas canvas) {
		View localView1 = getChildAt(0);
		int column = getWidth() / localView1.getWidth();
		int childCount = getChildCount();

		for (int i = 0; i < childCount; i++) {
			View view = getChildAt(i);
			int left = view.getLeft();
			int right = view.getRight();
			int top = view.getTop();
			int bottom = view.getBottom();
			if ((i + 1) % column == 0) {
				canvas.drawLine(left, bottom, right, bottom, mPaint);
			} else if ((i + 1) > (childCount - (childCount % column))) {
				canvas.drawLine(right, top, right, bottom, mPaint);
			} else {
				canvas.drawLine(right, top, right, bottom, mPaint);
				canvas.drawLine(left, bottom, right, bottom, mPaint);
			}
		}
		if (childCount % column != 0) {
			for (int j = 0; j < (column - childCount % column); j++) {
				View lastView = getChildAt(childCount - 1);
				canvas.drawLine(lastView.getRight() + lastView.getWidth() * j, lastView.getTop(),
						lastView.getRight() + lastView.getWidth() * j, lastView.getBottom(), mPaint);
			}
		}
	}

	/**
	 * @des 在item底部绘制虚线
	 * @param canvas
	 */
	private void drawLine(Canvas canvas) {
		View localView1 = getChildAt(0);
		int column = getWidth() / localView1.getWidth();
		int childCount = getChildCount();
		// 最后一行不绘制
		for (int i = 0; i < childCount - childCount % getNumColumns(); i++) {
			View view = getChildAt(i);
			int left = view.getLeft();
			int right = view.getRight();
			int bottom = view.getBottom();

			if ((i + 1) % column == 0) {
				mPath.moveTo(left, bottom);
				mPath.lineTo(right, bottom);
				canvas.drawPath(mPath, mLinePaint);
				mPath.reset();
			} else {
				mPath.moveTo(left, bottom);
				mPath.lineTo(right, bottom);
				canvas.drawPath(mPath, mLinePaint);
				mPath.reset();
			}
		}
	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources()
				.getDisplayMetrics());
	}

}

​

猜你喜欢

转载自blog.csdn.net/mr_tanenjoy/article/details/81188810