Android开发仿微信下拉关闭图片11
Posted l-d-d
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发仿微信下拉关闭图片11相关的知识,希望对你有一定的参考价值。
图片会跟随手指移动,只有是下滑时才会退出查看页面,其他情况会复位,直接当做ImageView使用即可,setViewCall方法是在下滑完成后要执行的操作,上,左,右,可自行扩展
onTouchEvent 监听手指坐标,GestureDetector 监听滑动的惯性,ViewHelper设置图片位移动画
public class FriendCircleView extends android.support.v7.widget.AppCompatImageView implements GestureDetector.OnGestureListener {
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")
@Override
public 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);
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
isFinsh = true;
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public 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();
}
}
以上是关于Android开发仿微信下拉关闭图片11的主要内容,如果未能解决你的问题,请参考以下文章
android仿微信红包动画Kotlin综合应用Xposed模块炫酷下拉视觉UC浏览器滑动动画等源码
Android 修改源码自定义SwipeRefreshLayout样式——高仿微信朋友圈下拉刷新
Android初学二之仿微信APP实现RecyclerView控件的设计开发,实现点击事件及图片瀑布流