以下只是一个简单的说明性示例。它没有经过优化,没有实现异常处理等。
public class PixelGridView extends View { private int numColumns, numRows; private int cellWidth, cellHeight; private Paint blackPaint = new Paint(); private boolean[][] cellChecked; public PixelGridView(Context context) { this(context, null); } public PixelGridView(Context context, AttributeSet attrs) { super(context, attrs); blackPaint.setStyle(Paint.Style.FILL_AND_STROKE); } public void setNumColumns(int numColumns) { this.numColumns = numColumns; calculateDimensions(); } public int getNumColumns() { return numColumns; } public void setNumRows(int numRows) { this.numRows = numRows; calculateDimensions(); } public int getNumRows() { return numRows; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); calculateDimensions(); } private void calculateDimensions() { if (numColumns < 1 || numRows < 1) { return; } cellWidth = getWidth() / numColumns; cellHeight = getHeight() / numRows; cellChecked = new boolean[numColumns][numRows]; invalidate(); } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); if (numColumns == 0 || numRows == 0) { return; } int width = getWidth(); int height = getHeight(); for (int i = 0; i < numColumns; i++) { for (int j = 0; j < numRows; j++) { if (cellChecked[i][j]) { canvas.drawRect(i * cellWidth, j * cellHeight, (i + 1) * cellWidth, (j + 1) * cellHeight, blackPaint); } } } for (int i = 1; i < numColumns; i++) { canvas.drawLine(i * cellWidth, 0, i * cellWidth, height, blackPaint); } for (int i = 1; i < numRows; i++) { canvas.drawLine(0, i * cellHeight, width, i * cellHeight, blackPaint); } } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { int column = (int)(event.getX() / cellWidth); int row = (int)(event.getY() / cellHeight); cellChecked[column][row] = !cellChecked[column][row]; invalidate(); } return true; }}
这是一个简单
Activity的演示:
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PixelGridView pixelGrid = new PixelGridView(this); pixelGrid.setNumColumns(4); pixelGrid.setNumRows(6); setContentView(pixelGrid); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)