日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

高仿苹果虚拟home键,手势拖动,放手会弹到屏幕边缘。

發布時間:2023/12/14 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 高仿苹果虚拟home键,手势拖动,放手会弹到屏幕边缘。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在最近的項目開發過程中,產品提出了一個需求,就是要求在屏幕上面有一個懸浮按鈕,然后手勢可以拖動,在手抬起的時候會回到屏幕邊緣,類似于蘋果的虛擬home鍵。 不多說了,直接上代碼了,代碼里面會有注釋,可以解釋清楚:整個Demo只有一個類,繼承FloatingActionButton: import android.animation.ObjectAnimator; import android.content.Context; import android.support.design.widget.FloatingActionButton; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.animation.DecelerateInterpolator;import com.rhzt.xgj.util.DisplayUtil;/*** Created by lixuce on 2017/5/8.*/public class DragFloatActionButton extends FloatingActionButton {private int screenWidth;private int screenHeight;private int screenWidthHalf;private int statusHeight;public DragFloatActionButton(Context context) {super(context);init();}public DragFloatActionButton(Context context, AttributeSet attrs) {super(context, attrs);init();}public DragFloatActionButton(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}private void init(){//獲取屏幕寬screenWidth= DisplayUtil.getScreenWidth(getContext());screenWidthHalf=screenWidth/2;//屏幕的高screenHeight=DisplayUtil.getScreenHight(getContext());//狀態欄的高度// 如果狀態欄可以拖動 就獲取狀態欄的高度 目前設置成0statusHeight=0;}private int lastX;private int lastY;private boolean isDrag;@Overridepublic boolean onTouchEvent(MotionEvent event) {int rawX = (int) event.getRawX();int rawY = (int) event.getRawY();switch (event.getAction() & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_DOWN:isDrag=false;getParent().requestDisallowInterceptTouchEvent(true);lastX=rawX;lastY=rawY;break;case MotionEvent.ACTION_MOVE:isDrag=true;//計算手指移動了多少int dx=rawX-lastX;int dy=rawY-lastY;//這里修復一些華為手機無法觸發點擊事件的問題int distance= (int) Math.sqrt(dx*dx+dy*dy);if(distance==0){isDrag=false;break;}float x=getX()+dx;float y=getY()+dy;//檢測是否到達邊緣 左上右下x=x<0?0:x>screenWidth-getWidth()?screenWidth-getWidth():x;y=y<statusHeight?statusHeight:y+getHeight()>screenHeight?screenHeight-getHeight():y;setX(x);setY(y);lastX=rawX;lastY=rawY;//Log.i("getX="+getX()+";getY="+getY()+";screenHeight="+screenHeight);break;case MotionEvent.ACTION_UP:if(isDrag){//恢復按壓效果setPressed(false); // Log.i("getX="+getX()+";screenWidthHalf="+screenWidthHalf);if(rawX>=screenWidthHalf){animate().setInterpolator(new DecelerateInterpolator()).setDuration(500).xBy(screenWidth-getWidth()-getX()).start();}else {ObjectAnimator oa=ObjectAnimator.ofFloat(this,"x",getX(),0);oa.setInterpolator(new DecelerateInterpolator());oa.setDuration(500);oa.start();}}break;}//如果是拖拽則消耗事件,否則正常傳遞即可。你return isDrag || super.onTouchEvent(event);} } 很簡單就可以實現效果了,代碼是應對我的需求來寫的,你也可以自己來寫,只是一個思路,希望可以幫到你。




總結

以上是生活随笔為你收集整理的高仿苹果虚拟home键,手势拖动,放手会弹到屏幕边缘。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。