1 package jp.ac.titech.sharp4k.cuten.sampletask;
3 import android.content.Context;
4 import android.content.res.Resources;
5 import android.content.res.TypedArray;
6 import android.graphics.Canvas;
7 import android.graphics.Color;
8 import android.graphics.Paint;
9 import android.graphics.Paint.FontMetrics;
10 import android.graphics.Paint.Style;
11 import android.view.MotionEvent;
12 import android.view.View;
14 public class MatrixView extends View {
15 private Resources res;
16 private float width, height;
19 private String[][] bits, cAnswer;
20 private TypedArray question;
22 public MatrixView(Context ctx, Resources res, int questionId) {
25 setFocusableInTouchMode(true);
27 question = res.obtainTypedArray(questionId);
28 row = question.getInteger(0, -1);
29 col = question.getInteger(1, -1);
30 opcol = res.getIntArray(question.getResourceId(2, -1));
31 TypedArray code = res.obtainTypedArray(question.getResourceId(3, -1));
32 bits = new String[code.length()][];
33 for (int i = 0; i < code.length(); i++) {
34 bits[i] = res.getStringArray(code.getResourceId(i, -1));
36 cAnswer = new String[opcol.length - 1][];
37 TypedArray answer = res.obtainTypedArray(question.getResourceId(4, -1));
38 for (int i = 0; i < answer.length(); i++) {
39 cAnswer[i] = res.getStringArray(answer.getResourceId(i, -1));
44 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
45 width = w / (col + 2);
46 height = h / (row + 2);
47 super.onSizeChanged(w, h, oldw, oldh);
51 protected void onDraw(Canvas canvas) {
52 // draw operable Rects
53 Paint opRect = new Paint();
54 opRect.setARGB(50, 240, 248, 255);
55 for (int i = 0; i < opcol.length; i++) {
56 if (i == opcol.length - 1) {
57 canvas.drawRect(width * opcol[i], height, width
58 * (opcol[i] + 1), height * (row + 1), opRect);
60 canvas.drawRect(width * opcol[i], height, width
61 * (opcol[i] + 1), height * row, opRect);
66 Paint line = new Paint();
67 line.setColor(Color.GRAY);
68 for (int i = 1; i < row + 2; i++) {
69 canvas.drawLine(width, i * height, width * (col + 1), i * height,
72 for (int i = 1; i < col + 2; i++) {
73 canvas.drawLine(i * width, height, i * width, height * (row + 1),
77 // draw cross in unused box
78 canvas.drawLine(width * (col - 1), height * row, width * col, height
80 canvas.drawLine(width * col, height * row, width * (col - 1), height
83 // draw boundary between code and extra row
84 Paint boundary = new Paint();
85 boundary.setColor(Color.BLUE);
86 canvas.drawLine(width, height * row, width * (col + 1), height * row,
90 Paint cline = new Paint();
91 cline.setColor(Color.RED);
92 canvas.drawLine(width * (col - 1), height, width * (col - 1), height
94 canvas.drawLine(width * col, height, width * col, height * row - 2,
96 canvas.drawLine(width * (col - 1), height, width * col, height, cline);
97 canvas.drawLine(width * (col - 1), height * row - 2, width * col,
98 height * row - 2, cline);
99 canvas.drawLine(width * col, height, width * col, height * (row + 1),
101 canvas.drawLine(width * (col + 1), height, width * (col + 1), height
103 canvas.drawLine(width * col, height, width * (col + 1), height, cline);
104 canvas.drawLine(width * col, height * (row + 1), width * (col + 1),
105 height * (row + 1), cline);
108 * // selRects Paint selected = new Paint();
109 * selected.setColor(Color.CYAN); for (Pair<Integer, Integer> i :
110 * uAnswer) { canvas.drawRect(i.second * width, i.first * height,
111 * i.second width + width, i.first * height + height, selected); }
115 Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
116 foreground.setColor(Color.GRAY);
117 foreground.setStyle(Style.FILL);
118 foreground.setTextSize(height * 0.75f);
119 foreground.setTextScaleX(width / height);
120 foreground.setTextAlign(Paint.Align.CENTER);
121 FontMetrics fm = foreground.getFontMetrics();
123 float y = height / 2 - (fm.ascent + fm.descent) / 2;
124 for (int i = 1; i < row + 1; i++) {
125 for (int j = 1; j < col + 1; j++) {
126 canvas.drawText(bits[i - 1][j - 1], j * width + x, i * height
133 public boolean onTouchEvent(MotionEvent event) {
134 if (event.getAction() != MotionEvent.ACTION_DOWN) {
135 return super.onTouchEvent(event);
139 selX = (int) (event.getX() / width);
140 selY = (int) (event.getY() / height);
141 if (selX <= 0 || selY <= 0 || selX > col || selY > row
142 || selX < col && selY == row) {
146 for (check = 0; check < opcol.length; check++) {
147 if (selX == opcol[check]) {
151 if (check == opcol.length) {
155 * // add user answer and inverse bit Pair<Integer, Integer> tmpAns
156 * = new Pair<Integer, Integer>(selY, selX); if
157 * (!uAnswer.remove(tmpAns)) { uAnswer.add(tmpAns); }
159 if (bits[selY - 1][selX - 1].equals("0")) {
160 bits[selY - 1][selX - 1] = "1";
162 bits[selY - 1][selX - 1] = "0";
169 public void reset() {
170 TypedArray code = res.obtainTypedArray(question.getResourceId(3, -1));
171 bits = new String[code.length()][];
172 for (int i = 0; i < code.length(); i++) {
173 bits[i] = res.getStringArray(code.getResourceId(i, -1));
179 for (int i = 0; i < opcol.length - 1; i++) {
180 for (int j = 0; j < row - 1; j++) {
181 if (!bits[j][opcol[i] - 1].equals(cAnswer[i][j])) {