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