画面に四角を描画し、描画の座標位置を変えて再描画することで四角が動いているように見せるのが目的です。
canvas.drawRect(x,y,width,height)でいうところのyの初期位置は250としており、yを50ずつ足していき、yが500の地点まで
四角を下にずらしながら描画していこうと考えました。
しかし、実行したとき実際に画面に描画されるのは一度だけでした。
デバッグをかけたところ、myView.invalidate()の部分は通過していましたが、
onDrawが呼び出されたのはwhile文を抜けた後の一度だけで、yの値は550となっていました。
なぜループ内でのyの値が250~500のときのinvalidate()で描画が行なわれていないのか、ご教授頂きたく存じます。
以下、コードになります。
MainActivity.java
package com.example.falltest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyView myView = (MyView) findViewById(R.id.my_view); boolean flg = true; //無限ループ while (true) { //現在位置を取得 myView.y = myView.youAreHere(); //最終到達座標(500)を超えていれば終了 flg = myView.isDownMove(myView.y); if (flg == false) { return; } //yに50加算することで移動させる myView.Move(); //再描画 myView.invalidate(); //処理を遅らせる DelayTimer(); } } //処理を遅らせる public void DelayTimer() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
MyView
package com.example.falltest; 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.falltest.MainActivity"> //パッケージ名+Viewクラス名とすることでButtonやらTextViewと同じように使える <com.example.falltest.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>
ちなみに、handlerを使用した場合、四角を下方向に動かすという目的は達成できました。
MainActivity
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() { int count = 0; boolean flg = true; String counter = ""; @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>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/25 04:59
2018/10/26 00:59