亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于SurfaceView實現(xiàn)可拖動視頻控件

 更新時間:2018年04月07日 13:50:10   作者:白少木丿  
這篇文章主要為大家詳細(xì)介紹了基于SurfaceView的可拖動視頻控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了基于SurfaceView的可拖動視頻控件,供大家參考,具體內(nèi)容如下

public class DragSurfaceView extends SurfaceView implements View.OnTouchListener {
 protected int screenWidth;
 protected int screenHeight;
 protected int lastX;
 protected int lastY;
 private int oriLeft;
 private int oriRight;
 private int oriTop;
 private int oriBottom;
 private int dragDirection;
 private static final int TOP = 0x15;
 private static final int LEFT = 0x16;
 private static final int BOTTOM = 0x17;
 private static final int RIGHT = 0x18;
 private static final int LEFT_TOP = 0x11;
 private static final int RIGHT_TOP = 0x12;
 private static final int LEFT_BOTTOM = 0x13;
 private static final int RIGHT_BOTTOM = 0x14;
 private static final int CENTER = 0x19;
 private int offset = 20;

 /**
  * 初始化獲取屏幕寬高
  */
 protected void initScreenW_H() {
  screenHeight = getResources().getDisplayMetrics().heightPixels - 40;
  screenWidth = getResources().getDisplayMetrics().widthPixels;
  Log.i("DragViewTAG", "DragSurfaceView.initScreenW_H: screenWidth="+screenWidth+", screenHeight="+screenHeight);
 }
 public DragSurfaceView(Context context) {
  super(context);
  setOnTouchListener(this);
  initScreenW_H();
 }

 public DragSurfaceView(Context context, AttributeSet attrs) {
  super(context, attrs);
  setOnTouchListener(this);
  initScreenW_H();
 }

 public DragSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  setOnTouchListener(this);
  initScreenW_H();
 }


 @Override
 public boolean onTouch(View v, MotionEvent event) {
  int action = event.getAction();
  if (action == MotionEvent.ACTION_DOWN) {
   oriLeft = v.getLeft();
   oriRight = v.getRight();
   oriTop = v.getTop();
   oriBottom = v.getBottom();
   lastY = (int) event.getRawY();
   lastX = (int) event.getRawX();
   dragDirection = getDirection(v, (int) event.getX(),
     (int) event.getY());
  }
  // 處理拖動事件
  delDrag(v, event, action);
  if(action==MotionEvent.ACTION_UP){
   dragDirection=0;
  }
  invalidate();
  return true;
 }


 /**
  * 獲取觸摸點flag
  *
  * @param v
  * @param x
  * @param y
  * @return
  */
 protected int getDirection(View v, int x, int y) {
  int left = v.getLeft();
  int right = v.getRight();
  int bottom = v.getBottom();
  int top = v.getTop();
  if (x < 40 && y < 40) {
   return LEFT_TOP;
  }
  if (y < 40 && right - left - x < 40) {
   return RIGHT_TOP;
  }
  if (x < 40 && bottom - top - y < 40) {
   return LEFT_BOTTOM;
  }
  if (right - left - x < 40 && bottom - top - y < 40) {
   return RIGHT_BOTTOM;
  }
  if (x < 40) {
   return LEFT;
  }
  if (y < 40) {
   return TOP;
  }
  if (right - left - x < 40) {
   return RIGHT;
  }
  if (bottom - top - y < 40) {
   return BOTTOM;
  }
  return CENTER;
 }

 /**
  * 處理拖動事件
  *
  * @param v
  * @param event
  * @param action
  */
 protected void delDrag(View v, MotionEvent event, int action) {
  switch (action) {
   case MotionEvent.ACTION_MOVE:
    int dx = (int) event.getRawX() - lastX;
    int dy = (int) event.getRawY() - lastY;
    switch (dragDirection) {
     case LEFT: // 左邊緣
      left(v, dx);
      break;
     case RIGHT: // 右邊緣
      right(v, dx);
      break;
     case BOTTOM: // 下邊緣
      bottom(v, dy);
      break;
     case TOP: // 上邊緣
      top(v, dy);
      break;
     case CENTER: // 點擊中心-->>移動
      center(v, dx, dy);
      break;
     case LEFT_BOTTOM: // 左下
      left(v, dx);
      bottom(v, dy);
      break;
     case LEFT_TOP: // 左上
      left(v, dx);
      top(v, dy);
      break;
     case RIGHT_BOTTOM: // 右下
      right(v, dx);
      bottom(v, dy);
      break;
     case RIGHT_TOP: // 右上
      right(v, dx);
      top(v, dy);
      break;
     default:
      break;
    }
    v.layout(oriLeft, oriTop, oriRight, oriBottom);
//    if (dragDirection != CENTER) {
//     v.layout(oriLeft, oriTop, oriRight, oriBottom);
//    }
    lastX = (int) event.getRawX();
    lastY = (int) event.getRawY();
    Log.i("DragViewTAG", "DragSurfaceView.delDrag:ACTION_MOVE direction="+dragDirection+", left="+oriLeft+", top="+oriTop+", right="+oriRight+", bottom="+oriBottom);
    break;
   case MotionEvent.ACTION_UP:
    ViewGroup.LayoutParams newLayoutParams = getNewLayoutParams();
    if(newLayoutParams!=null){
     Log.i("DragViewTAG", "DragSurfaceView.delDrag:ACTION_UP width="+newLayoutParams.width+", height="+newLayoutParams.height);
     setLayoutParams(newLayoutParams);
    }else {
     Log.e("DragViewTAG", "DragSurfaceView.delDrag: 父組件類型?");
     v.layout(oriLeft, oriTop, oriRight, oriBottom);
    }
    break;
   default:
    break;
  }
 }

 private ViewGroup.LayoutParams getNewLayoutParams(){
  if(getLayoutParams() instanceof RelativeLayout.LayoutParams){
   RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)getLayoutParams( );
   lp.leftMargin = oriLeft;
   lp.topMargin = oriTop;
   lp.width = oriRight-oriLeft;
   lp.height = oriBottom-oriTop;
   return lp;
  }else if(getLayoutParams() instanceof FrameLayout.LayoutParams) {
   FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
   lp.leftMargin = oriLeft;
   lp.topMargin = oriTop;
   lp.width = oriRight - oriLeft;
   lp.height = oriBottom - oriTop;
   return lp;
  }
  return null;
 }

 /**
  * 觸摸點為中心->>移動
  *
  * @param v
  * @param dx
  * @param dy
  */
 private void center(View v, int dx, int dy) {
  oriLeft += dx;
  oriTop += dy;
  oriRight += dx;
  oriBottom += dy;
  Log.i("DragViewTAG", "DragSurfaceView.center: v.left="+v.getLeft()+", v.top="+v.getTop());
  if (oriLeft < -offset) {
   Log.e("DragViewTAG", "DragSurfaceView.center: 左側(cè)越界, left="+oriLeft+", offset="+offset);
   oriLeft = -offset;
   oriRight = oriLeft + v.getWidth();
  }
  if (oriRight > screenWidth + offset) {
   Log.e("DragViewTAG", "DragSurfaceView.center: 右側(cè)越界, right="+oriRight+", screenWidth="+screenWidth+", offset="+offset);
   oriRight = screenWidth + offset;
   oriLeft = oriRight - v.getWidth();
  }
  if (oriTop < -offset) {
   Log.e("DragViewTAG", "DragSurfaceView.center: 頂部越界, top="+oriTop+", offset="+offset);
   oriTop = -offset;
   oriBottom = oriTop + v.getHeight();
  }
  if (oriBottom > screenHeight + offset) {
   Log.e("DragViewTAG", "DragSurfaceView.center: 底部越界, bottom="+oriBottom+", screenHeight="+screenHeight+", offset="+offset);
   oriBottom = screenHeight + offset;
   oriTop = oriBottom - v.getHeight();
  }
//  v.layout(left, top, right, bottom);

 }

 /**
  * 觸摸點為上邊緣
  *
  * @param v
  * @param dy
  */
 private void top(View v, int dy) {
  oriTop += dy;
  if (oriTop < -offset) {
   oriTop = -offset;
  }
  if (oriBottom - oriTop - 2 * offset < 200) {
   oriTop = oriBottom - 2 * offset - 200;
  }
 }

 /**
  * 觸摸點為下邊緣
  *
  * @param v
  * @param dy
  */
 private void bottom(View v, int dy) {
  oriBottom += dy;
  if (oriBottom > screenHeight + offset) {
   oriBottom = screenHeight + offset;
  }
  if (oriBottom - oriTop - 2 * offset < 200) {
   oriBottom = 200 + oriTop + 2 * offset;
  }
 }

 /**
  * 觸摸點為右邊緣
  *
  * @param v
  * @param dx
  */
 private void right(View v, int dx) {
  oriRight += dx;
  if (oriRight > screenWidth + offset) {
   oriRight = screenWidth + offset;
  }
  if (oriRight - oriLeft - 2 * offset < 200) {
   oriRight = oriLeft + 2 * offset + 200;
  }
 }

 /**
  * 觸摸點為左邊緣
  *
  * @param v
  * @param dx
  */
 private void left(View v, int dx) {
  oriLeft += dx;
  if (oriLeft < -offset) {
   oriLeft = -offset;
  }
  if (oriRight - oriLeft - 2 * offset < 200) {
   oriLeft = oriRight - 2 * offset - 200;
  }
 }

 /**
  * 獲取截取寬度
  *
  * @return
  */
 public int getCutWidth() {
  return getWidth() - 2 * offset;
 }

 /**
  * 獲取截取高度
  *
  * @return
  */
 public int getCutHeight() {
  return getHeight() - 2 * offset;
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android實現(xiàn)app開機自啟動功能

    Android實現(xiàn)app開機自啟動功能

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)app開機自啟動功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android互聯(lián)網(wǎng)訪問圖片并在客戶端顯示的方法

    Android互聯(lián)網(wǎng)訪問圖片并在客戶端顯示的方法

    這篇文章主要介紹了Android互聯(lián)網(wǎng)訪問圖片并在客戶端顯示的方法,結(jié)合實例分析了Android處理圖片的技巧,并附帶了Android的URL封裝類,網(wǎng)絡(luò)連接封裝類與輸出流封裝類,需要的朋友可以參考下
    2015-12-12
  • Android實現(xiàn)狀態(tài)欄(statusbar)漸變效果的示例

    Android實現(xiàn)狀態(tài)欄(statusbar)漸變效果的示例

    本篇文章主要介紹了Android實現(xiàn)狀態(tài)欄(statusbar)漸變效果的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • android實現(xiàn)背景平鋪的三種方法

    android實現(xiàn)背景平鋪的三種方法

    這篇文章主要介紹了Android的圖片平鋪效果的實現(xiàn)方法,主要有使用系統(tǒng)API、使用XML配置、自定義繪制三種方法,需要的朋友可以參考下
    2014-02-02
  • Android 消息隊列模型詳解及實例

    Android 消息隊列模型詳解及實例

    這篇文章主要介紹了 Android 消息隊列模型詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Android實現(xiàn)簡單手機震動效果

    Android實現(xiàn)簡單手機震動效果

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)手機震動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android編程使用Intent傳遞對象的方法分析

    Android編程使用Intent傳遞對象的方法分析

    這篇文章主要介紹了Android編程使用Intent傳遞對象的方法,結(jié)合實例形式詳細(xì)分析了Android使用Intent實現(xiàn)傳遞對象的相關(guān)技巧與注意事項,需要的朋友可以參考下
    2016-01-01
  • android BottomSheetDialog新控件解析實現(xiàn)知乎評論列表效果(實例代碼)

    android BottomSheetDialog新控件解析實現(xiàn)知乎評論列表效果(實例代碼)

    BottomSheetDialog是一個自定義的從底部滑入的對話框,這篇文章主要介紹了android BottomSheetDialog新控件解析實現(xiàn)知乎評論列表效果,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Android中SurfaceView用法簡單實例

    Android中SurfaceView用法簡單實例

    這篇文章主要介紹了Android中SurfaceView用法,以一個簡單的圖形繪制及改變位置實現(xiàn)方法分析了SurfaceView的使用技巧,需要的朋友可以參考下
    2015-10-10
  • 淺析Android手機衛(wèi)士手機定位的原理

    淺析Android手機衛(wèi)士手機定位的原理

    手機定位的三種方式:網(wǎng)絡(luò)定位,基站定位,GPS定位。本文給大家介紹Android手機衛(wèi)士手機定位的原理,感興趣的朋友一起學(xué)習(xí)吧
    2016-04-04

最新評論