HOME > Android > 定期処理 Handler

Android -定期処理 Handler-

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

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

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

HandlerEx.java

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

public class HandlerEx extends Activity{
    private TickHandler tHandler;
    private HandlerView hView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hView=new HandlerView(this);
        setContentView(hView);
    }

  @Override
  protected void onPause() {
    super.onPause();
    //ハンドラの停止
    tHandler=null;
  }

  @Override
  protected void onResume() {
    super.onResume();
    //ハンドラの開始
    tHandler=new TickHandler();
    tHandler.sleep(0);
  }

  //定期処理 Handlerクラス
  public class TickHandler extends Handler{
    //メッセージを受信することで起動
    @Override
    public void handleMessage(Message msg) {
      //再描画
      hView.invalidate();

      if(tHandler !=null){
        //sleepメソッドの呼び出し(定期的にメッセージを送るため)
        tHandler.sleep(100);
      }

      super.handleMessage(msg);
    }
    public void sleep(long delayMillis){
      //使用済メッセージを削除
      removeMessages(0);
      //新しいメッセージを取得して指定時間後にメッセージを送る
      sendMessageDelayed(obtainMessage(0), delayMillis);
    }
  }
}

class HandlerView extends View{
  Bitmap bmp;
  private int move_point=0;
  private boolean flag=true;

  public HandlerView(Context context) {
    super(context);
    setBackgroundColor(Color.WHITE);

    Resources r=context.getResources();
    bmp=BitmapFactory.decodeResource(r, R.drawable.droid80);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    int w=bmp.getWidth();
    int h=bmp.getHeight();
    int harf_w=getWidth()/2-(h/2);

    if(move_point<harf_w && flag){
      move_point++;
      if(move_point==harf_w){
        flag=false;
      }
    }

    if(!flag){
      move_point--;
      if(move_point ==0){
        flag=true;
      }
    }
    Rect src =new Rect(0,0,w,h);
    Rect dst=new Rect(move_point, move_point, move_point*2+w, move_point*2+h);
    canvas.drawBitmap(bmp, src, dst,null);
  }
}

Handlerサンプル2(Runnabe)

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

HandlerEx2.java

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

public class HandlerEx2 extends Activity implements Runnable{
    private Handler handler;
    private HandlerView2 hView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hView=new HandlerView2(this);
        setContentView(hView);
    }

  @Override
  protected void onPause() {
    super.onPause();
    handler.removeCallbacks(this);
    handler=null;
  }

  @Override
  protected void onResume() {
    super.onResume();
    handler=new Handler();
    handler.postDelayed(this, 100);
  }

  @Override
  public void run() {
    hView.invalidate();
    if(handler!=null){
      handler.postDelayed(this, 100);
    }
  }
}

class HandlerView2 extends View{
  private int touchX=0; //タッチX座標
  private int touchY=0; //タッチY座標
  private Bitmap bmp;

  public HandlerView2(Context context) {
    super(context);
    setBackgroundColor(Color.WHITE);
    setFocusable(true);
    setFocusableInTouchMode(true);

    Resources r=context.getResources();
    bmp=BitmapFactory.decodeResource(r, R.drawable.droid80);
  }

  @Override
  protected void onDraw(Canvas canvas) {

    if(touchX==0 && touchY==0){
      canvas.drawBitmap(bmp, 0, 0,null);
    }else{
      canvas.drawBitmap(bmp, touchX-(bmp.getWidth()/2),touchY-(bmp.getHeight()/2),null);
    }
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    touchX=(int)event.getX();
    touchY=(int)event.getY();
     return true;
  }
}
Copyright © 2018   MitoRoid  All Rights Reserved.