wechat135.jpg (32.84 KB, 下载次数: 1255)
下载附件 保存到相册
7 年前 上传
587163-da620814bd1017ec.gif (478.45 KB, 下载次数: 1313)
587163-883b041d40373d26.png (15.06 KB, 下载次数: 1251)
@Override public boolean onTouchEvent(MotionEvent event) { //获得根View View rootView = getRootView(); //获得触摸位置在全屏所在位置 float mRawX = event.getRawX(); float mRawY = event.getRawY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //请求父View不拦截 getParent().requestDisallowInterceptTouchEvent(true); //获得当前View在屏幕上的位置 int[] cLocation = new int[2]; getLocationOnScreen(cLocation); if (rootView instanceof ViewGroup) { //初始化拖拽时显示的View dragView = new DragView(getContext()); //设置固定圆的圆心坐标 dragView.setStickyPoint(cLocation[0] + mWidth / 2, cLocation[1] + mHeight / 2, mRawX, mRawY); //获得缓存的bitmap,滑动时直接通过drawBitmap绘制出来 setDrawingCacheEnabled(true); Bitmap bitmap = getDrawingCache(); if (bitmap != null) { dragView.setCacheBitmap(bitmap); //将DragView添加到RootView中,这样就可以全屏滑动了 ((ViewGroup) rootView).addView(dragView); setVisibility(INVISIBLE); } } break; case MotionEvent.ACTION_MOVE: //请求父View不拦截 getParent().requestDisallowInterceptTouchEvent(true); if (dragView != null) { //更新DragView的位置 dragView.setDragViewLocation(mRawX, mRawY); } break; case MotionEvent.ACTION_UP: getParent().requestDisallowInterceptTouchEvent(false); if (dragView != null) { //手抬起时来判断各种情况 dragView.setDragUp(); } break; } return true; }
private int mState;//当前红点的状态 private static final int STATE_INIT = 0;//默认静止状态 private static final int STATE_DRAG = 1;//拖拽状态 private static final int STATE_MOVE = 2;//移动状态 private static final int STATE_DISMISS = 3;//消失状态
/** * 设置固定圆的圆心和半径 * @param stickyX 固定圆的X坐标 * @param stickyY 固定圆的Y坐标 */ public void setStickyPoint(float stickyX, float stickyY, float touchX, float touchY) { //分别设置固定圆和拖拽圆的坐标 stickyPointF.set(stickyX, stickyY); dragPointF.set(touchX, touchY); //通过两个圆点算出圆心距,也是拖拽的距离 dragDistance = MathUtil.getTwoPointDistance(dragPointF, stickyPointF); if (dragDistance <= maxDistance) { //如果拖拽距离小于规定最大距离,则固定的圆应该越来越小,这样看着才符合实际 stickRadius = (int) (defaultRadius - dragDistance / 10) < 10 ? 10 : (int) (defaultRadius - dragDistance / 10); mState = STATE_DRAG; } else { mState = STATE_INIT; } }
/** * 设置拖拽的坐标位置 * * @param touchX 拖拽时的X坐标 * @param touchY 拖拽时的Y坐标 */ public void setDragViewLocation(float touchX, float touchY) { dragPointF.set(touchX, touchY); //随时更改圆心距 dragDistance = MathUtil.getTwoPointDistance(dragPointF, stickyPointF); if (mState == STATE_DRAG) { if (isInsideRange()) { stickRadius = (int) (defaultRadius - dragDistance / 10) < 10 ? 10 : (int) (defaultRadius - dragDistance / 10); } else { mState = STATE_MOVE; if (onDragListener != null) { onDragListener.onMove(); } } } invalidate(); }
public void setDragUp() { if (mState == STATE_DRAG && isInsideRange()) { //拖拽状态且在范围之内 startResetAnimator(); } else if (mState == STATE_MOVE) { if (isInsideRange()) { //在范围之内 需要RESET startResetAnimator(); } else { //在范围之外 消失动画 mState = STATE_DISMISS; startExplodeAnim(); } } }
@Override protected void onDraw(Canvas canvas) { if (isInsideRange() && mState == STATE_DRAG) { mPaint.setColor(Color.RED); //绘制固定的小圆 canvas.drawCircle(stickyPointF.x, stickyPointF.y, stickRadius, mPaint); //首先获得两圆心之间的斜率 Float linK = MathUtil.getLineSlope(dragPointF, stickyPointF); //然后通过两个圆心和半径、斜率来获得外切线的切点 PointF[] stickyPoints = MathUtil.getIntersectionPoints(stickyPointF, stickRadius, linK); dragRadius = (int) Math.min(mWidth, mHeight) / 2; PointF[] dragPoints = MathUtil.getIntersectionPoints(dragPointF, dragRadius, linK); mPaint.setColor(Color.RED); //二阶贝塞尔曲线的控制点取得两圆心的中点 controlPoint = MathUtil.getMiddlePoint(dragPointF, stickyPointF); //绘制贝塞尔曲线 mPath.reset(); mPath.moveTo(stickyPoints[0].x, stickyPoints[0].y); mPath.quadTo(controlPoint.x, controlPoint.y, dragPoints[0].x, dragPoints[0].y); mPath.lineTo(dragPoints[1].x, dragPoints[1].y); mPath.quadTo(controlPoint.x, controlPoint.y, stickyPoints[1].x, stickyPoints[1].y); mPath.lineTo(stickyPoints[0].x, stickyPoints[0].y); canvas.drawPath(mPath, mPaint); } if (mCacheBitmap != null && mState != STATE_DISMISS) { //绘制缓存的Bitmap canvas.drawBitmap(mCacheBitmap, dragPointF.x - mWidth / 2, dragPointF.y - mHeight / 2, mPaint); } if (mState == STATE_DISMISS && explodeIndex < explode_res.length) { //绘制小红点消失时的爆炸动画 canvas.drawBitmap(bitmaps[explodeIndex], dragPointF.x - mWidth / 2, dragPointF.y - mHeight / 2, mPaint); } }
aaa.png (2.34 KB, 下载次数: 1253)
来源:即时通讯网 - 即时通讯开发者社区!
轻量级开源移动端即时通讯框架。
快速入门 / 性能 / 指南 / 提问
轻量级Web端即时通讯框架。
详细介绍 / 精编源码 / 手册教程
移动端实时音视频框架。
详细介绍 / 性能测试 / 安装体验
基于MobileIMSDK的移动IM系统。
详细介绍 / 产品截图 / 安装体验
一套产品级Web端IM系统。
详细介绍 / 产品截图 / 演示视频
引用此评论
精华主题数超过100个。
连续任职达2年以上的合格正式版主
为论区做出突出贡献的开发者、版主等。
Copyright © 2014-2024 即时通讯网 - 即时通讯开发者社区 / 版本 V4.4
苏州网际时代信息科技有限公司 (苏ICP备16005070号-1)
Processed in 0.125000 second(s), 44 queries , Gzip On.