OSDN Git Service

draw matrix(code)
authorYuta Kawabe <yuyu3165@gmail>
Mon, 15 Oct 2012 08:47:02 +0000 (17:47 +0900)
committerYuta Kawabe <yuyu3165@gmail>
Mon, 15 Oct 2012 08:47:02 +0000 (17:47 +0900)
ECC/src/jp/ac/titech/sharp4k/cuten/sampletask/ECCTask.java
ECC/src/jp/ac/titech/sharp4k/cuten/sampletask/MatrixView.java

index 1a37f34..61410ba 100644 (file)
@@ -51,6 +51,15 @@ public class ECCTask extends BaseApp {
                finishParam.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1);
                finishParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, -1);
                reLay.addView(finish, finishParam);
+               // Matrix View
+               MatrixView mv = new MatrixView(ctx, 5, 8);
+               mv.setId(2);
+               RelativeLayout.LayoutParams mvParam = new RelativeLayout.LayoutParams(
+                               ViewGroup.LayoutParams.FILL_PARENT,
+                               ViewGroup.LayoutParams.FILL_PARENT);
+               mvParam.addRule(RelativeLayout.BELOW, 1);
+               mvParam.addRule(RelativeLayout.ABOVE, 3);
+               reLay.addView(mv, mvParam);
                return reLay;
        }
 
index eabe97e..2a3f21a 100644 (file)
@@ -1,14 +1,65 @@
 package jp.ac.titech.sharp4k.cuten.sampletask;
 
 import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.Paint.FontMetrics;
+import android.graphics.Paint.Style;
 import android.view.View;
 
 public class MatrixView extends View {
+       float width, height;
+       int row, col;
 
-       public MatrixView(Context context) {
+       public MatrixView(Context context, int row, int col) {
                super(context);
                setFocusable(true);
                setFocusableInTouchMode(true);
+               this.row = row;
+               this.col = col;
+       }
+
+       @Override
+       protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+               width = w / (col + 2);
+               height = h / (row + 2);
+               super.onSizeChanged(w, h, oldw, oldh);
+       }
+
+       @Override
+       protected void onDraw(Canvas canvas) {
+               // draw matrix line
+               Paint line = new Paint();
+               line.setColor(Color.GRAY);
+               for (int i = 1; i < row + 2; i++) {
+                       canvas.drawLine(width, i * height, width * (col + 1), i * height,
+                                       line);
+               }
+               for (int i = 1; i < col + 2; i++) {
+                       canvas.drawLine(i * width, height, i * width, height * (row + 1),
+                                       line);
+               }
+
+               // bits
+               Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
+               foreground.setColor(Color.GRAY);
+               foreground.setStyle(Style.FILL);
+               foreground.setTextSize(height * 0.75f);
+               foreground.setTextScaleX(width / height);
+               foreground.setTextAlign(Paint.Align.CENTER);
+               FontMetrics fm = foreground.getFontMetrics();
+               float x = width / 2;
+               float y = height / 2 - (fm.ascent + fm.descent) / 2;
+               for (int i = 1; i < row + 1; i++)
+                       for (int j = 1; j < col + 1; j++)
+                               canvas.drawText("1", j * width + x, i * height + y, foreground);
+
+               // draw selected area
+               /*
+                * Paint selected = new Paint(); selected.setColor(Color.BLUE);
+                * canvas.drawRect(selRect, selected);
+                */
        }
 
 }
\ No newline at end of file