スレッド処理を初めて触るため、前提から誤っていることがあるかもしれませんが、都度ご指摘頂ければ幸いです。
後述するコードでは、四角を描画し、下へとずらして再描画することで四角を動かすといった処理を行なっています。
runメソッドでは、一定時間ごとに描画位置を変更し、再描画させています。
run内のflgがfalseになった時点でスレッドが終了する(四角の移動が終わる)のですが、スレッドの終了を検知して、別のスレッドを開始することは
できるのでしょうか。
とりあえずスレッドが終わったタイミングを掴みたいので、スレッド終了後にToastでも表示してみようと考え、デバッグで処理を追ったのですが、スレッドが終わった時点でアプリケーションが終了してしまい、どこにToastの記述をすればいいのか掴めていません。
お聞きしたい内容としては、
1.AとBというスレッドがあったとして、Aが終わったときにBを開始といったことはできるのか。
2.後述のコードで、runメソッド(スレッド?)が終わった後にToastを表示したい場合、
どこに記述すればいいいのか
3.後述のコードで
if (flg == false) { return; }
としているがこれでスレッドを終了させたことになるのか(四角の落下は止まるのですが、スレッドを終了させる場合、removeCallbacksを使うとあったので)
どれか1つでも構いません。ご教授頂ければ幸いです。
MainActivity.java
package com.example.drawmove; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final MyView myView = (MyView)findViewById(R.id.my_view); final Handler handler = new Handler(); final Runnable r = new Runnable() { boolean flg = true; @Override public void run() { // UIスレッド //yの現在地を取得 myView.y = myView.youAreHere(); //まだ動くかどうかの判定 flg = myView.isDownMove(myView.y); if (flg == false) { return; } //描画位置変更 myView.Move(); //再描画 myView.invalidate(); handler.postDelayed(this, 1000); } }; handler.post(r); } }
MyView.java
package com.example.drawmove; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paint = new Paint(); int x = 660; int y = 250; int width = 710; int height = 300; public MyView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { paint.setColor(Color.BLACK); paint.setStrokeWidth(1.0f); paint.setStyle(Paint.Style.FILL); canvas.drawRect(x,y,width,height, paint); } public void Move () { this.y = this.y + 50; this.height = this.height + 50; } public boolean isDownMove (int y) { boolean flg = true; if (y > 500) { flg = false; } return flg; } //yの現在の数値を返す public int youAreHere () { return this.y; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context="com.example.drawmove.MainActivity"> //パッケージ名+Viewクラス名とすることでButtonやらTextViewと同じように使える <com.example.drawmove.MyView android:id="@+id/my_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" /> </android.support.constraint.ConstraintLayout>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/25 07:17
2018/10/25 07:32