Android开发仿微信下拉关闭图片11

图片会跟随手指移动,只有是下滑时才会退出查看页面,其他情况会复位,直接当做ImageView使用即可,setViewCall方法是在下滑完成后要执行的操作,上,左,右,可自行扩展

onTouchEvent 监听手指坐标,GestureDetector 监听滑动的惯性,ViewHelper设置图片位移动画

public class FriendCircleView extends android.support.v7.widget.AppCompatImageView implements GestureDetector.OnGestureListener {

Android开发仿微信下拉关闭图片11
public FriendCircleView(Context context) { super(context); initView(context, null, 0);}public FriendCircleView(Context context, AttributeSet attrs) { super(context, attrs); initView(context, attrs, 0);}public FriendCircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs, defStyleAttr);}//设置滑动完成后的回调方法public void setViewCall(FriendCircleViewCall viewCall) { this.viewCall = viewCall;}private void initView(Context context, AttributeSet attrs, int defStyleAttr) { screenHeight = ScreenUtils.getScreenHeight(context); detector = new GestureDetector(context, this);}private GestureDetector detector;private int screenHeight;//设备屏幕高度private float oldX, oldY;//手机放在屏幕的坐标private float movY;//移动中在屏幕上的坐标private float alphaPercent = 1f;//背景颜色透明度private boolean isFinsh = false;//是否执行关闭页面的操作private FriendCircleViewCall viewCall = null;@SuppressLint("ClickableViewAccessibility")@Overridepublic boolean onTouchEvent(MotionEvent event) { detector.onTouchEvent(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: oldX = event.getRawX(); oldY = event.getRawY(); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if (isFinsh) { isFinsh = false; if (viewCall != null) { viewCall.viewDestry(); } Log.e("ldd------", "页面返回"); } else { setupUping(); Log.e("ldd------", "手指抬起"); } break; case MotionEvent.ACTION_MOVE: isFinsh = false; float movX = event.getRawX() - oldX; movY = event.getRawY() - oldY; setupMoving(movX, movY); if (Math.abs(movX) > Math.abs(movY)) { if (movX < 0) { Log.e("ldd------", "左滑动"); } else { Log.e("ldd------", "右滑动"); } } else { if (movY < 0) { Log.e("ldd------", "上滑动"); } else { if (movY > (screenHeight / 6)) { isFinsh = true; Log.e("ldd------", "下滑动"); } } } return false; } return true;}private void setupUping() { animate().setDuration(200) .scaleX(1) .scaleY(1) .translationX(0) .translationY(0) .setUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { if (alphaPercent < animation.getAnimatedFraction()) { ((ViewGroup) getParent()).setBackgroundColor(convertPercentToBlackAlphaColor(animation.getAnimatedFraction())); } } }) .start();}private void setupMoving(float deltaX, float deltaY) { if (Math.abs(movY) < (screenHeight / 4)) { float scale = 1 - Math.abs(movY) / screenHeight; alphaPercent = 1 - Math.abs(deltaY) / (screenHeight / 2); ViewHelper.setScaleX(this, scale); ViewHelper.setScaleY(this, scale); ((ViewGroup) getParent()).setBackgroundColor(convertPercentToBlackAlphaColor(alphaPercent)); } ViewHelper.setTranslationX(this, deltaX); ViewHelper.setTranslationY(this, deltaY);}//设置背景颜色透明度protected int convertPercentToBlackAlphaColor(float percent) { percent = Math.min(1, Math.max(0, percent)); int intAlpha = (int) (percent * 255); String stringAlpha = Integer.toHexString(intAlpha).toLowerCase(); String color = "#" + (stringAlpha.length() < 2 ? "0" : "") + stringAlpha + "000000"; return Color.parseColor(color);}@Overridepublic boolean onDown(MotionEvent e) { return false;}@Overridepublic void onShowPress(MotionEvent e) {}@Overridepublic boolean onSingleTapUp(MotionEvent e) { isFinsh = true; return false;}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false;}@Overridepublic void onLongPress(MotionEvent e) {}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { float movX = e2.getRawX() - e1.getRawX(); float movY = e2.getRawY() - e1.getRawY(); Log.e("ldd------2", movX + "---------" + movY); if (Math.abs(movX) > Math.abs(movY)) { if (movX < 0) { Log.e("ldd------2", "左滑动"); } else { Log.e("ldd------2", "右滑动"); } } else { if (movY < 0) { Log.e("ldd------2", "上滑动"); } else { isFinsh = true; Log.e("ldd------2", "下滑动"); } } return true;}public interface FriendCircleViewCall { void viewDestry();}

}

相关文章