android开发 模仿鼠标 遥控器 上下左右

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class Mouseview extends View {

   public float currentX1 = 0;
   public float currentY1 = 0;
   public float x1 = 0;
   public float y1 = 0;
   public float x2 = 0;
   public float y2 = 0;
   public float radios = 0;
   private Bitmap bitmap;
   private OnSlidingDirectionListener onSlidingDirectionListener;

   public Mouseview(Context context) {
      super(context);
   }

   public Mouseview(Context context, AttributeSet attrs) {
      super(context, attrs);
   }

   public Mouseview(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
   }

   @Override
   protected void onDraw(Canvas canvas) {
//    Paint p = new Paint();
//    p.setColor(Color.RED);
//    if (bitmap != null) {
//       int height = bitmap.getHeight();
//       int width = bitmap.getWidth();
//       //位置画在图片的中心
//       canvas.drawBitmap(bitmap, currentX1 - height / 2, currentY1 - width / 2, p);
//    }
   }

   @Override
   public boolean onTouchEvent(MotionEvent event) {
      switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:
            //可以设置个图标  暂时不要
//          bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_remote);
            this.currentX1 = event.getX();
            this.currentY1 = event.getY();

            this.x1 = event.getX();
            this.y1 = event.getY();
//          invalidate();
            break;
         case MotionEvent.ACTION_MOVE:
            this.currentX1 = event.getX();
            this.currentY1 = event.getY();
//          invalidate();
            break;
         case MotionEvent.ACTION_UP:
            //计算是横向滑动还是纵向滑动
            x2 = event.getX();
            y2 = event.getY();
            float distanceY = Math.abs(Math.abs(y1) - Math.abs(y2));
            float distanceX = Math.abs(Math.abs(x1) - Math.abs(x2));
            if (y1 - y2 > 30 && distanceY > distanceX) {
               System.out.println("---------------->向上滑");
               onSlidingDirectionListener.up();
            } else if (y2 - y1 > 30 && distanceY > distanceX) {
               System.out.println("---------------->向下滑");
               onSlidingDirectionListener.down();
            } else if (x1 - x2 > 30 && distanceY < distanceX) {
               System.out.println("---------------->向左滑");
               onSlidingDirectionListener.left();
            } else if (x2 - x1 > 30 && distanceY < distanceX) {
               System.out.println("---------------->向右滑");
               onSlidingDirectionListener.right();
            } else {
               onSlidingDirectionListener.ok();
            }
            //回收bitmap
//          bitmap.recycle();
//          bitmap = null;
//          invalidate();
            break;
      }
      return true;
   }

   public void setMoveListener(OnSlidingDirectionListener onSlidingDirectionListener) {
      this.onSlidingDirectionListener = onSlidingDirectionListener;
   }

   public interface OnSlidingDirectionListener {

      void up();

      void down();

      void left();

      void right();

      void ok();
   }
}

你可能感兴趣的:(android开发 模仿鼠标 遥控器 上下左右)