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) {
53 Paint line = new Paint();
54 line.setColor(Color.GRAY);
55 for (int i = 1; i < row + 2; i++) {
56 canvas.drawLine(width, i * height, width * (col + 1), i * height,
59 for (int i = 1; i < col + 2; i++) {
60 canvas.drawLine(i * width, height, i * width, height * (row + 1),
64 // draw cross in unused box
65 canvas.drawLine(width * (col - 1), height * row, width * col, height
67 canvas.drawLine(width * col, height * row, width * (col - 1), height
70 // draw boundary between code and extra row
71 Paint boundary = new Paint();
72 boundary.setColor(Color.BLUE);
73 canvas.drawLine(width, height * row, width * (col + 1), height * row,
77 Paint cline = new Paint();
78 cline.setColor(Color.RED);
79 canvas.drawLine(width * (col - 1), height, width * (col - 1), height
81 canvas.drawLine(width * col, height, width * col, height * row - 2,
83 canvas.drawLine(width * (col - 1), height, width * col, height, cline);
84 canvas.drawLine(width * (col - 1), height * row - 2, width * col,
85 height * row - 2, cline);
86 canvas.drawLine(width * col, height, width * col, height * (row + 1),
88 canvas.drawLine(width * (col + 1), height, width * (col + 1), height
90 canvas.drawLine(width * col, height, width * (col + 1), height, cline);
91 canvas.drawLine(width * col, height * (row + 1), width * (col + 1),
92 height * (row + 1), cline);
95 * // selRects Paint selected = new Paint();
96 * selected.setColor(Color.CYAN); for (Pair<Integer, Integer> i :
97 * uAnswer) { canvas.drawRect(i.second * width, i.first * height,
98 * i.second width + width, i.first * height + height, selected); }
102 Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
103 foreground.setColor(Color.GRAY);
104 foreground.setStyle(Style.FILL);
105 foreground.setTextSize(height * 0.75f);
106 foreground.setTextScaleX(width / height);
107 foreground.setTextAlign(Paint.Align.CENTER);
108 FontMetrics fm = foreground.getFontMetrics();
110 float y = height / 2 - (fm.ascent + fm.descent) / 2;
111 for (int i = 1; i < row + 1; i++) {
112 for (int j = 1; j < col + 1; j++) {
113 canvas.drawText(bits[i - 1][j - 1], j * width + x, i * height
120 public boolean onTouchEvent(MotionEvent event) {
121 if (event.getAction() != MotionEvent.ACTION_DOWN) {
122 return super.onTouchEvent(event);
126 selX = (int) (event.getX() / width);
127 selY = (int) (event.getY() / height);
128 if (selX <= 0 || selY <= 0 || selX > col || selY > row
129 || selX < col && selY == row) {
133 for (check = 0; check < opcol.length; check++) {
134 if (selX == opcol[check]) {
138 if (check == opcol.length) {
142 * // add user answer and inverse bit Pair<Integer, Integer> tmpAns
143 * = new Pair<Integer, Integer>(selY, selX); if
144 * (!uAnswer.remove(tmpAns)) { uAnswer.add(tmpAns); }
146 if (bits[selY - 1][selX - 1].equals("0")) {
147 bits[selY - 1][selX - 1] = "1";
149 bits[selY - 1][selX - 1] = "0";
156 public void reset() {
157 TypedArray code = res.obtainTypedArray(question.getResourceId(3, -1));
158 bits = new String[code.length()][];
159 for (int i = 0; i < code.length(); i++) {
160 bits[i] = res.getStringArray(code.getResourceId(i, -1));
166 for (int i = 0; i < opcol.length - 1; i++) {
167 for (int j = 0; j < row - 1; j++) {
168 if (!bits[j][opcol[i] - 1].equals(cAnswer[i][j])) {