teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

ご指摘のあった部分のコードを追加で掲載しました(ピースをつくる部分)

2021/11/19 01:12

投稿

SanaeToyoda
SanaeToyoda

スコア1

title CHANGED
File without changes
body CHANGED
@@ -200,86 +200,32 @@
200
200
  }
201
201
 
202
202
  }
203
-
204
203
 
205
- 以下省略
204
+ 省略
206
-
207
- ```
208
205
 
209
- ○レイアウト
210
- ```java
211
- <?xml version="1.0" encoding="utf-8"?>
212
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
213
- xmlns:app="http://schemas.android.com/apk/res-auto"
214
- xmlns:tools="http://schemas.android.com/tools"
215
- android:layout_width="match_parent"
216
- android:layout_height="match_parent"
217
- tools:context=".WaikikipuzzleActivity">
218
206
 
219
- <LinearLayout
207
+ //画像を分割してピースを作る
220
- android:layout_width="match_parent"
221
- android:layout_height="match_parent"
208
+ private void drawPic(Canvas canvas,Patch patch){
209
+ if(patch.isEmpty()){
210
+ return;
211
+ }
212
+ Rect rb = new Rect(patch.getBitmapPicPoint().getX()*bitmapW + padding, patch.getBitmapPicPoint().getY()*bitmapH + padding,
222
- android:orientation="vertical">
213
+ (patch.getBitmapPicPoint().getX()+1)*bitmapW - padding, (patch.getBitmapPicPoint().getY()+1)*bitmapH - padding);
214
+ Rect rc = new Rect(patch.getCanvasPicPoint().getX()*canvasW + padding,patch.getCanvasPicPoint().getY()*canvasH + padding,
215
+ (patch.getCanvasPicPoint().getX()+1)*canvasW - padding, (patch.getCanvasPicPoint().getY()+1)*canvasH - padding);
223
216
 
224
- <androidx.appcompat.widget.Toolbar
225
- android:id="@+id/ac_toolbar_toolbar"
226
- android:layout_width="match_parent"
227
- android:layout_alignParentTop="true"
217
+ canvas.drawBitmap(mBitmap, rb, rc, null);
228
- app:popupTheme="@style/PopupMenu"
229
- android:layout_height="wrap_content"
218
+ }
230
- android:background="?attr/colorPrimary" />
231
219
 
220
+
232
- <LinearLayout
221
+ 以下省略
233
- android:layout_width="match_parent"
234
- android:layout_height="match_parent"
235
- android:orientation="horizontal">
236
222
 
223
+
224
+ ```
237
225
 
238
- <ImageView
239
- android:id="@+id/waikikiphoto"
240
- android:layout_width="0dp"
241
- android:layout_height="wrap_content"
242
- android:layout_weight="3"
243
- android:background="#add8e6"
244
- android:layout_marginRight="10dp"
245
- android:layout_gravity="center_horizontal"
246
- android:src="@drawable/waikikibeach"/>
247
226
 
248
- <com.toyocli.hawaii_travel_puzzle.GameView
249
- android:id="@+id/game_view"
250
- android:background="#77998855"
251
- android:layout_below="@id/ac_toolbar_toolbar"
252
- android:layout_width="0dp"
253
- android:layout_weight="5"
254
- android:layout_height="match_parent"
255
- android:layout_marginTop="10dp"
256
- android:layout_marginBottom="10dp"/>
257
227
 
258
- <ImageButton
259
- android:id="@+id/waikikipuzzle_giveup"
260
- android:layout_width="0dp"
261
- android:layout_height="wrap_content"
262
- android:layout_weight="1"
263
- android:layout_gravity="bottom"
264
- android:background="#00000000"
265
- android:src="@drawable/givebutun25"/>
266
228
 
267
-
268
-
269
-
270
-
271
- </LinearLayout>
272
-
273
- </LinearLayout>
274
-
275
-
276
- </RelativeLayout>
277
-
278
-
279
- ```
280
-
281
-
282
-
283
229
  ### 試したこと
284
230
 
285
231
  GameViewを以下のように変更してみましたが、画像が表示されませんでした。

5

追記です。

2021/11/19 01:12

投稿

SanaeToyoda
SanaeToyoda

スコア1

title CHANGED
File without changes
body CHANGED
@@ -20,7 +20,7 @@
20
20
  ```
21
21
 
22
22
  ### 該当のソースコード
23
-
23
+ *waikikiActivity(本来は、photo=0ですが、画像が変わるかをチェックするため、あえて”1”にしています。
24
24
  ```java
25
25
  public class WaikikipuzzleActivity extends AppCompatActivity {
26
26
 

4

追記です。

2021/11/18 08:01

投稿

SanaeToyoda
SanaeToyoda

スコア1

title CHANGED
File without changes
body CHANGED
@@ -282,15 +282,36 @@
282
282
 
283
283
  ### 試したこと
284
284
 
285
- いきなりgameview.setphoto(photo)とず、
285
+ GameViewを以下のように変更してみましたが画像が表示されまんでした。
286
+ ```java
286
- Public void setPhoto(int photo){
287
+ int[] mImageResources = { R.drawable.waikikibeach, R.drawable.diyamondophoto};
287
288
 
288
-  photo=1;
289
-  gameview.setPhoto(photo);
289
+ public void setPhoto(int photo) {
290
-
291
- と書いてみましたが、ダメでした。
290
+ this.photo = photo;
291
+ mResources = getResources();
292
+ mBitmap1 = BitmapFactory.decodeResource(mResources, mImageResources[photo]);
292
293
 
294
+ }
293
295
 
296
+ public GameView(Context context){
297
+ this(context, null);
298
+ }
299
+ public GameView(Context context, AttributeSet attrs) {
300
+
301
+ super(context, attrs);
302
+ mycontext = context;
303
+ mResources = getResources();
304
+ mBitmap = ((BitmapDrawable)mResources.getDrawable(mImageResources[photo])).getBitmap();
305
+ bitmapW = mBitmap.getWidth()/level;
306
+ bitmapH = mBitmap.getHeight()/level;
307
+ initPatches();
308
+ initSound();
309
+
310
+
311
+ ```
312
+
313
+
314
+
294
315
  ### 補足情報(FW/ツールのバージョンなど)
295
316
 
296
- ここより詳細な情報を記載してください。
317
+ GameViewは、昔、プロの方作成してもらったコードで、今回、ゲーム要素を入れるために自分で改変しましたが、ネットで調べながら行ったので、なんとなしかコード内容を理解できてません

3

レイアウトを修正し忘れていたので修正

2021/11/18 07:58

投稿

SanaeToyoda
SanaeToyoda

スコア1

title CHANGED
File without changes
body CHANGED
@@ -210,83 +210,72 @@
210
210
  ```java
211
211
  <?xml version="1.0" encoding="utf-8"?>
212
212
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
213
- xmlns:app="http://schemas.android.com/apk/res-auto"
213
+ xmlns:app="http://schemas.android.com/apk/res-auto"
214
- xmlns:tools="http://schemas.android.com/tools"
214
+ xmlns:tools="http://schemas.android.com/tools"
215
- android:layout_width="match_parent"
215
+ android:layout_width="match_parent"
216
- android:layout_height="match_parent"
216
+ android:layout_height="match_parent"
217
- tools:context=".WaikikipuzzleActivity">
217
+ tools:context=".WaikikipuzzleActivity">
218
218
 
219
- <LinearLayout
219
+ <LinearLayout
220
- android:layout_width="match_parent"
220
+ android:layout_width="match_parent"
221
- android:layout_height="match_parent"
221
+ android:layout_height="match_parent"
222
- android:orientation="vertical">
222
+ android:orientation="vertical">
223
223
 
224
- <androidx.appcompat.widget.Toolbar
224
+ <androidx.appcompat.widget.Toolbar
225
- android:id="@+id/ac_toolbar_toolbar"
225
+ android:id="@+id/ac_toolbar_toolbar"
226
- android:layout_width="match_parent"
226
+ android:layout_width="match_parent"
227
- android:layout_alignParentTop="true"
227
+ android:layout_alignParentTop="true"
228
- app:popupTheme="@style/PopupMenu"
228
+ app:popupTheme="@style/PopupMenu"
229
- android:layout_height="wrap_content"
229
+ android:layout_height="wrap_content"
230
- android:background="?attr/colorPrimary" />
230
+ android:background="?attr/colorPrimary" />
231
231
 
232
- <LinearLayout
232
+ <LinearLayout
233
- android:layout_width="match_parent"
233
+ android:layout_width="match_parent"
234
- android:layout_height="match_parent"
234
+ android:layout_height="match_parent"
235
- android:orientation="horizontal">
235
+ android:orientation="horizontal">
236
236
 
237
237
 
238
- <ImageView
238
+ <ImageView
239
- android:id="@+id/waikikiphoto"
239
+ android:id="@+id/waikikiphoto"
240
- android:layout_width="0dp"
240
+ android:layout_width="0dp"
241
- android:layout_height="wrap_content"
241
+ android:layout_height="wrap_content"
242
- android:layout_weight="3"
242
+ android:layout_weight="3"
243
- android:background="#add8e6"
243
+ android:background="#add8e6"
244
- android:layout_marginRight="10dp"
244
+ android:layout_marginRight="10dp"
245
- android:layout_gravity="center_horizontal"
245
+ android:layout_gravity="center_horizontal"
246
- android:src="@drawable/waikikibeach"/>
246
+ android:src="@drawable/waikikibeach"/>
247
247
 
248
- <com.toyocli.hawaii_travel_puzzle.GameView
248
+ <com.toyocli.hawaii_travel_puzzle.GameView
249
- android:id="@+id/game_view"
249
+ android:id="@+id/game_view"
250
- android:background="#77998855"
250
+ android:background="#77998855"
251
- android:layout_below="@id/ac_toolbar_toolbar"
251
+ android:layout_below="@id/ac_toolbar_toolbar"
252
- android:layout_width="0dp"
252
+ android:layout_width="0dp"
253
- android:layout_weight="5"
253
+ android:layout_weight="5"
254
- android:layout_height="match_parent"
254
+ android:layout_height="match_parent"
255
- android:layout_marginTop="10dp"
255
+ android:layout_marginTop="10dp"
256
- android:layout_marginBottom="10dp"/>
256
+ android:layout_marginBottom="10dp"/>
257
257
 
258
- <ImageButton
258
+ <ImageButton
259
- android:id="@+id/waikikipuzzle_giveup"
259
+ android:id="@+id/waikikipuzzle_giveup"
260
- android:layout_width="0dp"
260
+ android:layout_width="0dp"
261
- android:layout_height="wrap_content"
261
+ android:layout_height="wrap_content"
262
- android:layout_weight="1"
262
+ android:layout_weight="1"
263
- android:layout_gravity="bottom"
263
+ android:layout_gravity="bottom"
264
- android:background="#00000000"
264
+ android:background="#00000000"
265
- android:src="@drawable/givebutun25"/>
265
+ android:src="@drawable/givebutun25"/>
266
266
 
267
267
 
268
268
 
269
269
 
270
270
 
271
- </LinearLayout>
271
+ </LinearLayout>
272
272
 
273
+ </LinearLayout>
273
274
 
274
275
 
275
-
276
-
277
- </LinearLayout>
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
276
  </RelativeLayout>
287
277
 
288
278
 
289
-
290
279
  ```
291
280
 
292
281
 

2

なるべくやりたいことがわかるようにコードを出しましたが

2021/11/18 07:51

投稿

SanaeToyoda
SanaeToyoda

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  0. 番号リスト### 前提・実現したいこと
2
- Activityから値をviewに渡し、各レイアウトページごとに違う画像を表示したいのですが、初期値の画像しか表示されません。
2
+ Activityから値をviewに渡し、各レイアウトページごとに違う画像を表示したいのですが、photo=0;の画像しか表示されません。
3
3
  Activityの
4
4
  Gameview.setphoto(photo)の部分が違うのか?
5
5
  Viewクラスの
@@ -24,114 +24,186 @@
24
24
  ```java
25
25
  public class WaikikipuzzleActivity extends AppCompatActivity {
26
26
 
27
- private GameView gameView;
27
+ private GameView gameView;
28
- int level = 3;
28
+ int level = 3;
29
- private int photo = 1;
29
+ private int photo = 1;
30
30
 
31
31
 
32
- private InterstitialAd mInterstitialAd;
32
+ private InterstitialAd mInterstitialAd;
33
- AdRequest adRequest_interstitial;
33
+ AdRequest adRequest_interstitial;
34
- String AdMobID = "ca-app-pub-3940256099942544/1033173712";
34
+ String AdMobID = "ca-app-pub-3940256099942544/1033173712";
35
35
 
36
36
 
37
37
 
38
- @Override
38
+ @Override
39
- protected void onCreate(Bundle savedInstanceState) {
39
+ protected void onCreate(Bundle savedInstanceState) {
40
- super.onCreate(savedInstanceState);
40
+ super.onCreate(savedInstanceState);
41
- setContentView(R.layout.activity_waikikipuzzle);
41
+ setContentView(R.layout.activity_waikikipuzzle);
42
42
 
43
43
 
44
+ 省略
44
45
 
45
- //全画面表示
46
- View decor = getWindow().getDecorView();
47
- decor.setSystemUiVisibility(
48
- View.SYSTEM_UI_FLAG_LAYOUT_STABLE
49
- | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
50
- | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
51
- | View.SYSTEM_UI_FLAG_FULLSCREEN
52
- | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
53
- | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
54
46
 
47
+ Toolbar toolbar = (Toolbar) findViewById(R.id.ac_toolbar_toolbar);
55
48
 
49
+ setSupportActionBar(toolbar);
50
+ gameView = (GameView) this.findViewById(R.id.game_view);
51
+ gameView.setPhoto(photo);
52
+ gameView.setOnFinishListener(new GameView.OnFinishListener(){
53
+ @Override
54
+ public void onFinish() {
55
+ Toast.makeText(WaikikipuzzleActivity.this, "完成!", Toast.LENGTH_LONG).show();
56
+ dialog2.show();
56
57
 
57
- //ダイアログ表示
58
- DisplayMetrics metrics = getResources().getDisplayMetrics();
59
- int dialogWidth2 = (int)(metrics.widthPixels * 0.9);
60
58
 
61
- final AppCompatDialog dialog2 = new AppCompatDialog(this);
59
+ }
62
60
 
63
- dialog2.setContentView(R.layout.waikikipuzzleclear);
61
+ });
64
62
 
65
- WindowManager.LayoutParams lp2 = dialog2.getWindow().getAttributes(); // コンテンツを追加したあと、showの前に記述
63
+ 省略
66
- lp2.width = dialogWidth2;
64
+ ```
67
- dialog2.getWindow().setAttributes(lp2);
68
65
 
69
- Toolbar toolbar = (Toolbar) findViewById(R.id.ac_toolbar_toolbar);
70
66
 
71
- setSupportActionBar(toolbar);
72
- gameView = (GameView) this.findViewById(R.id.game_view);
73
- gameView.setPhoto(photo);
74
- gameView.setOnFinishListener(new GameView.OnFinishListener(){
75
- @Override
67
+ ○view
68
+ ```java
76
- public void onFinish() {
69
+ public class GameView extends View {
77
- Toast.makeText(WaikikipuzzleActivity.this, "完成!", Toast.LENGTH_LONG).show();
78
- dialog2.show();
79
70
 
71
+ private Context mycontext;
72
+ private Random random = new Random();
73
+ private int level = 3;//レベル(初期値は3分割)
74
+ private int padding = 3;
75
+ private int photo;
76
+ private Resources mResources;
77
+ private Bitmap mBitmap;
78
+ private Bitmap mBitmap1;
79
+ private ArrayList<Patch> patches;
80
+ private int viewH;//viewの高さ
81
+ private int viewW;//viewの幅
82
+ private int canvasH;//1つのピースを格納するキャンバスの高さ
83
+ private int canvasW;//幅
84
+ private int bitmapH;//1つのピースの高さ
85
+ private int bitmapW;//幅
80
86
 
87
+ private SoundPool soundPool;//おと
88
+ private int s_move;//移動OK
89
+ private int s_error;//移動NG
81
- }
90
+ private OnFinishListener listener;
82
91
 
83
- });
84
- ```
85
92
 
93
+ int[] mImageResources = { R.drawable.waikikibeach, R.drawable.diyamondophoto};
86
94
 
87
- ○view
88
- ```java
89
- public class GameView extends View {
95
+ public void setPhoto(int photo) {
96
+ this.photo = photo;
97
+ mResources = getResources();
98
+ mBitmap1 = BitmapFactory.decodeResource(mResources, mImageResources[photo]);
90
99
 
91
- private Context mycontext;
92
- private Random random = new Random();
93
- private int level = 3;//默认分3层
94
- private int padding = 3;
95
- private int photo;
100
+ }
96
- private Resources mResources;
97
- private Bitmap mBitmap;
98
- private ArrayList<Patch> patches;
99
- private int viewH;//view的高度
100
- private int viewW;//view的宽度
101
- private int canvasH;//一格画布的高度
102
- private int canvasW;//一格画布的宽度
103
- private int bitmapH;//一片图片的高度
104
- private int bitmapW;//一片图片的宽度
105
101
 
106
- private SoundPool soundPool;//声音池
107
- private int s_move;//移动声音
108
- private int s_error;//不能移动声音
109
- private OnFinishListener listener;
102
+ public GameView(Context context){
103
+ this(context, null);
104
+ }
105
+ public GameView(Context context, AttributeSet attrs) {
110
106
 
107
+ super(context, attrs);
108
+ mycontext = context;
109
+ mResources = getResources();
110
+ mBitmap = ((BitmapDrawable)mResources.getDrawable(mImageResources[photo])).getBitmap();
111
+ bitmapW = mBitmap.getWidth()/level;
112
+ bitmapH = mBitmap.getHeight()/level;
113
+ initPatches();
114
+ initSound();
115
+ }
111
116
 
117
+ //レベルで分割を変える
118
+ public void setLevel(int level){
119
+ this.level = level;
120
+ bitmapW = mBitmap.getWidth()/level;
112
- int[] mImageResources = { R.drawable.waikikibeach, R.drawable.diyamondophoto};
121
+ bitmapH = mBitmap.getHeight()/level;
122
+ canvasH = viewH/level;
123
+ canvasW = viewW/level;
124
+ initPatches();
125
+ invalidate();
126
+ }
113
127
 
128
+ //ピースの設置
114
- public void setPhoto(int photo) {
129
+ public void setmBitmap(Bitmap bitmap){
115
- this.photo = photo;
130
+ this.mBitmap = bitmap;
116
- invalidate();
131
+ invalidate();
132
+ }
117
133
 
134
+ public void setOnFinishListener(OnFinishListener listener){
135
+ this.listener = listener;
118
- }
136
+ }
119
137
 
138
+ //正方形の設定
139
+ @Override
140
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
141
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
142
+ int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
143
+ int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
144
+ int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
120
- public GameView(Context context){
145
+ if(heightSpecMode == MeasureSpec.AT_MOST){
121
- this(context, null);
146
+ setMeasuredDimension(widthSpecSize,widthSpecSize);
122
- }
147
+ }
123
- public GameView(Context context, AttributeSet attrs) {
148
+ }
124
149
 
150
+ @Override
151
+ protected void onDraw(Canvas canvas) {
125
- super(context, attrs);
152
+ super.onDraw(canvas);
126
- mycontext = context;
127
- mResources = getResources();
153
+ drawAllPic(patches, canvas);
128
- mBitmap = ((BitmapDrawable)mResources.getDrawable(mImageResources[photo])).getBitmap();
129
- bitmapW = mBitmap.getWidth()/level;
130
- bitmapH = mBitmap.getHeight()/level;
131
- initPatches();
132
- initSound();
133
- }
134
154
 
155
+ }
156
+
157
+ @Override
158
+ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
159
+ super.onSizeChanged(w, h, oldw, oldh);
160
+ viewW = w;
161
+ viewH = h;
162
+ canvasW = w/level;
163
+ canvasH = h/level;
164
+ }
165
+
166
+ private void initSound(){
167
+ //クリック時の音
168
+ soundPool=new SoundPool(2, AudioManager.STREAM_MUSIC,0);
169
+ s_move = soundPool.load(mycontext,R.raw.s_move,1);
170
+ s_error = soundPool.load(mycontext,R.raw.s_error,1);
171
+ }
172
+
173
+ private void initPatches(){
174
+ patches = new ArrayList<Patch>();
175
+ Patch emptyPatch = null;
176
+ for(int i = 0;i < level;i++){
177
+ for(int j = 0;j < level;j++){
178
+ Patch patch = new Patch();
179
+
180
+ PicPoint bp = new PicPoint();
181
+ bp.setX(i);
182
+ bp.setY(j);
183
+
184
+ PicPoint cp = new PicPoint();
185
+ cp.setX(i);
186
+ cp.setY(j);
187
+
188
+ patch.setBitmapPicPoint(bp);
189
+ patch.setCanvasPicPoint(cp);
190
+ if(bp.getX() == (level - 1) && bp.getY() == (level - 1)){
191
+ patch.setEmpty(true);
192
+ emptyPatch = patch;
193
+ }
194
+ patches.add(patch);
195
+ }
196
+ }
197
+
198
+ for(int i = 0;i < 50*level;i++){
199
+ emptyPatch = exchange(emptyPatch);
200
+ }
201
+
202
+ }
203
+
204
+
205
+ 以下省略
206
+
135
207
  ```
136
208
 
137
209
  ○レイアウト

1

2021/11/18 07:44

投稿

SanaeToyoda
SanaeToyoda

スコア1

title CHANGED
File without changes
body CHANGED
@@ -21,11 +21,7 @@
21
21
 
22
22
  ### 該当のソースコード
23
23
 
24
- ```Android Java
24
+ ```java
25
- ソースコード
26
-
27
- ○Activity
28
-
29
25
  public class WaikikipuzzleActivity extends AppCompatActivity {
30
26
 
31
27
  private GameView gameView;
@@ -58,7 +54,7 @@
58
54
 
59
55
 
60
56
 
61
- //完成時のダイアログ表示と使う画像の指定
57
+ //ダイアログ表示
62
58
  DisplayMetrics metrics = getResources().getDisplayMetrics();
63
59
  int dialogWidth2 = (int)(metrics.widthPixels * 0.9);
64
60
 
@@ -85,12 +81,11 @@
85
81
  }
86
82
 
87
83
  });
84
+ ```
88
85
 
89
86
 
90
- 以下省略
91
-
92
- Viewクラス
87
+ view
93
-
88
+ ```java
94
89
  public class GameView extends View {
95
90
 
96
91
  private Context mycontext;
@@ -137,10 +132,10 @@
137
132
  initSound();
138
133
  }
139
134
 
140
- 以下省略
135
+ ```
141
136
 
142
-
143
- Waikikipuzzleのレイアウトxml
137
+ ○レイアウト
138
+ ```java
144
139
  <?xml version="1.0" encoding="utf-8"?>
145
140
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
146
141
  xmlns:app="http://schemas.android.com/apk/res-auto"
@@ -179,7 +174,6 @@
179
174
  android:src="@drawable/waikikibeach"/>
180
175
 
181
176
  <com.toyocli.hawaii_travel_puzzle.GameView
182
-
183
177
  android:id="@+id/game_view"
184
178
  android:background="#77998855"
185
179
  android:layout_below="@id/ac_toolbar_toolbar"
@@ -221,7 +215,10 @@
221
215
 
222
216
 
223
217
 
218
+ ```
224
219
 
220
+
221
+
225
222
  ### 試したこと
226
223
 
227
224
  いきなり、gameview.setphoto(photo)とせず、