Launcher中做到长按时,别的图标自动让位(仿Iphone桌面)

在china-android-dev@googlegroups 邮件组看到有个朋友提问: 


在 2011年6月4日 下午4:34,彭海涛  <penghaitao19860226@gmail.com>写道:
     想问一下,刷过MIUI ROM的同学,知道那个图标位置挪动是怎么实现的吗?就是按住桌面图表之后可以改变图标的顺序,按着移动到别的图标上面的时候,别的图标会主动给他让位。这是怎么做的?
正好我做过Launcher 的相关工作,把我的思路讲下:
      长按拖动图标时,将长按的View添加到 一个新的windows中. 然后将所点击桌面的View 隐藏掉.  
按着移动到别的图标上面的时候,别的图标会主动给他让位。其实是多个Animation。每个cell的animation结束之后,对其View的进行重新布局。 毕竟Animation给人的只是假象,不会去移动view的位置。  具体的实现代码片段: 
 public void startViewAnimation(CellLayout cellLayout, View startCell, int screen) {
// reCalculateLayout();
cellLayout.isNeedReLayout = false;
int[] cellI = new int[2];
int index = screen * G.MAX_ONE_SCREEN;
for (int i = 0; i < G.MAX_ONE_SCREEN; i++) {
if (index >= viewList.size()) {
break;
}
if (G.isPort) {
indexToPortCell(i, cellI);
} else {
indexToLandCell(i, cellI);
}
View cell = viewList.get(index);
CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
lp.cellX = cellI[0];
lp.cellY = cellI[1];
cellLayout.setupLayoutParams(lp);
if (cell == startCell) {
cell.layout(lp.x, lp.y, lp.x + lp.width, lp.y + lp.height);
index++;
continue;
}
translate(cell, cell.getLeft(), lp.x, cell.getTop(), lp.y);
index++;
}
}
public void translate(View v, float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
RecordTranslateAnimation ani = (RecordTranslateAnimation) v.getAnimation();
float fromX = fromXDelta;
float fromY = fromYDelta;
if (ani != null && v.getVisibility() != GONE) {
fromX = ani.toX;
fromY = ani.toY;
CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
v.clearAnimation();
v.layout((int) fromX, (int) fromY, (int) fromX + lp.width, (int) fromY + lp.height);
}
if (toXDelta - fromX == 0 && toYDelta - fromY == 0) {
return;
}
TranslateAnimation trans = new RecordTranslateAnimation(0, toXDelta - fromX, 0, toYDelta - fromY, toXDelta,
toYDelta);
trans.setInterpolator(new AccelerateDecelerateInterpolator());
trans.setDuration(250);
trans.setAnimationListener(new RecordTranslateAnimationListener(v));
v.startAnimation(trans);
}
public class RecordTranslateAnimationListener implements AnimationListener {
private View view;
public RecordTranslateAnimationListener(View view) {
this.view = view;
}
@Override
public void onAnimationEnd(Animation animation) {
RecordTranslateAnimation ani = (RecordTranslateAnimation) animation;
if (view.getVisibility() != GONE) {
float toX = ani.toX;
float toY = ani.toY;
CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
view.clearAnimation();
view.layout((int) toX, (int) toY, (int) toX + lp.width, (int) toY + lp.height);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
public static class RecordTranslateAnimation extends TranslateAnimation {
public float toX;
public float toY;
public RecordTranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta, float toX,
float toY) {
super(fromXDelta, toXDelta, fromYDelta, toYDelta);
this.toX = toX;
this.toY = toY;
}
}

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值