Android浮动图片处理 FloatView

public class FloatView extends ImageView
{
   
    private float x , y , mTouchStartX , mTouchStartY;

    private int VIEW_HEIGHT = 25;
   
    private WindowManager windowManager;
   
    /*
     * 此wmParams为获取的全局变量,用以保存悬浮窗口的属性
     */
    private WindowManager.LayoutParams wmParams;

    /**
     * constructed function
     *
     * @param Context context,
     *        WindowManager manager ,
     *        WindowManager.LayoutParams params
     * @throws
     * @since Tiantian
     */
    public FloatView(Context context , WindowManager Wmanager , WindowManager.LayoutParams params)
    {
        super(context);   
        windowManager = Wmanager;
        wmParams = params;
    }

/**
  *
  * @param event
  * @return boolean
  */
@Override
public boolean onTouchEvent(MotionEvent event)
{
    
     /*
      * 获取相对屏幕的坐标,即以屏幕左上角为原点
      */
     x = event.getRawX();  
    
     /*
      * 系统状态栏的高度
      */
     y = event.getRawY()- VIEW_HEIGHT;
    
     CommonUtil.log("currX" + x + "====currY" + y);
    
     switch (event.getAction())
     {
             /*
              * 捕获手指触摸按下动作
              */
        case MotionEvent.ACTION_DOWN:
           
            /*
             * 获取相对View的坐标,即以此View左上角为原点
             */
        mTouchStartX =  event.getX(); 
                mTouchStartY =  event.getY();
               
            CommonUtil.log("startX" + mTouchStartX + "====startY" + mTouchStartY);
           
            break;
       
            /*
             * 捕获手指触摸移动动作
             */
        case MotionEvent.ACTION_MOVE:
           
            updateViewPosition();
           
            break;

            /*
             * 捕获手指触摸离开动作
             */
        case MotionEvent.ACTION_UP: 
           
        updateViewPosition();
        mTouchStartX = mTouchStartY=0;
       
        break;
       
        }
    
        return true;
       
}

/**
  * 更新浮动窗口位置
  *
  * @param
  * @return void
  * @throws
  * @since TianTian
  */
private void updateViewPosition()
{
    
     /*
     * 更新浮动窗口位置参数
     */
wmParams.x = (int)( x - mTouchStartX);
wmParams.y = (int) (y - mTouchStartY);

windowManager.updateViewLayout(this , wmParams);

}

}

你可能感兴趣的:(floatView)