#前提、実現したいこと
画面に四角形にテキストボックスを4つ表示しその中にcanvasを配置しています(xmlファイルに記述)。またcanvas上をボールが動き壁(画面の端のテキストボックス)に当たったらそこのテキストを変更するということをしたいです。
#当該のソースコード
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/wood" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > //ここのtextViewの文字列を変更したいです。 <TextView android:layout_width="match_parent" android:layout_height="20dp" android:background="#CCffffff" android:id="@+id/bar_top" /> <LinearLayout android:layout_width="match_parent" android:layout_height="470dp" android:orientation="horizontal"> <TextView android:layout_width="20dp" android:layout_height="470dp" android:background="#CCffffff" android:id="@+id/bar_left" /> <com.example.yyoshida.mygameprot.SimulationView android:id="@+id/canvas" android:layout_width="345dp" android:layout_height="470dp" /> <TextView android:layout_width="20dp" android:layout_height="470dp" android:background="#CCffffff" android:id="@+id/bar_right" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="20dp" android:background="#CCffffff" android:id="@+id/bar_bottom" /> </LinearLayout> </RelativeLayout>
該当部
lang
1public class SimulationView extends SurfaceView implements SurfaceHolder.Callback { 2 private SurfaceHolder holder; 3 private Bitmap backGroundBitmap; 4 private Paint myPaint; 5 private Ball ball; 6 private int windowsWidth, windowsHeight; 7 private TextView textView; 8 private MainActivity main; 9 10//コンストラクタ 11 public SimulationView(Context c) { 12 super(c); 13 init(); 14 } 15 16 public SimulationView(Context context, AttributeSet attrs) { 17 super(context, attrs); 18 init(); 19 } 20 21 public SimulationView(Context context, AttributeSet attrs, int defStyleAttr) { 22 super(context, attrs, defStyleAttr); 23 init(); 24 } 25 26 public void init() { 27 holder = getHolder(); 28 holder.addCallback(this); 29 ball = new Ball(); 30 setFocusable(true); 31 requestFocus(); 32 main = new MainActivity(); 33 } 34 35 public void update() {//当たり判定とボールの移動 36 //右壁 37 if (x < 0) { 38 checkBar = 1; 39 main.countUpBarText(checkBar); 40 dx = -dx; 41 } 42 //左壁 43 if (x + ballWidth > windowsWidth) { 44 checkBar = 2; 45 main.countUpBarText(checkBar); 46 dx = -dx; 47 } 48 //上壁 49 if (y < 0) { 50 checkBar = 3; 51 main.countUpBarText(checkBar); 52 dy = -dy; 53 } 54 //下壁 55 if (y + ballHeight > windowsHeight) { 56 checkBar = 4; 57 main.countUpBarText(checkBar); 58 dy = -dy; 59 } 60 x += dx; 61 y += dy; 62 } 63 }
lang
1 2public class MainActivity extends AppCompatActivity { 3 private TextView textView; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 setBarText(); 10 } 11 12 13 public void setBarText() {//textviewの文字を設定 14 int number = 0; 15 String text = ""; 16 17 for (int key = 1; key < 5; key++) { 18 if (key == 1) { 19 textView = findViewById(R.id.bar_left); 20 } else if (key == 2) { 21 textView = findViewById(R.id.bar_right); 22 } else if (key == 3) { 23 textView = findViewById(R.id.bar_top); 24 } else { 25 textView = findViewById(R.id.bar_bottom); 26 } 27//最初にランダムでテキスト設定 28 number = (int) (Math.random() * 10); 29 text = String.valueOf(number); 30 textView.setText(text); 31 } 32 } 33 34 35//壁に当たった時に呼ばれる 36 public void countUpBarText(int key){//textviewを変更する 37 String barText = ""; 38 int countUp = 0; 39 Log.d("test","test"); 40//ここまでは実行される 41 if(key == 1){ 42 textView = findViewById(R.id.bar_left); 43 }else if(key == 2){ 44 textView = findViewById(R.id.bar_right); 45 }else if(key == 3){ 46 textView = findViewById(R.id.bar_top); 47 }else { 48 textView = findViewById(R.id.bar_bottom); 49 } 50//ここは実行されず止まってしまう 51 Log.d("test","test2"); 52 barText = (String)textView.getText(); 53 //countUp = Integer.getInteger(barText);エラーがでる。 54 countUp++; 55 56 textView = findViewById(R.id.bar_right); 57 textView.setText("String.valueOf(countUp)"); 58 } 59} 60
#試したこと
setTextを使っての変更(止まってしまう)
ボールが壁に当たったタイミングでtextviewを変更できるようにできませんか?
java android ともに初心者で変なことをしているかもしれませんがどなたか教えていただけませんか。
#追加
このようにupdateを実行しています。
@Override public void surfaceCreated(SurfaceHolder holder) { windowsWidth = getWidth(); windowsHeight = getHeight(); draw(); startnow(); } public void draw(){ Canvas c = getHolder().lockCanvas(); //背景の描画 backGroundBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.wood); //bitmap大きさ設定 backGroundBitmap = Bitmap.createScaledBitmap(backGroundBitmap, windowsWidth, windowsHeight, false); c.drawBitmap(backGroundBitmap, 0, 0, myPaint); //ボールの描画 c.drawBitmap(ball.myBitmap, ball.x, ball.y, myPaint); holder.unlockCanvasAndPost(c); } public void startnow(){ ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { ball.update(); Log.d("test", "ループ"); draw(); } }, 100, 100, TimeUnit.MICROSECONDS); }
#構成
public class SimulationView{ class Ball { public void update() {} } }
のようになっています。
回答1件
あなたの回答
tips
プレビュー