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

質問編集履歴

1

項目名の修正、コードの追加

2017/01/22 12:57

投稿

ko20vonobird
ko20vonobird

スコア50

title CHANGED
File without changes
body CHANGED
@@ -1,38 +1,245 @@
1
1
  ###実現したいこと
2
2
  タイトルの通りなのですが、アクティビティを継承しているクラスの中にビューを継承しているクラスがあり、そのクラスから外側のクラスで測った画面の幅の定数を取得したいのです。
3
3
 
4
- ###発生している問題・エラーメッセージ
4
+ ###発生している問題
5
5
  なぜか値が0になります。また、下記の試したことで書いたようにインスタンスを作成してインスタンス.定数;としてみたのですが、うまくいきませんでした。
6
6
 
7
7
  ###該当のソースコード
8
+ 全文
8
- ```Java
9
+ ```java
10
+ package com.gmail.archerygame;
11
+
12
+ //import ゾーン
13
+
9
14
  public class Main extends Activity {
15
+ //コンストラクタ的ゾーン
16
+ private TextView title;
10
- private int winWM, winHM;
17
+ private int winWM, winHM;
18
+ private int winWMl, winHMl;
19
+ private int a, r, g, b, cnt;
20
+ private int sw = 0;
21
+ private double kaku;
22
+ private float titley;
23
+ private float titleh;
24
+ private int deltay;
25
+ private double rad;
26
+ private Handler mHandler;
27
+ private Runnable titleloop;
28
+ private Handler stHandler = new Handler();
29
+ private Runnable st;
30
+ private Handler cbHandler;
31
+ private Runnable circlebreaking;
32
+ private Handler cbrunHandler = new Handler();
33
+ private Runnable circlemake;
34
+ Ct cta;
35
+ float s = 37.0f;
36
+ public int getWinWM(){//##########
37
+ return winWM;
38
+ }
39
+ public int getWinHM(){//##########
40
+ return winHM;
41
+ }
11
- @Override
42
+ @Override
12
- protected void onCreate(Bundle savedInstanceState) {
43
+ protected void onCreate(Bundle savedInstanceState) {
13
- super.onCreate(savedInstanceState);
44
+ super.onCreate(savedInstanceState);
45
+ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
46
+ //setContentView(R.layout.activity_main);
47
+ //ステータスバー非表示
48
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
49
+ //タイトルバー非表示
50
+ requestWindowFeature(Window.FEATURE_NO_TITLE);
14
- }
51
+ }
15
- public void onResume() {
52
+ public void onResume() {
16
- super.onResume();
53
+ super.onResume();
54
+ if (mHandler != null){
55
+ mHandler.postDelayed(titleloop, 1000);
56
+ }
57
+ if (stHandler != null){
58
+ stHandler.postDelayed(st, 1000);
59
+ }
60
+ if (cbHandler != null){
61
+ cbHandler.postDelayed(circlebreaking, 1000);
62
+ }
63
+ if (cbrunHandler != null){
64
+ cbrunHandler.postDelayed(circlemake, 1000);
65
+ }
66
+ cta = new Ct(this);
17
- parts();
67
+ parts();
18
- }
68
+ }
19
- public void parts() {
69
+ public void parts() {
20
- WindowManager wmM = (WindowManager)getSystemService(WINDOW_SERVICE);
70
+ WindowManager wmM = (WindowManager)getSystemService(WINDOW_SERVICE);
21
71
  Display dpM = wmM.getDefaultDisplay();
22
72
  Point poM = new Point();
23
73
  dpM.getSize(poM);
74
+ winWM = poM.x;
75
+ winHM = poM.y;
76
+ FrameLayout fl = new FrameLayout(this);
77
+ this.setContentView(fl);
78
+ fl.addView(cta);
79
+ final LinearLayout ll = new LinearLayout(this);
80
+ ll.setOrientation(LinearLayout.VERTICAL);
81
+ fl.addView(ll);
82
+ title = new TextView(this);
83
+ title.setText("N!ce・Sl!ngshot");
84
+ title.setTextSize(17.0f);
85
+ title.setTypeface(Typeface.SERIF);
86
+ float titlewidth = title.getCompoundPaddingLeft() + title.getCompoundPaddingRight() + Layout.getDesiredWidth(title.getText(),title.getPaint());
87
+ float titlemiddle = titlewidth;
88
+ int titlewhere = (int)((winWM / 2) - (titlemiddle / 2));
89
+ float titlewheref = (float)titlewhere;
90
+ title.setTranslationX(titlewheref);
91
+ ll.addView(title);
92
+ title.setVisibility(View.GONE);
93
+ st = new Runnable() {
94
+ @Override
95
+ public void run() {
96
+ stHandler.removeCallbacks(st);
97
+ looper();
98
+ }
99
+ };
100
+ stHandler.postDelayed(st, 3000);
101
+ }
102
+ public void looper() {
103
+ WindowManager wmMl = (WindowManager)getSystemService(WINDOW_SERVICE);
104
+ Display dpMl = wmMl.getDefaultDisplay();
105
+ Point poMl = new Point();
106
+ dpMl.getSize(poMl);
24
- final int winWM = poM.x;
107
+ final int winWMl = poMl.x;
25
- final int winHM = poM.y;
108
+ final int winHMl = poMl.y;
109
+ mHandler = new Handler();
110
+ a = 35;
111
+ r = 0;
112
+ g = 0;
113
+ b = 0;
114
+ cnt = 0;
115
+ kaku = 0;
116
+ titley = winWMl - titleh;
117
+ titleloop = new Runnable() {
118
+ @Override
119
+ public void run() {
120
+ title.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
121
+ titleh = title.getCompoundPaddingTop() + title.getCompoundPaddingBottom() + title.getMeasuredHeight();
122
+ float titlewidthcb = title.getCompoundPaddingLeft() + title.getCompoundPaddingRight() + Layout.getDesiredWidth(title.getText(),title.getPaint());
123
+ float titlewherecb = (winWMl / 2) - (titlewidthcb / 2);
124
+ title.setTranslationX(titlewherecb);
125
+ title.setTranslationY(titley);
126
+ title.setTypeface(Typeface.SERIF);
127
+ switch(title.getVisibility()){
128
+ case View.VISIBLE:
129
+ break;
130
+ case View.GONE:
131
+ title.setVisibility(View.VISIBLE);
132
+ break;
133
+ }
134
+ s = s + 0.4f;
135
+ kaku = -1 * cnt * 7.2;
136
+ rad = kaku * Math.PI / 180;
137
+ int deltay = (int)(winHMl * Math.sin(rad));
138
+ titley = winHMl + deltay;
139
+ title.setTextSize(s);
140
+ title.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
141
+ titleh = title.getCompoundPaddingTop() + title.getCompoundPaddingBottom() + title.getMeasuredHeight();
142
+ float titlewidthc = title.getCompoundPaddingLeft() + title.getCompoundPaddingRight() + Layout.getDesiredWidth(title.getText(),title.getPaint());
143
+ float titlewherec = (winWMl / 2) - (titlewidthc / 2);
144
+ title.setTranslationX(titlewherec);
145
+ title.setTranslationY(titley);
146
+ if (titley + titleh > winHMl) {
147
+ titley = winHMl - titleh;
148
+ title.setTranslationY(titley);
149
+ }
150
+ title.setTypeface(Typeface.SERIF);
151
+ title.setTextColor(Color.argb(a, r, g, b));
152
+ a = a + 4;
153
+ mHandler.postDelayed(titleloop, 15);
154
+ if (s >= 59.0f) {
155
+ mHandler.removeCallbacks(titleloop);
156
+ //次のまで数秒のハンドラ作成
157
+ cbHandler = new Handler();
158
+ circlebreaking = new Runnable() {
159
+ @Override
160
+ public void run() {
161
+ cb();
162
+ }
163
+ };
164
+ cbHandler.postDelayed(circlebreaking, 1500);
165
+ }
166
+ cnt = cnt + 1;
167
+ }
168
+ };
169
+ mHandler.postDelayed(titleloop, 15);
170
+ }
171
+ public void cb() {
172
+ cbHandler.removeCallbacks(circlebreaking);
173
+ Paint cipa = new Paint();
174
+ circlemake = new Runnable() {
175
+ @Override
176
+ public void run() {
177
+ //円のアニメーション描画
178
+ sw = 1;
179
+ cta.endraw();
180
+ cbrunHandler.removeCallbacks(circlemake);
181
+ }
182
+ };
183
+ cbrunHandler.postDelayed(circlemake, 15);
184
+ }
185
+ public class Ct extends View {
186
+ float gw = getWinWM();//##########
187
+ float gh = getWinHM();//##########
188
+ private float r1 = gw + 20;
189
+ private float cx = gw / 2;
190
+ private float cy = (gh / 2) - (gh / 6);
191
+ public Ct(Context context){
192
+ super(context);
193
+ }
194
+ protected void onDraw(Canvas canvas){
195
+ super.onDraw(canvas);
196
+ Paint pam = new Paint();
197
+ if (sw == 1){
198
+ //ここにすべての処理を書いていく↓
199
+ pam.setColor(Color.argb(255, 0, 0, 0));
200
+ pam.setStyle(Paint.Style.STROKE);
201
+ pam.setStrokeWidth(3);
202
+ canvas.drawCircle(cx, cy, r1, pam);
203
+ //ここにすべての処理を書いていく↑
204
+ }
205
+ }
206
+ public void endraw(){
207
+ invalidate();
208
+ }
209
+ }
210
+ public void onPause() {
211
+ super.onPause();
212
+ if (mHandler != null){
213
+ mHandler.removeCallbacks(titleloop);
214
+ }
215
+ if (stHandler != null){
216
+ stHandler.removeCallbacks(st);
217
+ }
218
+ if (cbHandler != null){
219
+ cbHandler.removeCallbacks(circlebreaking);
220
+ }
221
+ if (cbrunHandler != null) {
222
+ cbrunHandler.removeCallbacks(circlemake);
223
+ }
224
+ }
225
+ public void onDestroy() {
226
+ super.onDestroy();
227
+ if (mHandler != null){
228
+ mHandler.removeCallbacks(titleloop);
229
+ }
230
+ if (stHandler != null){
231
+ stHandler.removeCallbacks(st);
232
+ }
233
+ if (cbHandler != null){
234
+ cbHandler.removeCallbacks(circlebreaking);
235
+ }
236
+ if (cbrunHandler != null){
237
+ cbrunHandler.removeCallbacks(circlemake);
238
+ }
239
+ }
26
240
  }
27
- public class Ct extends View {
241
+
28
- Main ma = new Main();
29
- private float r1 = ma.winWM + 20;
30
- private float cx = winWM / 2;
31
- private float cy = (winHM / 2) - (winHM / 6);
32
- }
33
- }
34
242
  ```
35
- こんな感じです
243
+ 問題個所に//##########を付けました
36
-
37
244
  ###試したこと
38
245
  デバッグして値を確かめたり、外側のクラスのインスタンスを作成してそれを用いて参照できないか試みました。