模仿android相机的网格线功能

        做自定义相机时需要实现显示网格线,这个需求实现的方法有很多种,最简单的就是用布局实现,这种方法感觉太low,维护的人肯定会说low逼,所以为了维护方便和应对网格个数的改变,所以决定在SurfaceView的基础上自定义一个带网格线的GridSurfacemView,练练手实现很简单,话不多说直接上代码。

首先继承SurfaceView实现GridView:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceView;
import android.view.WindowManager;

public class GridSurfaceView extends SurfaceView {
	/**
	 * 竖向线条数,可通过布局属性设置列数
	 */
	private int linesX = 2;
	/**
	 * 竖向线条数,可通过布局属性设置行数
	 */
	private int linesY = 2;
	
	private int width;
	private int height;
	private Paint mPaint = null;
	public GridSurfaceView(Context context) {
		this(context, null);
	}
	
	public GridSurfaceView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public GridSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.gridSurfaceView);
		linesX = a.getInteger(R.styleable.gridSurfaceView_linesX, linesX);
		linesY = a.getInteger(R.styleable.gridSurfaceView_linesY, linesY);
		a.recycle();
		init();
	}
	
	private void init(){
		mPaint = new Paint();
		mPaint.setColor(Color.WHITE);
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setStrokeWidth(1);
	}
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
		width = wm.getDefaultDisplay().getWidth() - this.getResources().getDimensionPixelOffset(R.dimen.classes_dp_40);
		height = wm.getDefaultDisplay().getHeight();
		setMeasuredDimension(width, height);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int x = width/(linesX + 1);
		int y = height/(linesY + 1);
		for(int i = 1 ; i <= linesX ; i++){
			canvas.drawLine(x * i, 0, x * i, height, mPaint);
		}
		
		for (int i = 1; i <= linesY; i++) {
			canvas.drawLine(0, y * i, width, y * i, mPaint);
		}
	}
	
}

设置它的自定义属性:行列数


 

最后就是使用,具体相机中实现可以看我的另一篇关于自定义相机的文章 点击打开链接  只需要把布局中的SurfaceView换成下面GridView即可:





你可能感兴趣的:(android,camera,自定义控件)