質問編集履歴

6

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

2021/11/19 01:12

投稿

SanaeToyoda
SanaeToyoda

スコア1

test CHANGED
File without changes
test CHANGED
@@ -402,11 +402,45 @@
402
402
 
403
403
  }
404
404
 
405
+
406
+
407
+ 省略
408
+
409
+
410
+
411
+
412
+
413
+ //画像を分割してピースを作る
414
+
415
+ private void drawPic(Canvas canvas,Patch patch){
416
+
417
+ if(patch.isEmpty()){
418
+
419
+ return;
420
+
421
+ }
422
+
423
+ Rect rb = new Rect(patch.getBitmapPicPoint().getX()*bitmapW + padding, patch.getBitmapPicPoint().getY()*bitmapH + padding,
424
+
425
+ (patch.getBitmapPicPoint().getX()+1)*bitmapW - padding, (patch.getBitmapPicPoint().getY()+1)*bitmapH - padding);
426
+
427
+ Rect rc = new Rect(patch.getCanvasPicPoint().getX()*canvasW + padding,patch.getCanvasPicPoint().getY()*canvasH + padding,
428
+
429
+ (patch.getCanvasPicPoint().getX()+1)*canvasW - padding, (patch.getCanvasPicPoint().getY()+1)*canvasH - padding);
430
+
431
+
432
+
433
+ canvas.drawBitmap(mBitmap, rb, rc, null);
434
+
435
+ }
436
+
437
+
438
+
405
439
 
406
440
 
407
-
408
-
409
- 以下省略
441
+ 以下省略
442
+
443
+
410
444
 
411
445
 
412
446
 
@@ -414,141 +448,63 @@
414
448
 
415
449
 
416
450
 
451
+
452
+
453
+
454
+
455
+
456
+
417
- ○レイアウト
457
+ ### 試したこと
458
+
459
+
460
+
461
+ GameViewを以下のように変更してみましたが、画像が表示されませんでした。
418
462
 
419
463
  ```java
420
464
 
421
- <?xml version="1.0" encoding="utf-8"?>
422
-
423
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
424
-
425
- xmlns:app="http://schemas.android.com/apk/res-auto"
426
-
427
- xmlns:tools="http://schemas.android.com/tools"
428
-
429
- android:layout_width="match_parent"
430
-
431
- android:layout_height="match_parent"
432
-
433
- tools:context=".WaikikipuzzleActivity">
434
-
435
-
436
-
437
- <LinearLayout
438
-
439
- android:layout_width="match_parent"
440
-
441
- android:layout_height="match_parent"
442
-
443
- android:orientation="vertical">
444
-
445
-
446
-
447
- <androidx.appcompat.widget.Toolbar
448
-
449
- android:id="@+id/ac_toolbar_toolbar"
450
-
451
- android:layout_width="match_parent"
452
-
453
- android:layout_alignParentTop="true"
454
-
455
- app:popupTheme="@style/PopupMenu"
456
-
457
- android:layout_height="wrap_content"
458
-
459
- android:background="?attr/colorPrimary" />
460
-
461
-
462
-
463
- <LinearLayout
464
-
465
- android:layout_width="match_parent"
466
-
467
- android:layout_height="match_parent"
468
-
469
- android:orientation="horizontal">
470
-
471
-
472
-
473
-
474
-
475
- <ImageView
476
-
477
- android:id="@+id/waikikiphoto"
478
-
479
- android:layout_width="0dp"
480
-
481
- android:layout_height="wrap_content"
482
-
483
- android:layout_weight="3"
484
-
485
- android:background="#add8e6"
486
-
487
- android:layout_marginRight="10dp"
488
-
489
- android:layout_gravity="center_horizontal"
490
-
491
- android:src="@drawable/waikikibeach"/>
492
-
493
-
494
-
495
- <com.toyocli.hawaii_travel_puzzle.GameView
496
-
497
- android:id="@+id/game_view"
498
-
499
- android:background="#77998855"
500
-
501
- android:layout_below="@id/ac_toolbar_toolbar"
502
-
503
- android:layout_width="0dp"
504
-
505
- android:layout_weight="5"
506
-
507
- android:layout_height="match_parent"
508
-
509
- android:layout_marginTop="10dp"
510
-
511
- android:layout_marginBottom="10dp"/>
512
-
513
-
514
-
515
- <ImageButton
516
-
517
- android:id="@+id/waikikipuzzle_giveup"
518
-
519
- android:layout_width="0dp"
520
-
521
- android:layout_height="wrap_content"
522
-
523
- android:layout_weight="1"
524
-
525
- android:layout_gravity="bottom"
526
-
527
- android:background="#00000000"
528
-
529
- android:src="@drawable/givebutun25"/>
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
- </LinearLayout>
542
-
543
-
544
-
545
- </LinearLayout>
546
-
547
-
548
-
549
-
550
-
551
- </RelativeLayout>
465
+ int[] mImageResources = { R.drawable.waikikibeach, R.drawable.diyamondophoto};
466
+
467
+
468
+
469
+ public void setPhoto(int photo) {
470
+
471
+ this.photo = photo;
472
+
473
+ mResources = getResources();
474
+
475
+ mBitmap1 = BitmapFactory.decodeResource(mResources, mImageResources[photo]);
476
+
477
+
478
+
479
+ }
480
+
481
+
482
+
483
+ public GameView(Context context){
484
+
485
+ this(context, null);
486
+
487
+ }
488
+
489
+ public GameView(Context context, AttributeSet attrs) {
490
+
491
+
492
+
493
+ super(context, attrs);
494
+
495
+ mycontext = context;
496
+
497
+ mResources = getResources();
498
+
499
+ mBitmap = ((BitmapDrawable)mResources.getDrawable(mImageResources[photo])).getBitmap();
500
+
501
+ bitmapW = mBitmap.getWidth()/level;
502
+
503
+ bitmapH = mBitmap.getHeight()/level;
504
+
505
+ initPatches();
506
+
507
+ initSound();
552
508
 
553
509
 
554
510
 
@@ -562,70 +518,6 @@
562
518
 
563
519
 
564
520
 
565
- ### 試したこと
566
-
567
-
568
-
569
- GameViewを以下のように変更してみましたが、画像が表示されませんでした。
570
-
571
- ```java
572
-
573
- int[] mImageResources = { R.drawable.waikikibeach, R.drawable.diyamondophoto};
574
-
575
-
576
-
577
- public void setPhoto(int photo) {
578
-
579
- this.photo = photo;
580
-
581
- mResources = getResources();
582
-
583
- mBitmap1 = BitmapFactory.decodeResource(mResources, mImageResources[photo]);
584
-
585
-
586
-
587
- }
588
-
589
-
590
-
591
- public GameView(Context context){
592
-
593
- this(context, null);
594
-
595
- }
596
-
597
- public GameView(Context context, AttributeSet attrs) {
598
-
599
-
600
-
601
- super(context, attrs);
602
-
603
- mycontext = context;
604
-
605
- mResources = getResources();
606
-
607
- mBitmap = ((BitmapDrawable)mResources.getDrawable(mImageResources[photo])).getBitmap();
608
-
609
- bitmapW = mBitmap.getWidth()/level;
610
-
611
- bitmapH = mBitmap.getHeight()/level;
612
-
613
- initPatches();
614
-
615
- initSound();
616
-
617
-
618
-
619
-
620
-
621
- ```
622
-
623
-
624
-
625
-
626
-
627
-
628
-
629
521
  ### 補足情報(FW/ツールのバージョンなど)
630
522
 
631
523
 

5

追記です。

2021/11/19 01:12

投稿

SanaeToyoda
SanaeToyoda

スコア1

test CHANGED
File without changes
test CHANGED
@@ -42,7 +42,7 @@
42
42
 
43
43
  ### 該当のソースコード
44
44
 
45
-
45
+ *waikikiActivity(本来は、photo=0ですが、画像が変わるかをチェックするため、あえて”1”にしています。
46
46
 
47
47
  ```java
48
48
 

4

追記です。

2021/11/18 08:01

投稿

SanaeToyoda
SanaeToyoda

スコア1

test CHANGED
File without changes
test CHANGED
@@ -566,19 +566,61 @@
566
566
 
567
567
 
568
568
 
569
- いきなり、gameview.setphoto(photo)とず、
569
+ GameViewを以下のように変更してみましたが、画像が表示されまんでした。
570
+
570
-
571
+ ```java
572
+
573
+ int[] mImageResources = { R.drawable.waikikibeach, R.drawable.diyamondophoto};
574
+
575
+
576
+
571
- Public void setPhoto(int photo)
577
+ public void setPhoto(int photo) {
572
-
573
-
574
-
575
-  photo=1;
578
+
576
-
577
-  gameview.setPhoto(photo);
579
+ this.photo = photo;
580
+
578
-
581
+ mResources = getResources();
582
+
583
+ mBitmap1 = BitmapFactory.decodeResource(mResources, mImageResources[photo]);
584
+
585
+
586
+
579
-
587
+ }
588
+
589
+
590
+
580
-
591
+ public GameView(Context context){
592
+
593
+ this(context, null);
594
+
595
+ }
596
+
597
+ public GameView(Context context, AttributeSet attrs) {
598
+
599
+
600
+
601
+ super(context, attrs);
602
+
603
+ mycontext = context;
604
+
605
+ mResources = getResources();
606
+
607
+ mBitmap = ((BitmapDrawable)mResources.getDrawable(mImageResources[photo])).getBitmap();
608
+
609
+ bitmapW = mBitmap.getWidth()/level;
610
+
611
+ bitmapH = mBitmap.getHeight()/level;
612
+
581
- と書いてみましたが、ダメでした。
613
+ initPatches();
614
+
615
+ initSound();
616
+
617
+
618
+
619
+
620
+
621
+ ```
622
+
623
+
582
624
 
583
625
 
584
626
 
@@ -588,4 +630,4 @@
588
630
 
589
631
 
590
632
 
591
- ここより詳細情報記載しください。
633
+ GameViewは、昔、プロの方作成してもらったコードで、今回、ゲーム要素を入れるために自分で改変しましたが、ネットで調べがら行ったので、なんとなくしかコード内容理解できていません

3

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

2021/11/18 07:58

投稿

SanaeToyoda
SanaeToyoda

スコア1

test CHANGED
File without changes
test CHANGED
@@ -422,147 +422,127 @@
422
422
 
423
423
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
424
424
 
425
- xmlns:app="http://schemas.android.com/apk/res-auto"
426
-
427
- xmlns:tools="http://schemas.android.com/tools"
428
-
429
- android:layout_width="match_parent"
430
-
431
- android:layout_height="match_parent"
432
-
433
- tools:context=".WaikikipuzzleActivity">
434
-
435
-
436
-
437
- <LinearLayout
438
-
439
- android:layout_width="match_parent"
440
-
441
- android:layout_height="match_parent"
442
-
443
- android:orientation="vertical">
444
-
445
-
446
-
447
- <androidx.appcompat.widget.Toolbar
448
-
449
- android:id="@+id/ac_toolbar_toolbar"
450
-
451
- android:layout_width="match_parent"
452
-
453
- android:layout_alignParentTop="true"
454
-
455
- app:popupTheme="@style/PopupMenu"
456
-
457
- android:layout_height="wrap_content"
458
-
459
- android:background="?attr/colorPrimary" />
460
-
461
-
462
-
463
- <LinearLayout
464
-
465
- android:layout_width="match_parent"
466
-
467
- android:layout_height="match_parent"
468
-
469
- android:orientation="horizontal">
470
-
471
-
472
-
473
-
474
-
475
- <ImageView
476
-
477
- android:id="@+id/waikikiphoto"
478
-
479
- android:layout_width="0dp"
480
-
481
- android:layout_height="wrap_content"
482
-
483
- android:layout_weight="3"
484
-
485
- android:background="#add8e6"
486
-
487
- android:layout_marginRight="10dp"
488
-
489
- android:layout_gravity="center_horizontal"
490
-
491
- android:src="@drawable/waikikibeach"/>
492
-
493
-
494
-
495
- <com.toyocli.hawaii_travel_puzzle.GameView
496
-
497
- android:id="@+id/game_view"
498
-
499
- android:background="#77998855"
500
-
501
- android:layout_below="@id/ac_toolbar_toolbar"
502
-
503
- android:layout_width="0dp"
504
-
505
- android:layout_weight="5"
506
-
507
- android:layout_height="match_parent"
508
-
509
- android:layout_marginTop="10dp"
510
-
511
- android:layout_marginBottom="10dp"/>
512
-
513
-
514
-
515
- <ImageButton
516
-
517
- android:id="@+id/waikikipuzzle_giveup"
518
-
519
- android:layout_width="0dp"
520
-
521
- android:layout_height="wrap_content"
522
-
523
- android:layout_weight="1"
524
-
525
- android:layout_gravity="bottom"
526
-
527
- android:background="#00000000"
528
-
529
- android:src="@drawable/givebutun25"/>
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
- </LinearLayout>
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
- </LinearLayout>
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
425
+ xmlns:app="http://schemas.android.com/apk/res-auto"
426
+
427
+ xmlns:tools="http://schemas.android.com/tools"
428
+
429
+ android:layout_width="match_parent"
430
+
431
+ android:layout_height="match_parent"
432
+
433
+ tools:context=".WaikikipuzzleActivity">
434
+
435
+
436
+
437
+ <LinearLayout
438
+
439
+ android:layout_width="match_parent"
440
+
441
+ android:layout_height="match_parent"
442
+
443
+ android:orientation="vertical">
444
+
445
+
446
+
447
+ <androidx.appcompat.widget.Toolbar
448
+
449
+ android:id="@+id/ac_toolbar_toolbar"
450
+
451
+ android:layout_width="match_parent"
452
+
453
+ android:layout_alignParentTop="true"
454
+
455
+ app:popupTheme="@style/PopupMenu"
456
+
457
+ android:layout_height="wrap_content"
458
+
459
+ android:background="?attr/colorPrimary" />
460
+
461
+
462
+
463
+ <LinearLayout
464
+
465
+ android:layout_width="match_parent"
466
+
467
+ android:layout_height="match_parent"
468
+
469
+ android:orientation="horizontal">
470
+
471
+
472
+
473
+
474
+
475
+ <ImageView
476
+
477
+ android:id="@+id/waikikiphoto"
478
+
479
+ android:layout_width="0dp"
480
+
481
+ android:layout_height="wrap_content"
482
+
483
+ android:layout_weight="3"
484
+
485
+ android:background="#add8e6"
486
+
487
+ android:layout_marginRight="10dp"
488
+
489
+ android:layout_gravity="center_horizontal"
490
+
491
+ android:src="@drawable/waikikibeach"/>
492
+
493
+
494
+
495
+ <com.toyocli.hawaii_travel_puzzle.GameView
496
+
497
+ android:id="@+id/game_view"
498
+
499
+ android:background="#77998855"
500
+
501
+ android:layout_below="@id/ac_toolbar_toolbar"
502
+
503
+ android:layout_width="0dp"
504
+
505
+ android:layout_weight="5"
506
+
507
+ android:layout_height="match_parent"
508
+
509
+ android:layout_marginTop="10dp"
510
+
511
+ android:layout_marginBottom="10dp"/>
512
+
513
+
514
+
515
+ <ImageButton
516
+
517
+ android:id="@+id/waikikipuzzle_giveup"
518
+
519
+ android:layout_width="0dp"
520
+
521
+ android:layout_height="wrap_content"
522
+
523
+ android:layout_weight="1"
524
+
525
+ android:layout_gravity="bottom"
526
+
527
+ android:background="#00000000"
528
+
529
+ android:src="@drawable/givebutun25"/>
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+ </LinearLayout>
542
+
543
+
544
+
545
+ </LinearLayout>
566
546
 
567
547
 
568
548
 
@@ -574,8 +554,6 @@
574
554
 
575
555
 
576
556
 
577
-
578
-
579
557
  ```
580
558
 
581
559
 

2

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

2021/11/18 07:51

投稿

SanaeToyoda
SanaeToyoda

スコア1

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  0. 番号リスト### 前提・実現したいこと
2
2
 
3
- Activityから値をviewに渡し、各レイアウトページごとに違う画像を表示したいのですが、初期値の画像しか表示されません。
3
+ Activityから値をviewに渡し、各レイアウトページごとに違う画像を表示したいのですが、photo=0;の画像しか表示されません。
4
4
 
5
5
  Activityの
6
6
 
@@ -50,119 +50,79 @@
50
50
 
51
51
 
52
52
 
53
- private GameView gameView;
54
-
55
- int level = 3;
56
-
57
- private int photo = 1;
58
-
59
-
60
-
61
-
62
-
63
- private InterstitialAd mInterstitialAd;
64
-
65
- AdRequest adRequest_interstitial;
66
-
67
- String AdMobID = "ca-app-pub-3940256099942544/1033173712";
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
- @Override
76
-
77
- protected void onCreate(Bundle savedInstanceState) {
78
-
79
- super.onCreate(savedInstanceState);
80
-
81
- setContentView(R.layout.activity_waikikipuzzle);
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
- //全画面表示
90
-
91
- View decor = getWindow().getDecorView();
92
-
93
- decor.setSystemUiVisibility(
94
-
95
- View.SYSTEM_UI_FLAG_LAYOUT_STABLE
96
-
97
- | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
98
-
99
- | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
100
-
101
- | View.SYSTEM_UI_FLAG_FULLSCREEN
102
-
103
- | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
104
-
105
- | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
- //ダイアログ表示
114
-
115
- DisplayMetrics metrics = getResources().getDisplayMetrics();
116
-
117
- int dialogWidth2 = (int)(metrics.widthPixels * 0.9);
118
-
119
-
120
-
121
- final AppCompatDialog dialog2 = new AppCompatDialog(this);
122
-
123
-
124
-
125
- dialog2.setContentView(R.layout.waikikipuzzleclear);
126
-
127
-
128
-
129
- WindowManager.LayoutParams lp2 = dialog2.getWindow().getAttributes(); // コンテンツを追加したあと、showの前に記述
130
-
131
- lp2.width = dialogWidth2;
132
-
133
- dialog2.getWindow().setAttributes(lp2);
134
-
135
-
136
-
137
- Toolbar toolbar = (Toolbar) findViewById(R.id.ac_toolbar_toolbar);
138
-
139
-
140
-
141
- setSupportActionBar(toolbar);
142
-
143
- gameView = (GameView) this.findViewById(R.id.game_view);
144
-
145
- gameView.setPhoto(photo);
146
-
147
- gameView.setOnFinishListener(new GameView.OnFinishListener(){
148
-
149
- @Override
150
-
151
- public void onFinish() {
152
-
153
- Toast.makeText(WaikikipuzzleActivity.this, "完成!", Toast.LENGTH_LONG).show();
154
-
155
- dialog2.show();
156
-
157
-
158
-
159
-
160
-
161
- }
162
-
163
-
164
-
165
- });
53
+ private GameView gameView;
54
+
55
+ int level = 3;
56
+
57
+ private int photo = 1;
58
+
59
+
60
+
61
+
62
+
63
+ private InterstitialAd mInterstitialAd;
64
+
65
+ AdRequest adRequest_interstitial;
66
+
67
+ String AdMobID = "ca-app-pub-3940256099942544/1033173712";
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+ @Override
76
+
77
+ protected void onCreate(Bundle savedInstanceState) {
78
+
79
+ super.onCreate(savedInstanceState);
80
+
81
+ setContentView(R.layout.activity_waikikipuzzle);
82
+
83
+
84
+
85
+
86
+
87
+ 省略
88
+
89
+
90
+
91
+
92
+
93
+ Toolbar toolbar = (Toolbar) findViewById(R.id.ac_toolbar_toolbar);
94
+
95
+
96
+
97
+ setSupportActionBar(toolbar);
98
+
99
+ gameView = (GameView) this.findViewById(R.id.game_view);
100
+
101
+ gameView.setPhoto(photo);
102
+
103
+ gameView.setOnFinishListener(new GameView.OnFinishListener(){
104
+
105
+ @Override
106
+
107
+ public void onFinish() {
108
+
109
+ Toast.makeText(WaikikipuzzleActivity.this, "完成!", Toast.LENGTH_LONG).show();
110
+
111
+ dialog2.show();
112
+
113
+
114
+
115
+
116
+
117
+ }
118
+
119
+
120
+
121
+ });
122
+
123
+
124
+
125
+ 省略
166
126
 
167
127
  ```
168
128
 
@@ -178,93 +138,277 @@
178
138
 
179
139
 
180
140
 
181
- private Context mycontext;
182
-
183
- private Random random = new Random();
184
-
185
- private int level = 3;//默认3层
186
-
187
- private int padding = 3;
188
-
189
- private int photo;
190
-
191
- private Resources mResources;
192
-
193
- private Bitmap mBitmap;
194
-
195
- private ArrayList<Patch> patches;
196
-
197
- private int viewH;//view的高度
198
-
199
- private int viewW;//view的宽度
200
-
201
- private int canvasH;//一格画布的高度
202
-
203
- private int canvasW;//画布的宽度
204
-
205
- private int bitmapH;//一片图片的高度
206
-
207
- private int bitmapW;//一片图片的宽度
208
-
209
-
210
-
211
- private SoundPool soundPool;//声音池
212
-
213
- private int s_move;//移动声音
214
-
215
- private int s_error;//不能动声音
216
-
217
- private OnFinishListener listener;
218
-
219
-
220
-
221
-
222
-
223
- int[] mImageResources = { R.drawable.waikikibeach, R.drawable.diyamondophoto};
224
-
225
-
226
-
227
- public void setPhoto(int photo) {
228
-
229
- this.photo = photo;
230
-
231
- invalidate();
232
-
233
-
234
-
235
- }
236
-
237
-
238
-
239
- public GameView(Context context){
240
-
241
- this(context, null);
242
-
243
- }
244
-
245
- public GameView(Context context, AttributeSet attrs) {
246
-
247
-
248
-
249
- super(context, attrs);
250
-
251
- mycontext = context;
252
-
253
- mResources = getResources();
254
-
255
- mBitmap = ((BitmapDrawable)mResources.getDrawable(mImageResources[photo])).getBitmap();
256
-
257
- bitmapW = mBitmap.getWidth()/level;
258
-
259
- bitmapH = mBitmap.getHeight()/level;
260
-
261
- initPatches();
262
-
263
- initSound();
264
-
265
- }
266
-
267
-
141
+ private Context mycontext;
142
+
143
+ private Random random = new Random();
144
+
145
+ private int level = 3;//レベル(初期値は3割)
146
+
147
+ private int padding = 3;
148
+
149
+ private int photo;
150
+
151
+ private Resources mResources;
152
+
153
+ private Bitmap mBitmap;
154
+
155
+ private Bitmap mBitmap1;
156
+
157
+ private ArrayList<Patch> patches;
158
+
159
+ private int viewH;//viewの高さ
160
+
161
+ private int viewW;//viewの幅
162
+
163
+ private int canvasH;//1つのピースを納するキャンバスの高さ
164
+
165
+ private int canvasW;//
166
+
167
+ private int bitmapH;//1つのピースの高さ
168
+
169
+ private int bitmapW;//幅
170
+
171
+
172
+
173
+ private SoundPool soundPool;//おと
174
+
175
+ private int s_move;//移動OK
176
+
177
+ private int s_error;//移動NG
178
+
179
+ private OnFinishListener listener;
180
+
181
+
182
+
183
+
184
+
185
+ int[] mImageResources = { R.drawable.waikikibeach, R.drawable.diyamondophoto};
186
+
187
+
188
+
189
+ public void setPhoto(int photo) {
190
+
191
+ this.photo = photo;
192
+
193
+ mResources = getResources();
194
+
195
+ mBitmap1 = BitmapFactory.decodeResource(mResources, mImageResources[photo]);
196
+
197
+
198
+
199
+ }
200
+
201
+
202
+
203
+ public GameView(Context context){
204
+
205
+ this(context, null);
206
+
207
+ }
208
+
209
+ public GameView(Context context, AttributeSet attrs) {
210
+
211
+
212
+
213
+ super(context, attrs);
214
+
215
+ mycontext = context;
216
+
217
+ mResources = getResources();
218
+
219
+ mBitmap = ((BitmapDrawable)mResources.getDrawable(mImageResources[photo])).getBitmap();
220
+
221
+ bitmapW = mBitmap.getWidth()/level;
222
+
223
+ bitmapH = mBitmap.getHeight()/level;
224
+
225
+ initPatches();
226
+
227
+ initSound();
228
+
229
+ }
230
+
231
+
232
+
233
+ //レベルで分割を変える
234
+
235
+ public void setLevel(int level){
236
+
237
+ this.level = level;
238
+
239
+ bitmapW = mBitmap.getWidth()/level;
240
+
241
+ bitmapH = mBitmap.getHeight()/level;
242
+
243
+ canvasH = viewH/level;
244
+
245
+ canvasW = viewW/level;
246
+
247
+ initPatches();
248
+
249
+ invalidate();
250
+
251
+ }
252
+
253
+
254
+
255
+ //ピースの設置
256
+
257
+ public void setmBitmap(Bitmap bitmap){
258
+
259
+ this.mBitmap = bitmap;
260
+
261
+ invalidate();
262
+
263
+ }
264
+
265
+
266
+
267
+ public void setOnFinishListener(OnFinishListener listener){
268
+
269
+ this.listener = listener;
270
+
271
+ }
272
+
273
+
274
+
275
+ //正方形の設定
276
+
277
+ @Override
278
+
279
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
280
+
281
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
282
+
283
+ int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
284
+
285
+ int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
286
+
287
+ int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
288
+
289
+ if(heightSpecMode == MeasureSpec.AT_MOST){
290
+
291
+ setMeasuredDimension(widthSpecSize,widthSpecSize);
292
+
293
+ }
294
+
295
+ }
296
+
297
+
298
+
299
+ @Override
300
+
301
+ protected void onDraw(Canvas canvas) {
302
+
303
+ super.onDraw(canvas);
304
+
305
+ drawAllPic(patches, canvas);
306
+
307
+
308
+
309
+ }
310
+
311
+
312
+
313
+ @Override
314
+
315
+ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
316
+
317
+ super.onSizeChanged(w, h, oldw, oldh);
318
+
319
+ viewW = w;
320
+
321
+ viewH = h;
322
+
323
+ canvasW = w/level;
324
+
325
+ canvasH = h/level;
326
+
327
+ }
328
+
329
+
330
+
331
+ private void initSound(){
332
+
333
+ //クリック時の音
334
+
335
+ soundPool=new SoundPool(2, AudioManager.STREAM_MUSIC,0);
336
+
337
+ s_move = soundPool.load(mycontext,R.raw.s_move,1);
338
+
339
+ s_error = soundPool.load(mycontext,R.raw.s_error,1);
340
+
341
+ }
342
+
343
+
344
+
345
+ private void initPatches(){
346
+
347
+ patches = new ArrayList<Patch>();
348
+
349
+ Patch emptyPatch = null;
350
+
351
+ for(int i = 0;i < level;i++){
352
+
353
+ for(int j = 0;j < level;j++){
354
+
355
+ Patch patch = new Patch();
356
+
357
+
358
+
359
+ PicPoint bp = new PicPoint();
360
+
361
+ bp.setX(i);
362
+
363
+ bp.setY(j);
364
+
365
+
366
+
367
+ PicPoint cp = new PicPoint();
368
+
369
+ cp.setX(i);
370
+
371
+ cp.setY(j);
372
+
373
+
374
+
375
+ patch.setBitmapPicPoint(bp);
376
+
377
+ patch.setCanvasPicPoint(cp);
378
+
379
+ if(bp.getX() == (level - 1) && bp.getY() == (level - 1)){
380
+
381
+ patch.setEmpty(true);
382
+
383
+ emptyPatch = patch;
384
+
385
+ }
386
+
387
+ patches.add(patch);
388
+
389
+ }
390
+
391
+ }
392
+
393
+
394
+
395
+ for(int i = 0;i < 50*level;i++){
396
+
397
+ emptyPatch = exchange(emptyPatch);
398
+
399
+ }
400
+
401
+
402
+
403
+ }
404
+
405
+
406
+
407
+
408
+
409
+ 以下省略
410
+
411
+
268
412
 
269
413
  ```
270
414
 

1

2021/11/18 07:44

投稿

SanaeToyoda
SanaeToyoda

スコア1

test CHANGED
File without changes
test CHANGED
@@ -44,15 +44,7 @@
44
44
 
45
45
 
46
46
 
47
- ```Android Java
47
+ ```java
48
-
49
- ソースコード
50
-
51
-
52
-
53
- ○Activity
54
-
55
-
56
48
 
57
49
  public class WaikikipuzzleActivity extends AppCompatActivity {
58
50
 
@@ -118,7 +110,7 @@
118
110
 
119
111
 
120
112
 
121
- //完成時のダイアログ表示と使う画像の指定
113
+ //ダイアログ表示
122
114
 
123
115
  DisplayMetrics metrics = getResources().getDisplayMetrics();
124
116
 
@@ -172,17 +164,15 @@
172
164
 
173
165
  });
174
166
 
175
-
176
-
177
-
178
-
179
- 以下省略
167
+ ```
180
-
181
-
182
-
168
+
169
+
170
+
171
+
172
+
183
- Viewクラス
173
+ view
174
+
184
-
175
+ ```java
185
-
186
176
 
187
177
  public class GameView extends View {
188
178
 
@@ -276,13 +266,13 @@
276
266
 
277
267
 
278
268
 
279
- 以下省略
269
+ ```
280
-
281
-
282
-
283
-
284
-
270
+
271
+
272
+
285
- Waikikipuzzleのレイアウトxml
273
+ ○レイアウト
274
+
275
+ ```java
286
276
 
287
277
  <?xml version="1.0" encoding="utf-8"?>
288
278
 
@@ -360,8 +350,6 @@
360
350
 
361
351
  <com.toyocli.hawaii_travel_puzzle.GameView
362
352
 
363
-
364
-
365
353
  android:id="@+id/game_view"
366
354
 
367
355
  android:background="#77998855"
@@ -444,6 +432,12 @@
444
432
 
445
433
 
446
434
 
435
+ ```
436
+
437
+
438
+
439
+
440
+
447
441
 
448
442
 
449
443
  ### 試したこと