HOME > Android > 定期処理 Handler

Android -定期処理 Handler-

Handlerサンプル1(Handlerクラスを使用)

Handlerクラスを利用して、定期処理サンプルです。
イメージが(0,0)の地点から、45度に徐々に拡大しながら前進し、画面サイズまで来ると同じ角度で 後退しながら、縮小されていきます。

イメージファイルが別途必要です。(今回は、droid80.png(80×80)を使用しています。)
resフォルダ内にdrawableフォルダを作成後、ご用意されたイメージファイルを配置して下さい。

HandlerEx.java

  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.content.res.Resources;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.graphics.Rect;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.view.View;
  13. public class HandlerEx extends Activity{
  14. private TickHandler tHandler;
  15. private HandlerView hView;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. hView=new HandlerView(this);
  20. setContentView(hView);
  21. }
  22. @Override
  23. protected void onPause() {
  24. super.onPause();
  25. //ハンドラの停止
  26. tHandler=null;
  27. }
  28. @Override
  29. protected void onResume() {
  30. super.onResume();
  31. //ハンドラの開始
  32. tHandler=new TickHandler();
  33. tHandler.sleep(0);
  34. }
  35. //定期処理 Handlerクラス
  36. public class TickHandler extends Handler{
  37. //メッセージを受信することで起動
  38. @Override
  39. public void handleMessage(Message msg) {
  40. //再描画
  41. hView.invalidate();
  42. if(tHandler !=null){
  43. //sleepメソッドの呼び出し(定期的にメッセージを送るため)
  44. tHandler.sleep(100);
  45. }
  46. super.handleMessage(msg);
  47. }
  48. public void sleep(long delayMillis){
  49. //使用済メッセージを削除
  50. removeMessages(0);
  51. //新しいメッセージを取得して指定時間後にメッセージを送る
  52. sendMessageDelayed(obtainMessage(0), delayMillis);
  53. }
  54. }
  55. }
  56. class HandlerView extends View{
  57. Bitmap bmp;
  58. private int move_point=0;
  59. private boolean flag=true;
  60. public HandlerView(Context context) {
  61. super(context);
  62. setBackgroundColor(Color.WHITE);
  63. Resources r=context.getResources();
  64. bmp=BitmapFactory.decodeResource(r, R.drawable.droid80);
  65. }
  66. @Override
  67. protected void onDraw(Canvas canvas) {
  68. int w=bmp.getWidth();
  69. int h=bmp.getHeight();
  70. int harf_w=getWidth()/2-(h/2);
  71. if(move_point<harf_w && flag){
  72. move_point++;
  73. if(move_point==harf_w){
  74. flag=false;
  75. }
  76. }
  77. if(!flag){
  78. move_point--;
  79. if(move_point ==0){
  80. flag=true;
  81. }
  82. }
  83. Rect src =new Rect(0,0,w,h);
  84. Rect dst=new Rect(move_point, move_point, move_point*2+w, move_point*2+h);
  85. canvas.drawBitmap(bmp, src, dst,null);
  86. }
  87. }

Handlerサンプル2(Runnabe)

Runnabeでの定期処理のサンプルです。
タッチするとその位置に移動してきます。
(ついて回るって感じです。)

HandlerEx2.java

  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.content.res.Resources;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. public class HandlerEx2 extends Activity implements Runnable{
  13. private Handler handler;
  14. private HandlerView2 hView;
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. hView=new HandlerView2(this);
  19. setContentView(hView);
  20. }
  21. @Override
  22. protected void onPause() {
  23. super.onPause();
  24. handler.removeCallbacks(this);
  25. handler=null;
  26. }
  27. @Override
  28. protected void onResume() {
  29. super.onResume();
  30. handler=new Handler();
  31. handler.postDelayed(this, 100);
  32. }
  33. @Override
  34. public void run() {
  35. hView.invalidate();
  36. if(handler!=null){
  37. handler.postDelayed(this, 100);
  38. }
  39. }
  40. }
  41. class HandlerView2 extends View{
  42. private int touchX=0; //タッチX座標
  43. private int touchY=0; //タッチY座標
  44. private Bitmap bmp;
  45. public HandlerView2(Context context) {
  46. super(context);
  47. setBackgroundColor(Color.WHITE);
  48. setFocusable(true);
  49. setFocusableInTouchMode(true);
  50. Resources r=context.getResources();
  51. bmp=BitmapFactory.decodeResource(r, R.drawable.droid80);
  52. }
  53. @Override
  54. protected void onDraw(Canvas canvas) {
  55. if(touchX==0 && touchY==0){
  56. canvas.drawBitmap(bmp, 0, 0,null);
  57. }else{
  58. canvas.drawBitmap(bmp, touchX-(bmp.getWidth()/2),touchY-(bmp.getHeight()/2),null);
  59. }
  60. }
  61. @Override
  62. public boolean onTouchEvent(MotionEvent event) {
  63. touchX=(int)event.getX();
  64. touchY=(int)event.getY();
  65. return true;
  66. }
  67. }
Copyright © 2018   MitoRoid  All Rights Reserved.