中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android實現隨意拖動View效果的實例代碼

發布時間:2020-09-09 17:08:14 來源:腳本之家 閱讀:136 作者:龍旋 欄目:移動開發

項目過程中要實現能在頁面中隨意的拖動,剛開始實現是用懸浮球的形式進行實現,因為之前項目中用過,實現后發現用戶每次安裝后,都有權限的限制,甚至有些用戶關閉懸浮球權限之后,不知道怎么在手機上打開懸浮球的權限,這樣的話用戶體驗很不好,所以自己重新自定義實現在頁面中拖動,不需要請求權限。

自定義隨意拖動View:

package com.dragdemo;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
/**
 *隨意拖動的view
 */
@SuppressLint("AppCompatCustomView")
public class DragView extends ImageView {
  private int width;
  private int height;
  private int screenWidth;
  private int screenHeight;
  private Context context;
  //是否拖動
  private boolean isDrag=false;
  public boolean isDrag() {
    return isDrag;
  }
  public DragView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context=context;
  }
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    width=getMeasuredWidth();
    height=getMeasuredHeight();
    screenWidth= ScreenUtil.getScreenWidth(context);
    screenHeight=ScreenUtil.getScreenHeight(context)-getStatusBarHeight();
  }
  public int getStatusBarHeight(){
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    return getResources().getDimensionPixelSize(resourceId);
  }
  private float downX;
  private float downY;
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    if (this.isEnabled()) {
      switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
          isDrag=false;
          downX = event.getX();
          downY = event.getY();
          break;
        case MotionEvent.ACTION_MOVE:
          Log.e("kid","ACTION_MOVE");
          final float xDistance = event.getX() - downX;
          final float yDistance = event.getY() - downY;
          int l,r,t,b;
          //當水平或者垂直滑動距離大于10,才算拖動事件
          if (Math.abs(xDistance) >10 ||Math.abs(yDistance)>10) {
            Log.e("kid","Drag");
            isDrag=true;
             l = (int) (getLeft() + xDistance);
             r = l+width;
             t = (int) (getTop() + yDistance);
             b = t+height;
            //不劃出邊界判斷,此處應按照項目實際情況,因為本項目需求移動的位置是手機全屏,
            // 所以才能這么寫,如果是固定區域,要得到父控件的寬高位置后再做處理
            if(l<0){
              l=0;
              r=l+width;
            }else if(r>screenWidth){
              r=screenWidth;
              l=r-width;
            }
            if(t<0){
              t=0;
              b=t+height;
            }else if(b>screenHeight){
              b=screenHeight;
              t=b-height;
            }
            this.layout(l, t, r, b);
          }
          break;
        case MotionEvent.ACTION_UP:
          setPressed(false);
          break;
        case MotionEvent.ACTION_CANCEL:
          setPressed(false);
          break;
      }
      return true;
    }
    return false;
  }
}

用到的工具類:

package com.dragdemo;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class ScreenUtil {
  private static int width = 0;
  private static int height = 0;
  private static int showHeight = 0;
  private static int statusHeight = 0;
  private static float density = 0;
  public static int getScreenWidth(Context context) {
    if (width == 0) {
      WindowManager manager = (WindowManager) context
          .getSystemService(Context.WINDOW_SERVICE);
      Display display = manager.getDefaultDisplay();
      width = display.getWidth();
    }
    return width;
  }
  public static int getScreenHeight(Context context) {
    if (height == 0) {
      WindowManager manager = (WindowManager) context
          .getSystemService(Context.WINDOW_SERVICE);
      Display display = manager.getDefaultDisplay();
      height = display.getHeight();
    }
    return height;
  }
  public static int getScreenShowHeight(Context context) {
    if (showHeight == 0) {
      showHeight = getScreenHeight(context) - getStatusBarHeight(context);
    }
    return showHeight;
  }
  public static int getStatusBarHeight(Context context) {
    if (statusHeight > 0) {
      return statusHeight;
    }
    Class<?> c = null;
    Object obj = null;
    java.lang.reflect.Field field = null;
    int x = 0;
    try {
      c = Class.forName("com.android.internal.R$dimen");
      obj = c.newInstance();
      field = c.getField("status_bar_height");
      x = Integer.parseInt(field.get(obj).toString());
      statusHeight = context.getResources().getDimensionPixelSize(x);
      return statusHeight;
    } catch (Throwable e) {
      e.printStackTrace();
    }
    return statusHeight;
  }
  public static float getScreenDensity(Context context) {
    if (density == 0) {
      try {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager manager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getMetrics(dm);
        density = dm.density;
      } catch (Exception ex) {
        ex.printStackTrace();
        density = 1.0f;
      }
    }
    return density;
  }
  public static float getScreentMinLength(Context context) {
    return getScreenHeight(context) > getScreenWidth(context) ? getScreenWidth(context)
        : getScreenHeight(context);
  }
  /**
   * 根據指定k的系數獲取屏幕在max范圍內的最大長寬,默認寬比較小
   * 
   * @param context
   * @param k
   * @return
   */
  public static DrawWrap getCutWrap(Context context, float k, float max) {
    float tWidth = getScreenWidth(context);
    float tHeight = getScreenHeight(context);
    if (tWidth * max * k > tHeight) {
      return new DrawWrap(tHeight * max / k, tHeight * max);
    } else {
      return new DrawWrap(tWidth * max, tWidth * max * k);
    }
  }
  public static class DrawWrap {
    public float width;
    public float height;
    public DrawWrap(float width, float height) {
      this.width = width;
      this.height = height;
    }
  }
  public static int dip2px(Context context, float dipValue) {
    return (int) (dipValue * getScreenDensity(context) + 0.5f);
  }
  /**
   * 將sp值轉換為px值,保證文字大小不變
   *
   * @param context
   * @param spValue
   *      (DisplayMetrics類中屬性scaledDensity)
   * @return
   */
  public static int sp2px(Context context, float spValue) {
    final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (spValue * fontScale + 0.5f);
  }
  /**
   * 根據手機的分辨率從 px(像素) 的單位 轉成為 dp
   */
  public static int px2dip(Context context, float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
  }
  /**
   * 獲取屏幕中控件頂部位置的高度--即控件頂部的Y點
   * 
   * @return
   */
  public static int getScreenViewTopHeight(View view) {
    return view.getTop();
  }
  /**
   * 獲取屏幕中控件底部位置的高度--即控件底部的Y點
   * 
   * @return
   */
  public static int getScreenViewBottomHeight(View view) {
    return view.getBottom();
  }
  /**
   * 獲取屏幕中控件左側的位置--即控件左側的X點
   * 
   * @return
   */
  public static int getScreenViewLeftHeight(View view) {
    return view.getLeft();
  }
  /**
   * 獲取屏幕中控件右側的位置--即控件右側的X點
   * 
   * @return
   */
  public static int getScreenViewRightHeight(View view) {
    return view.getRight();
  }
  /*
   * 獲取控件寬
   */
  public static int getWidth(View view) {
    int w = View.MeasureSpec.makeMeasureSpec(0,
        View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0,
        View.MeasureSpec.UNSPECIFIED);
    view.measure(w, h);
    return (view.getMeasuredWidth());
  }
  /*
   * 獲取控件高
   */
  public static int getHeight(View view) {
    int w = View.MeasureSpec.makeMeasureSpec(0,
        View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0,
        View.MeasureSpec.UNSPECIFIED);
    view.measure(w, h);
    return (view.getMeasuredHeight());
  }
}

XML文件:

<com.dragdemo.DragView
    android:id="@+id/iv_drag"
    android:layout_gravity="center"
    android:src="@drawable/function_night_open"
    android:layout_width="80dp"
    android:layout_height="80dp"
    />

MainActivity:

package com.dragdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
  DragView iv_drag;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv_drag= (DragView) findViewById(R.id.iv_drag);
    iv_drag.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(!iv_drag.isDrag()){
          Toast.makeText(MainActivity.this, "響應點擊", Toast.LENGTH_SHORT).show();
        }
      }
    });
  }
}

總結

以上所述是小編給大家介紹的Android實現隨意拖動View效果的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

芜湖市| 封开县| 泸州市| 平南县| 灵丘县| 邮箱| 成都市| 东乡县| 昔阳县| 城口县| 太仓市| 丹寨县| 扬中市| 安阳市| 岗巴县| 文成县| 延吉市| 韶山市| 德州市| 上栗县| 普陀区| 洛川县| 万州区| 常熟市| 泽州县| 渭南市| 太湖县| 景德镇市| 临澧县| 万荣县| 临武县| 无棣县| 井研县| 疏勒县| 阜阳市| 金塔县| 四子王旗| 呈贡县| 博湖县| 招远市| 三门峡市|