热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android侧边滑动关闭Activity的示例代码

这篇文章主要介绍了Android侧边滑动关闭Activity的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

0.效果图

1.设置Activity样式属性


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 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 

4.使用

继承SlideBaseActivity类,可调用addShieldViewaddHorizontalShieldView方法解决事件冲突。

5.项目源码

https://gitee.com/yugu/slide-demo

总结

到此这篇关于Android 侧边滑动关闭Activity的文章就介绍到这了,更多相关Android 侧边滑动关闭Activity内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!


推荐阅读
author-avatar
淡水鱼yw灬s
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有