国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現單頁面浮層可拖動view的一種方法

2019-10-22 18:26:01
字體:
來源:轉載
供稿:網友

上一篇講到通過通過goolge官方的ViewDragHelper工具實現拖動的方法(上一篇見http://www.survivalescaperooms.com/article/125481.htm),那么有一個問題就是在DragframeLayout中的onTouchEvent一直接收不到觸摸消息,而且在onInterceptTouchEvent的時候,并沒有觸發ViewDragHelper.tryCaptureView方法,因此誕生了另一種比較原始的方法:通過自定義android/244996.html">android/285862.html">可拖動view來實現

主要方法:

initEdge:設置可拖動view能拖動范圍的初始邊界,一般情況下為父布局的邊界。注意view.getLeft...等會獲取到會0,我是在網路數據返回的情況下設置邊界,并顯示的。也有方法開一個子線程獲取。

onTouchEvent:拖動的計算以及重新layout

代碼:

import android.content.Context;import android.support.annotation.Nullable;import android.support.v7.widget.AppCompatImageView;import android.util.AttributeSet;import android.view.MotionEvent;/** * Created by hq on 2017/10/10. * 參考:http://blog.csdn.net/zane_xiao/article/details/51188867 */public class DragImageView extends AppCompatImageView {  String TAG = "DragImageView";  public DragImageView(Context context) {    this(context, null);  }  public DragImageView(Context context, @Nullable AttributeSet attrs) {    this(context, attrs, 0);  }  public DragImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);  }  /**   * 設置在父布局中的邊界   * @param l   * @param t   * @param r   * @param b   */  public void initEdge(int l,int t,int r,int b) {    edgeLeft = l;    edgeTop = t;    edgeRight = r;    edgeBottom = b;  }  int edgeLeft, edgeTop, edgeRight, edgeBottom;  int lastX, lastY, movex, movey, dx, dy;  @Override  public boolean onTouchEvent(MotionEvent event) {    switch (event.getAction()) {      case MotionEvent.ACTION_DOWN:        lastX = (int) event.getRawX();        lastY = (int) event.getRawY();        movex = lastX;        movey = lastY;        break;      case MotionEvent.ACTION_MOVE:        dx = (int) event.getRawX() - lastX;        dy = (int) event.getRawY() - lastY;        int left = getLeft() + dx;        int top = getTop() + dy;        int right = getRight() + dx;        int bottom = getBottom() + dy;        if (left < edgeLeft) {          left = edgeLeft;          right = left + getWidth();        }        if (right > edgeRight) {          right = edgeRight;          left = right - getWidth();        }        if (top < edgeTop) {          top = edgeTop;          bottom = top + getHeight();        }        if (bottom > edgeBottom) {          bottom = edgeBottom;          top = bottom - getHeight();        }        layout(left, top, right, bottom);        lastX = (int) event.getRawX();        lastY = (int) event.getRawY();        break;      case MotionEvent.ACTION_UP:        //避免滑出觸發點擊事件        if ((int) (event.getRawX() - movex) != 0          || (int) (event.getRawY() - movey) != 0) {          return true;        }        break;      default:        break;    }    return super.onTouchEvent(event);  }}

布局:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:custom="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/df_content"android:layout_width="match_parent"android:layout_height="match_parent"><com.windfindtech.ishanghai.view.SwipeScrollView  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@color/default_white"  android:scrollbars="none">  <RelativeLayout    android:id="@+id/network_tab_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/default_white">    ...........  </RelativeLayout></com.windfindtech.ishanghai.view.SwipeScrollView><com.windfindtech.ishanghai.view.DragImageView  android:id="@+id/iv_drag_adver"  android:layout_width="40dp"  android:layout_height="40dp"  android:layout_gravity="right|top"  android:src="@drawable/ic_launcher" /></FrameLayout>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 温州市| 昭平县| 类乌齐县| 无棣县| 太湖县| 视频| 盖州市| 靖宇县| 乌兰浩特市| 弥渡县| 云安县| 双鸭山市| 新昌县| 红原县| 唐河县| SHOW| 北京市| 西吉县| 德钦县| 石家庄市| 循化| 鹿泉市| 叙永县| 金阳县| 卢湾区| 灯塔市| 大邑县| 嫩江县| 灵宝市| 霍林郭勒市| 南昌县| 东港市| 亳州市| 扶余县| 天津市| 临澧县| 安阳县| 轮台县| 客服| 新平| 南宫市|