Skip to content

Instantly share code, notes, and snippets.

@npike
Created June 19, 2014 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npike/94ab74edf5e498f6fb98 to your computer and use it in GitHub Desktop.
Save npike/94ab74edf5e498f6fb98 to your computer and use it in GitHub Desktop.
A simple view that builds out a grid.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<tv.revolt.android.view.IAPWatchGridView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
package tv.revolt.android.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
/**
* @author npike
*
*/
public class IAPWatchGridView extends View {
private Paint mPaintGrid;
private int mCellWidth = 30;
private int mCellHeight = mCellWidth;
private int mStrokeWidth = 5;
public IAPWatchGridView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintGrid = new Paint();
mPaintGrid.setStyle(Style.STROKE);
mPaintGrid.setColor(Color.BLACK);
mPaintGrid.setStrokeWidth(mStrokeWidth);
}
@Override
protected void onDraw(Canvas canvas) {
if (getWidth() > 0 && getHeight() > 0) {
// Fill the canvas with our background color
canvas.drawColor(Color.GRAY);
// draw the grid
for (int x = 0; x < getWidth(); x += mCellWidth) {
for (int y = 0; y < getHeight(); y += mCellHeight) {
canvas.drawRect(x, y, x+mCellWidth, y+mCellHeight, mPaintGrid);
}
}
} else {
// View probably hasn't been measured yet.
super.onDraw(canvas);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment