未命名  

程式碼下載

 

 

package com.android.weather.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class MovingPictureView extends View implements Runnable{
 float downXValue;
 long downTime;
 private float lastTouchX, lastTouchY;
 private boolean hasMoved = false;

 Bitmap bitmap;


 public static boolean isRuning = true;


 int left = 100;
 int top = 20;


 Handler handler;
 MoveHandler movehandler;
 int sleepSeconds;
 int goBackType;

 
 int dx = 1;
 int dy = 1;
 
 public int index;
 public Thread moving;
 public boolean isstarted = false;

 
 public MovingPictureView(Context context,int resource,int left,int top,int sleepSeconds) {
  super(context);
  this.left = left;
  this.top = top;
  this.sleepSeconds = sleepSeconds;
  this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
  bitmap = BitmapFactory.decodeResource(getResources(), resource);
  handler = new Handler();
  movehandler = new MoveHandler();
  //new Thread(this).start();
 }

 
 public void move(){
  moving = new Thread(this);
  System.out.println("mov1.bitmap");
  moving.start();
 } 
 
 @Override
 protected void onDraw(Canvas canvas) {
 
  if(bitmap!=null){
   canvas.drawBitmap(bitmap, left, top, null);
  }
 }

 @Override
 public boolean onTouchEvent(MotionEvent evt) {
 
  
  boolean consumed = super.onTouchEvent(evt);
  if (isClickable()) {
   switch (evt.getAction()) {

   case MotionEvent.ACTION_DOWN:
    lastTouchX = evt.getX();
    lastTouchY = evt.getY();
    downXValue = evt.getX();
    downTime = evt.getEventTime();
    hasMoved = false;
    break;

   case MotionEvent.ACTION_MOVE:
    hasMoved = moved(evt);
    break;

   case MotionEvent.ACTION_UP:
    float currentX = evt.getX();
    long currentTime = evt.getEventTime();

    break;
   }

  }

  return consumed || isClickable();
  
  //return true;
 }
 
 @Override
 public void run() {
  isstarted = true;
  while (MovingPictureView.isRuning) {
 
//   dx = left < 0 || left > (getWidth() - bitmap.getWidth()) ? -dx : dx;
//   dy = top < 0 || top > (getHeight() - bitmap.getHeight()) ? -dy : dy;
//   left = left + dx;
//   top = top + dy;
   
   if((bitmap!=null) && (left > (getWidth()))){
    left = - bitmap.getWidth();
   }

   left = left + dx;
 
//   handler.post(new Runnable() {
//    @Override
//    public void run() {
//     invalidate();
//    }
//   });
   movehandler.sendMessage(handler.obtainMessage());

   try {
    Thread.sleep(sleepSeconds);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
 
 private boolean moved(MotionEvent evt) {

  return hasMoved || Math.abs(evt.getX() - lastTouchX) > 10.0
    || Math.abs(evt.getY() - lastTouchY) > 10.0;
 }
 
 public class MoveHandler extends Handler{
  @Override
        public void handleMessage(Message msg) {
   MovingPictureView.this.invalidate();
  }
 }
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()