package com.ifenglian.superapp1;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
/**
* 可上下拖动且有浮沉动画的View
* create by shixm on 2017/5/17 15:11
*/
public class SHDrawerAnimLayerLayout extends FrameLayout {
private static final String OBJECT_ANIMATION_PROPERTY_NAME = "translationY";
// 可拖动的顶部View
private View viewTopDragView;
// 可拖动的底部View
private View viewBottomDragView;
// View在关状态,露出的高度
private int viewVisibleHeight = 50;
// 可识别触摸事件的高度
private int touchHeight = 50;
// 顶部View偏移Y
private int topViewTranslationY;
// 底部View偏移Y
private int bottomViewTranslationY;
// 顶部View是否在触摸范围
private boolean isTopViewInRect;
// 底部View是否在触摸范围
private boolean isBottomViewInRect;
public SHDrawerAnimLayerLayout(Context context, View topView, View bottomView) {
this(context);
setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addView(topView);
addView(bottomView);
viewTopDragView = topView;
viewBottomDragView = bottomView;
viewTopDragView.setAlpha(1);
viewBottomDragView.setAlpha(0);
}
public SHDrawerAnimLayerLayout(Context context) {
super(context, null);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
topViewTranslationY = - viewVisibleHeight;
viewTopDragView.setY(topViewTranslationY);
bottomViewTranslationY = viewVisibleHeight;
viewBottomDragView.setY(bottomViewTranslationY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
float downX = event.getX();
float downY = event.getY();
if (!isInTopViewRect(downX, downY)) {
isTopViewInRect = false;
} else {
isTopViewInRect = true;
}
if (!isInBottomViewRect(downX, downY)) {
isBottomViewInRect = false;
} else {
isBottomViewInRect = true;
}
break;
case MotionEvent.ACTION_MOVE:
if (isTopViewInRect) {
float moveY = event.getY();
if (moveY >= 0 && moveY < getHeight()) {
viewTopDragView.setY(-getHeight() + moveY);
float alpha = moveY / getHeight();
viewTopDragView.setAlpha(alpha);
viewBottomDragView.setAlpha(1 - alpha);
}
}
if (isBottomViewInRect) {
float moveY = event.getY();
if (moveY >= 0 && moveY < getHeight()) {
viewBottomDragView.setY(moveY);
float alpha = moveY / getHeight();
viewTopDragView.setAlpha(alpha);
viewBottomDragView.setAlpha(1 - alpha);
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
if (isTopViewInRect) {
if (viewTopDragView.getTranslationY() < -getHeight() / 2) {
topViewScrollToTop();
} else {
topViewScrollToBottom();
}
isTopViewInRect = false;
}
if (isBottomViewInRect) {
if (viewBottomDragView.getTranslationY() < getHeight() / 2) {
bottomViewScrollToTop();
} else {
bottomViewScrollToBottom();
}
isBottomViewInRect = false;
}
break;
}
return true;
}
private void bottomViewScrollToTop() {
ObjectAnimator topAnimation = ObjectAnimator.ofFloat(viewBottomDragView, OBJECT_ANIMATION_PROPERTY_NAME, viewBottomDragView.getTranslationY(), viewVisibleHeight);
topAnimation.start();
}
private void topViewScrollToTop() {
ObjectAnimator topAnimation = ObjectAnimator.ofFloat(viewTopDragView, OBJECT_ANIMATION_PROPERTY_NAME, viewTopDragView.getTranslationY(), -getHeight() + viewVisibleHeight);
topAnimation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
ObjectAnimator.ofFloat(viewTopDragView, OBJECT_ANIMATION_PROPERTY_NAME, viewTopDragView.getTranslationY(), -viewVisibleHeight).setDuration(0).start();
viewTopDragView.setAlpha(0);
}
});
topAnimation.start();
}
private void bottomViewScrollToBottom() {
ObjectAnimator bottomAnimation = ObjectAnimator.ofFloat(viewBottomDragView, OBJECT_ANIMATION_PROPERTY_NAME, viewBottomDragView.getTranslationY(), getHeight() - bottomViewTranslationY);
bottomAnimation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
viewBottomDragView.setAlpha(0);
ObjectAnimator.ofFloat(viewBottomDragView, OBJECT_ANIMATION_PROPERTY_NAME, viewBottomDragView.getTranslationY(), bottomViewTranslationY).setDuration(0).start();
viewTopDragView.setAlpha(1);
}
});
bottomAnimation.start();
}
private void topViewScrollToBottom() {
ObjectAnimator bottomAnimation = ObjectAnimator.ofFloat(viewTopDragView, OBJECT_ANIMATION_PROPERTY_NAME, viewTopDragView.getTranslationY(), (topViewTranslationY));
bottomAnimation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
viewBottomDragView.setAlpha(0);
viewTopDragView.setAlpha(1);
}
});
bottomAnimation.start();
}
private boolean isInTopViewRect(float downX, float downY) {
if (downX > viewTopDragView.getLeft() && downX < viewTopDragView.getRight()) {
if (downY >= viewTopDragView.getTranslationY() + getHeight() - touchHeight && downY <= viewTopDragView.getTranslationY() + getHeight()) {
return true;
}
}
return false;
}
private boolean isInBottomViewRect(float downX, float downY) {
if (downX > viewBottomDragView.getLeft() && downX < viewBottomDragView.getRight()) {
if (downY >= viewBottomDragView.getTranslationY() && downY <= touchHeight + viewBottomDragView.getTranslationY()) {
return true;
}
}
return false;
}
}
使用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout lin = new LinearLayout(this);
lin.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
RelativeLayout relativeLayout1 = new RelativeLayout(this);
RelativeLayout.LayoutParams reParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(this, 310));
relativeLayout1.setLayoutParams(reParams);
relativeLayout1.setBackgroundColor(Color.parseColor("#00FF00"));
RelativeLayout relativeLayout2 = new RelativeLayout(this);
RelativeLayout.LayoutParams reParams2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(this, 310));
relativeLayout2.setLayoutParams(reParams2);
relativeLayout2.setBackgroundColor(Color.parseColor("#0000FF"));
SHDrawerAnimLayerLayout dragLayout = new SHDrawerAnimLayerLayout(this, relativeLayout1, relativeLayout2);
dragLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
dragLayout.setBackgroundColor(Color.parseColor("#000000"));
lin.addView(dragLayout);
lin.setBackgroundColor(Color.WHITE);
setContentView(lin);
}