diff --git a/app/src/main/java/org/ntlab/acanthus_client/views/animation/AnimationActivity.java b/app/src/main/java/org/ntlab/acanthus_client/views/animation/AnimationActivity.java index cdfab58..9b5de20 100644 --- a/app/src/main/java/org/ntlab/acanthus_client/views/animation/AnimationActivity.java +++ b/app/src/main/java/org/ntlab/acanthus_client/views/animation/AnimationActivity.java @@ -3,33 +3,33 @@ import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; +import android.view.View; +import android.widget.Button; + +import org.ntlab.acanthus_client.R; +import org.ntlab.acanthus_client.databinding.ActivityPaintBinding; // 表示ページ public class AnimationActivity extends AppCompatActivity { private AnimationCanvas animationCanvas; + private ActivityPaintBinding binding; + private View view; //animationCanvasの表示 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.activity_animation); - animationCanvas = new AnimationCanvas(this); - setContentView(animationCanvas); - } - - //タイマーの停止 - @Override - protected void onPause(){ - super.onPause(); - animationCanvas.onPouse(); - } - - //再生(タイマーの生成) - @Override - protected void onResume(){ - super.onResume(); - animationCanvas.onResume(); + animationCanvas = this.findViewById(R.id.animationMyCanvas); + Button button = findViewById(R.id.button_playback); + button.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View v) { + animationCanvas.onClickPlayback(); + } + }); } } \ No newline at end of file diff --git a/app/src/main/java/org/ntlab/acanthus_client/views/animation/AnimationCanvas.java b/app/src/main/java/org/ntlab/acanthus_client/views/animation/AnimationCanvas.java index 72c38c8..8106117 100644 --- a/app/src/main/java/org/ntlab/acanthus_client/views/animation/AnimationCanvas.java +++ b/app/src/main/java/org/ntlab/acanthus_client/views/animation/AnimationCanvas.java @@ -3,71 +3,73 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; - +import android.os.Handler; +import android.os.Looper; +import android.util.AttributeSet; import android.view.View; - import org.ntlab.acanthus_client.Acanthus; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import retrofit2.Retrofit; +import java.util.Timer; +import java.util.TimerTask; public class AnimationCanvas extends View { private Acanthus acanthus; private AnimationViewModel animationViewModel = new AnimationViewModel(); private Paint paint = new Paint(); - private ScheduledExecutorService ses = null; + + //タイマー用の変数 + private Timer timer; + private CountUpTimerTask timerTask; + private Handler handler = new Handler(Looper.getMainLooper()); //ページ指定用の変数 private int pageNo = 0; - //ページ進ませる - private final Runnable task = new Runnable(){ - @Override - public void run(){ - pageNo++; - postInvalidate(); - } - }; - - public AnimationCanvas(Context context) { - super(context); + public AnimationCanvas(Context context, AttributeSet attrs) { + super(context, attrs); } - //ダミーです。 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); + //ダミーです。 //100msごとに1pxずつ3つ目の四角が下に降りる canvas.drawRect(100, 100, 200, 300, paint); canvas.drawRect(300, 200, 400, 400, paint); canvas.drawRect(500, 100 + pageNo, 600, 300 + pageNo, paint); - /* - //線の描写 - paint.setStrokeWidth(20); - paint.setColor(Color.RED); - float pts[] = {100, 1000, 500, 100, 500, 100, 900, 1000, 900, 1000, 250, 450, 250, 450, 800, 450}; - canvas.drawLines(pts, paint); -*/ } - //タイマーの生成 - public void onResume(){ - //タイマーの作成 - ses = Executors.newSingleThreadScheduledExecutor(); - //タイマーを100msごとにRunnableの処理を実行 - ses.scheduleAtFixedRate(task, 0L, 100L, TimeUnit.MILLISECONDS); + public void onClickPlayback(){ + if(timer != null){ + //timerの終了 + timer.cancel(); + timer = null; + } + else{ + //timerの生成 + this.timer = new Timer(); + this.timerTask = new CountUpTimerTask(); + this.timer.schedule(timerTask,0,100);//スケジュールを100ms毎に設定する。 + } } - //タイマーを停止する - public void onPouse(){ - ses.shutdown(); - ses = null; + //ページを進める + class CountUpTimerTask extends TimerTask{ + @Override + public void run(){ + handler.post(new Runnable(){ + public void run(){ + pageNo++; + postInvalidate(); + } + }); + } } + + + } diff --git a/app/src/main/res/layout/activity_animation.xml b/app/src/main/res/layout/activity_animation.xml index e9aedc5..7d9c320 100644 --- a/app/src/main/res/layout/activity_animation.xml +++ b/app/src/main/res/layout/activity_animation.xml @@ -6,13 +6,23 @@ android:layout_height="match_parent" tools:context=".views.animation.AnimationActivity"> - + +