package luffy.com.cn.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
public class SlideMenuView extends ViewGroup {
private int leftMargin;
private int gap;
public SlideMenuView(Context context, View backView, View frontView) {
super(context);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
addView(backView, lp);
addView(frontView, lp);
}
public SlideMenuView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
checkChildCount();
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
int height = child.getMeasuredHeight();
int width = child.getMeasuredWidth();
if (i == 1) {
child.layout(leftMargin, 0, leftMargin + width, height);
} else {
child.layout(0, 0, width, height);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"ScrollLayout only canmCurScreen run at EXACTLY mode!");
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"ScrollLayout only can run at EXACTLY mode!");
}
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
}
/**
* 使用之前先传入需要留下间隔的宽度。或者调用setGap(final View measureView)。
*
* @param gap
* 间隔宽度
*/
public void setGap(int gap) {
this.gap = gap;
// invalidate();
}
/**
* 使用之前先传入要留下的空间。或者调用setGap(int gap)。
*
* @param measureView
* 间隔宽度
* @param gap
* 间隔修正值,无需修正传入0
*/
public void setGap(final View measureView, final int resize) {
ViewTreeObserver vto = measureView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int width = measureView.getMeasuredWidth();
gap = width;
getChildAt(0).setPadding(0, 0, gap + resize, 0);
setGap(gap + resize);
}
});
}
private void checkChildCount() {
if (2 != getChildCount()) {
throw new IndexOutOfBoundsException("只允许拥有2个子View!");
}
}
private void setLeftMargin(int leftMargin) {
this.leftMargin = leftMargin;
}
private void ani(final int from, final int to, final int left) {
final View v = getChildAt(1);
TranslateAnimation translateAnimation = new TranslateAnimation(from,
to, 0, 0);
translateAnimation.setInterpolator(new DecelerateInterpolator());
translateAnimation.setFillAfter(true);
translateAnimation.setDuration(250);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
setLeftMargin(left);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
v.clearAnimation();
v.layout(left, 0, left + getWidth(), v.getHeight());
}
});
v.startAnimation(translateAnimation);
}
public boolean isMenuOpen() {
if (getChildAt(1).getLeft() == 0) {
return false;
}
return true;
}
public void sliding() {
if (isMenuOpen()) {
ani(0, gap - getWidth(), 0);
} else {
ani(0, getWidth() - gap, getWidth() - gap);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ViewGroupRR.zip (34个子文件)
ViewGroupRR
AndroidManifest.xml 771B
res
layout
back_view.xml 757B
main.xml 650B
drawable-ldpi
ic_launcher.png 2KB
drawable-mdpi
ic_launcher.png 3KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 188B
proguard.cfg 1KB
src
luffy
com
cn
view
BackView.java 927B
SlideMenuView.java 4KB
activity
ViewGroupRRActivity.java 1KB
bin
classes
luffy
com
cn
view
SlideMenuView$1.class 1KB
BackView.class 1KB
BackView$1.class 914B
SlideMenuView.class 5KB
SlideMenuView$2.class 1KB
activity
ViewGroupRRActivity.class 2KB
ViewGroupRRActivity$1.class 999B
zhangkai
com
cn
viewgroup
R.class 580B
R$layout.class 448B
R$attr.class 361B
R$id.class 537B
R$string.class 448B
R$drawable.class 428B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-mdpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
classes.dex 9KB
ViewGroupRR.apk 17KB
resources.ap_ 11KB
.classpath 364B
assets
project.properties 361B
.project 847B
gen
zhangkai
com
cn
viewgroup
R.java 1024B
共 34 条
- 1
Luffy_1988
- 粉丝: 5
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页