简单实现点击扩展内容的layout
转载请注明出处http://blog.csdn.net/ligenxun
简单实现了点击扩展内容的功能。
前段时间脑洞大开,在看新闻的时候觉得apk内部的跳转太单调了,一点都不酷炫,就想着为什么不能点击layout然后内容流畅地出现呢?想想就觉得酷,就做了一个小例子。
首先,通过继承LinearLayout 来完成大概布局,
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
然后开始实现目标,首先我们假设两个layout在mylayout中,分别为layout1和layout2,要实现触摸扩展,我们要分别监听触摸事件:
private boolean b1 = false,b2 = false;
private int l1l,l1r,l2l,l2r;
private int pid = 22;
private Scroller scroller;
private boolean bt1 = false,bt2=false;
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
l1l = layout1.getLeft();
l1r = layout1.getRight();
l2l = layout2.getLeft();
l2r = layout2.getRight();
layout1.getChildAt(0).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
bt1 = true;
if (b2){
scroller.startScroll(l1l + pid, layout1.getTop(), l2r - pid, layout1.getTop(),700);
layout2.layout(l1l + pid + 1, layout2.getTop(), layout2.getRight(), layout2.getBottom());
}
else {
if (!b1) {
//TODo layout1 c
scroller.startScroll(l1r, layout1.getTop(), l2r - l1r - pid, layout1.getTop(), 400);
layout2.layout(layout1.getRight() + 1, layout2.getTop(), layout2.getRight(), layout2.getBottom());
Log.d("left", "false");
Log.d("layout2.getRight()", Integer.toString(layout2.getRight()));
} else {
//TODo layout1 d
layout2.layout(layout1.getRight() - 1, layout2.getTop(), layout2.getRight(), layout2.getBottom());
Log.d("left", "true");
Log.d("l1r", Integer.toString(l1r));
}
}
}
});
layout2.getChildAt(0).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
bt2 = true;
if(b1){
scroller.startScroll(l1l + pid ,layout1.getTop(), l2r - pid,layout1.getTop(),700);
layout1.layout(layout1.getLeft(), layout1.getTop(), l2r - pid - 1, layout1.getBottom());
}else {
if (!b2) {
//TODo layout2 c
scroller.startScroll(l1l + pid, layout2.getTop(), l2l, layout2.getTop(), 400);
layout1.layout(layout1.getLeft(), layout1.getTop(), l1r - 1, layout1.getBottom());
Log.d("right", "false");
Log.d("layout1.getLeft()", Integer.toString(layout1.getLeft()));
} else {
//TODo layout2 d
scroller.startScroll(l1l + pid, layout2.getTop(), l2l, layout2.getTop(), 400);
layout1.layout(layout1.getLeft(), layout1.getTop(), layout2.getLeft() + 1, layout1.getBottom());
Log.d("right", "true");
Log.d("l2r", Integer.toString(l2r));
}
}
}
});
invalidate();
break;
}
return super.onTouchEvent(event);
}
通过监听layout1和layout2中第一个child来实现触摸监听。然后我们再通过Scroller 来实现layout的渐渐扩展:
public void computeScroll() {
if (scroller.computeScrollOffset()) {
int x = scroller.getCurrX();
int y = scroller.getCurrY();
System.out.println("x=" + x);
System.out.println("y=" + y);
if (bt1) {
if(b2){
if (x<l2r-pid){
LayoutParams l = new LayoutParams( x - (l1l),
layout1.getBottom() - layout1.getTop());
layout1.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout1.getLayoutParams().width,layout1.getHeight());
layout2.setLayoutParams(l1);
}
else {
LayoutParams l = new LayoutParams(l2r - pid - (l1l),
layout1.getBottom() - layout1.getTop());
layout1.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout1.getLayoutParams().width,layout1.getHeight());
layout2.setLayoutParams(l1);
}
if (scroller.isFinished()) {
b2 = false;
b1 = true;
bt1 = false;
Log.d("finish", "DDDDDDDD");
return;
}
}
else {
if (!b1) {
//ToDO l1 c
if (x < l2r - pid) {
LayoutParams l = new LayoutParams(x - (l1l),
layout1.getBottom() - layout1.getTop());
layout1.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout1.getLayoutParams().width, layout1.getHeight());
layout2.setLayoutParams(l1);
} else {
LayoutParams l = new LayoutParams(l2r - pid - (l1l),
layout1.getBottom() - layout1.getTop());
layout1.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout1.getLayoutParams().width, layout1.getHeight());
layout2.setLayoutParams(l1);
Log.d("11", "c");
}
if (scroller.isFinished()) {
b1 = true;
bt1 = false;
Log.d("finish", "DDDDDDDD");
return;
}
}
if (b1) {
//ToDO l1 d
if (l2r - x - pid > 0) {
LayoutParams l = new LayoutParams(l1r + l2r - x - pid,
layout1.getBottom() - layout1.getTop());
layout1.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout1.getLayoutParams().width, layout1.getHeight());
layout2.setLayoutParams(l1);
} else {
LayoutParams l = new LayoutParams(l1r - l1l,
layout1.getBottom() - layout1.getTop());
layout1.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout1.getLayoutParams().width, layout1.getHeight());
layout2.setLayoutParams(l1);
Log.d("11", "d");
}
if (scroller.isFinished()) {
b1 = false;
bt1 = false;
Log.d("finish", "WWWWWW");
return;
}
}
}
}
if (bt2) {
if(b1){
if (x<l2r-pid){
LayoutParams l = new LayoutParams(l2r - pid - x ,
layout1.getBottom() - layout1.getTop());
layout1.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout1.getLayoutParams().width,layout1.getHeight());
layout2.setLayoutParams(l1);
}
else {
LayoutParams l = new LayoutParams(l1l + pid, layout1.getHeight());
layout1.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout1.getLayoutParams().width, layout1.getHeight());
layout2.setLayoutParams(l1);
}
if(scroller.isFinished()){
b1 = false;
b2 = true;
bt2 = false;
return;
}
}
else {
if (!b2) {
//ToDO l2 c
if (x < l2l) {
LayoutParams l = new LayoutParams(l2r - l2l + x,
layout2.getBottom() - layout2.getTop());
layout2.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout2.getLayoutParams().width, layout2.getHeight());
layout1.setLayoutParams(l1);
} else {
LayoutParams l = new LayoutParams(l2r - l1l - pid, layout2.getHeight());
layout2.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout2.getLayoutParams().width, layout2.getHeight());
layout1.setLayoutParams(l1);
Log.d("12", "c");
}
if (scroller.isFinished()) {
b2 = true;
bt2 = false;
return;
}
}
if (b2) {
//ToDO l2 d
if (x <= l2l) {
LayoutParams l = new LayoutParams(l2r - l2l + (l1r - l1l - x), layout2.getHeight());
layout2.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout2.getLayoutParams().width, layout2.getHeight());
layout1.setLayoutParams(l1);
} else {
LayoutParams l = new LayoutParams(l2r - l2l,
layout2.getBottom() - layout2.getTop());
layout2.setLayoutParams(l);
LayoutParams l1 = new LayoutParams(l2r - layout2.getLayoutParams().width, layout2.getHeight());
layout1.setLayoutParams(l1);
Log.d("12", "d");
}
if (scroller.isFinished()) {
b2 = false;
bt2 = false;
return;
}
}
}
}
invalidate();
}
}
通过Scroller我们然layout的变化更加舒心了。这样就大概实现了我的脑洞了。
这个是简单的实现,完整代码在github上可以获取到。过段时间(先找找看有木有实习以后看看有木有时间),我会把这个脑洞完善好。