溫馨提示×

溫馨提示×

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

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

Android側邊滑動關閉Activity的方法

發(fā)布時間:2020-07-23 15:11:26 來源:億速云 閱讀:437 作者:小豬 欄目:移動開發(fā)

這篇文章主要講解了Android側邊滑動關閉Activity的方法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

0.效果圖

Android側邊滑動關閉Activity的方法

1.設置Activity樣式屬性

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
</style>

2.自定義側邊陰影視圖

class SlideBackView extends View {

    private Paint mBgPaint, mShadowPaint;
    private RectF mBgRectF, mShadowRectF;
    private float mRatio;
    private float mShadowSize;

    public SlideBackView(Context context) {
      super(context);
      mBgPaint = new Paint();
      mBgPaint.setAntiAlias(true);
      mBgPaint.setColor(0xff000000);
      mShadowPaint = new Paint();
      mShadowPaint.setAntiAlias(true);
      mShadowPaint.setStyle(Paint.Style.FILL);
      mShadowSize = dp2px(15);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      mBgRectF = new RectF();
      mBgRectF.top = 0;
      mBgRectF.left = 0;
      mBgRectF.bottom = MeasureSpec.getSize(heightMeasureSpec);

      mShadowRectF = new RectF();
      mShadowRectF.top = 0;
      mShadowRectF.bottom = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
      int width = getMeasuredWidth();
      float right = mRatio * width;
      mBgRectF.right = right;
      mBgPaint.setAlpha((int) (128 * (1 - mRatio)));
      canvas.drawRect(mBgRectF, mBgPaint);
      mShadowRectF.left = right - mShadowSize;
      mShadowRectF.right = right;
      mShadowPaint.setShader(new LinearGradient(mShadowRectF.left, 0, mShadowRectF.right, 0, 0x00000000, 0x26000000, Shader.TileMode.CLAMP));
      canvas.drawRect(mShadowRectF, mShadowPaint);
    }

    public void setDistance(float ratio) {
      mRatio = ratio;
      invalidate();
    }

    private float dp2px(float dpValue) {
      float density = getResources().getDisplayMetrics().density;
      return dpValue * density + 0.5F;
    }
  }

3.定義可滑動的Activity父類

public class SlideBaseActivity extends AppCompatActivity implements ValueAnimator.AnimatorUpdateListener {

  private boolean isAnimate, isSlide, isHandle;
  private float moveNum;
  private float lastX, lastY;
  private int lastPointerCount;
  private float mAnimatedValue;
  private ValueAnimator mValueAnimator;
  private SlideBackView mSlideBackView;
  private float mTouchSlop;
  private List<ShieldView> shieldViews = new ArrayList<>();

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    super.onCreate(savedInstanceState);
    initAnimator();
    initSlideBackView();
    mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
  }

  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
    mAnimatedValue = (float) animation.getAnimatedValue();
    moveView(mAnimatedValue);
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
    if (!isAnimate) {
      float x = event.getRawX();
      float y = event.getRawY();
      if (event.getPointerCount() != lastPointerCount) {
        lastPointerCount = event.getPointerCount();
        lastX = x;
        lastY = y;
      }
      float offsetX, offsetY;
      switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
          offsetX = x - lastX;
          offsetY = y - lastY;
          if (!isHandle) {
            float absX = Math.abs(offsetX);
            float absY = Math.abs(offsetY);
            if (absX > mTouchSlop) {
              if (absX * 0.5f > absY) {
                isSlide = true;
                checkSlide((int) x, (int) y);
              } else {
                isSlide = false;
              }
              isHandle = true;
            }
          } else if (isSlide) {
            moveNum += offsetX;
            if (moveNum < 0) {
              moveNum = 0;
            }
            moveView(moveNum);
            lastX = event.getX();
          }
          break;
        case MotionEvent.ACTION_UP:
          if (isHandle) {
            isSlide = false;
            isHandle = false;
            isAnimate = true;
            int width = getWindow().getDecorView().getMeasuredWidth();
            if (moveNum < width / 3f) {
              mValueAnimator.setFloatValues(moveNum, 0);
            } else {
              mValueAnimator.setFloatValues(moveNum, width);
            }
            mValueAnimator.start();
            moveNum = 0;
            lastPointerCount = 0;
          }
      }
    }
    return isSlide || super.dispatchTouchEvent(event);
  }

  /**
   * 添加禁用滑動的子布局
   */
  public void addShieldView(View view) {
    shieldViews.add(new ShieldView(false, view));
  }

  /**
   * 添加水平禁用滑動的子布局
   */
  public void addHorizontalShieldView(View view) {
    shieldViews.add(new ShieldView(true, view));
  }

  /**
   * 移除禁用滑動的子布局
   */
  public void removeShieldView(View view) {
    for (ShieldView v : shieldViews) {
      if (v.view != null && v.view.equals(view)) {
        shieldViews.remove(v);
        break;
      }
    }
  }

  /**
   * 清空禁用滑動的子布局
   */
  public void clearShieldView() {
    shieldViews.clear();
  }

  private void initAnimator() {
    mValueAnimator = new ValueAnimator();
    mValueAnimator.setDuration(300);
    mValueAnimator.addUpdateListener(this);
    mValueAnimator.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        isAnimate = false;
        if (mAnimatedValue == getWindow().getDecorView().getMeasuredWidth()) {
          SlideBaseActivity.super.finish();
          overridePendingTransition(0, 0);
        }
      }
    });
  }

  private void initSlideBackView() {
    mSlideBackView = new SlideBackView(this);
    ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
    decorView.addView(mSlideBackView);
  }

  private void moveView(float moveX) {
    ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
    mSlideBackView.setDistance(moveX / decorView.getMeasuredWidth());
    int count = decorView.getChildCount();
    for (int i = 0; i < count; i++) {
      View view = decorView.getChildAt(i);
      if (view != mSlideBackView) {
        view.setX(moveX);
      }
    }
  }

  private void checkSlide(int x, int y) {
    for (ShieldView v : shieldViews) {
      Rect rect = new Rect();
      v.view.getGlobalVisibleRect(rect);
      if (rect.contains(x, y) && (!(lastX < x && !v.view.canScrollHorizontally(-1)) || (!v.isHorizontal))) {
        isSlide = false;
      }
    }
  }
  
  class ShieldView {
    boolean isHorizontal;
    View view;

    public ShieldView(boolean isHorizontal, View view) {
      this.isHorizontal = isHorizontal;
      this.view = view;
    }
  }
}

4.使用

繼承SlideBaseActivity類,可調用addShieldViewaddHorizontalShieldView方法解決事件沖突。

看完上述內容,是不是對Android側邊滑動關閉Activity的方法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

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

AI