資源簡介
安卓開發(fā)可以拖拽,拖拽后自動(dòng)吸附到手機(jī)屏幕邊的自定義view(已更新之前的有問題)
代碼片段和文件信息
package?com.ggx.test;
import?android.animation.objectAnimator;
import?android.content.Context;
import?android.support.design.widget.FloatingActionButton;
import?android.util.AttributeSet;
import?android.util.Log;
import?android.view.MotionEvent;
import?android.view.ViewGroup;
import?android.view.animation.DecelerateInterpolator;
/**
?*?Created?by?jerry.guan?on?2/15/2017.
?*/
public?class?DragFloatActionButton?extends?FloatingActionButton{
????private?int?parentHeight;
????private?int?parentWidth;
????public?DragFloatActionButton(Context?context)?{
????????super(context);
????}
????public?DragFloatActionButton(Context?context?AttributeSet?attrs)?{
????????super(context?attrs);
????}
????public?DragFloatActionButton(Context?context?AttributeSet?attrs?int?defstyleAttr)?{
????????super(context?attrs?defstyleAttr);
????}
????private?int?lastX;
????private?int?lastY;
????private?boolean?isDrag;
????@Override
????public?boolean?onTouchEvent(MotionEvent?event)?{
????????int?rawX?=?(int)?event.getRawX();
????????int?rawY?=?(int)?event.getRawY();
????????switch?(event.getAction()?&?MotionEvent.ACTION_MASK)?{
????????????case?MotionEvent.ACTION_DOWN:
????????????????setPressed(true);
????????????????isDrag=false;
????????????????getParent().requestDisallowInterceptTouchEvent(true);
????????????????lastX=rawX;
????????????????lastY=rawY;
????????????????ViewGroup?parent;
????????????????if(getParent()!=null){
????????????????????parent=?(ViewGroup)?getParent();
????????????????????parentHeight=parent.getHeight();
????????????????????parentWidth=parent.getWidth();
????????????????}
????????????????break;
????????????case?MotionEvent.ACTION_MOVE:
????????????????if(parentHeight<=0||parentWidth==0){
????????????????????isDrag=false;
????????????????????break;
????????????????}else?{
????????????????????isDrag=true;
????????????????}
??????????????
評論
共有 條評論