shake phone and rearrange workspace apps

新增功能,基本上国内手机产商都会加上的功能,其实主要做的就是桌面图标重新排列的算法。

Launcher.java继承SensorEventListener重写监听接口

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
        int sensorType = event.sensor.getType();  
        float[] values = event.values; 
        long vibrateTime = 200L;
  
        if (sensorType == Sensor.TYPE_ACCELEROMETER)  
        {  
  
        	int value = 15;
            if ((Math.abs(values[0]) > value || Math.abs(values[1]) > value || Math  
                    .abs(values[2]) > value + 10))  
            {  
            	if (mWorkspace.isInOverviewMode()){
                	if (CommonUtils.isFastDoubleClick()) {
                	
                	} else {
	            		mWorkspace.rearrangeApps();
	            		mVibrator.vibrate(vibrateTime); 
	            	
                	}
            	}
            }  
        }  
	}
CommonUtils 这个类是防止连续触发用的

class CommonUtils {  
    private static long lastClickTime;  
    public static boolean isFastDoubleClick() {  
        long time = System.currentTimeMillis();  
         
        long timeD = time - lastClickTime; 
        if ( 0 < timeD && timeD < 1000) { 
        	return true; 
        } 
        lastClickTime = time; 
        return false; 
    }
}

workspace.java

    public void rearrangeApps(){
    	int count = getChildCount();
    	for (int i=0 ; i<count ; i++ ){
    		long screenID = getScreenIdForPageIndex(i);
    		if (screenID == CUSTOM_CONTENT_SCREEN_ID){
    			continue;
    		}
			
    		if (i == 1){
        		CellLayout cl = (CellLayout)getChildAt(i);
        		cl.setUseTempCoords(false);
        		cl.rearrangeHomePage();
    		} else {
        		CellLayout cl = (CellLayout)getChildAt(i);
        		cl.setUseTempCoords(false);
        		cl.rearrangeAppItems();
    		}
    		
    	}
    }

CellLayout.java

    public void rearrangeAppItems(){
    	ArrayList<View> list = new ArrayList<View>();
    	ArrayList<String> blackList = new ArrayList<String>();
    	int neadReoderSize = getShortcutsAndWidgets().getChildCount();
    	for (int i=0; i<getShortcutsAndWidgets().getChildCount(); i++){
    		View tempView = getShortcutsAndWidgets().getChildAt(i);
    		list.add(tempView);
    		ItemInfo info = (ItemInfo) tempView.getTag();
            if (info.spanX > 1 || info.spanY > 1){
            	neadReoderSize --;
            	for (int y=info.cellY; y<info.cellY+info.spanY; y++){
            		for (int x=info.cellX; x<info.cellX+info.spanX; x++){
        				int a = x + y*4;
        				blackList.add(a + "");
        				if (a < neadReoderSize) {
        					neadReoderSize ++;
        				}
            		}
            	}
            }
    	}
    	
    	for (int i=0; i<list.size(); i++){
    		ItemInfo info = (ItemInfo) list.get(i).getTag();
            if (info.spanX > 1 || info.spanY > 1){
            } else {
                int oldPosition = info.cellX + info.cellY*4; 
                if (oldPosition < neadReoderSize ) {
                	blackList.add(oldPosition + "");
                	info.neadReoder = false;
                } else {
                	info.neadReoder = true;
                }
            }

    	}
    	
//    	getShortcutsAndWidgets().removeAllViews();
        int position = 0;
        int newX, newY, rank;
        rank = 0;
        InvariantDeviceProfile grid = LauncherAppState.getInstance().getInvariantDeviceProfile();

        for (int i=0; i<list.size(); i++){
        	View v = list.get(i);
        	if (v != null){
//        		CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
        		
                newX = position % grid.numColumns;
                newY = position / grid.numColumns;
                ItemInfo info = (ItemInfo) v.getTag();

                if (blackList != null){
                	while(blackList.contains(position + "")){
                		newX ++;
                        rank ++;
                        position++;
                		if (newX > 3){
                			newY++;
                			newX = 0;

                		}
                	}
                }
                
                
                if (info.cellX != newX || info.cellY != newY || info.rank != rank) {
                	if (info.spanX == 1 && info.spanY == 1 & info.neadReoder ){
                        info.cellX = newX;
                        info.cellY = newY;
                        info.rank = rank;
	                    
	                    if (animateChildToPosition(v, info.cellX, info.cellY, REORDER_ANIMATION_DURATION, 0, true, true)){
	                    	Log.d("txk", "info.title=" + info.title + "; info.cellX=" + info.cellX + "; info.cellY=" + info.cellY + ";newX=" + newX + ";newY=" + newY);
		                    LauncherModel.modifyItemInDatabase(getContext(), info,
	                                info.container, info.screenId, info.cellX, info.cellY, info.spanX, info.spanY);
	                    }
                	} else {
                        rank --;
                        position --;
                	}

                }

        	}
            rank ++;
            position++;
        }
    }
    
    public void rearrangeHomePage(){
    	ArrayList<View> list = new ArrayList<View>();
    	ArrayList<String> blackList = new ArrayList<String>();
    	int neadReoderSize = 20 - getShortcutsAndWidgets().getChildCount();
    	for (int i=0; i<getShortcutsAndWidgets().getChildCount(); i++){
    		View tempView = getShortcutsAndWidgets().getChildAt(i);
    		list.add(tempView);
    		ItemInfo info = (ItemInfo) tempView.getTag();
            if (info.spanX > 1 || info.spanY > 1){
            	neadReoderSize ++;
            	for (int y=info.cellY; y<info.cellY+info.spanY; y++){
            		for (int x=info.cellX; x<info.cellX+info.spanX; x++){
            				int a = x + y*4;
            				blackList.add(a + "");
            				if (a > neadReoderSize) {
            					neadReoderSize --;
            				}
            		}
            	}
            }

    	}
    	
    	for (int i=0; i<list.size(); i++){
    		ItemInfo info = (ItemInfo) list.get(i).getTag();
            if (info.spanX > 1 || info.spanY > 1){
            } else {
                int oldPosition = info.cellX + info.cellY*4; 
                if (oldPosition > neadReoderSize ) {
                	blackList.add(oldPosition + "");
                	info.neadReoder = false;
                } else {
                	info.neadReoder = true;
                }
            }

    	}
//    	getShortcutsAndWidgets().removeAllViews();
        InvariantDeviceProfile grid = LauncherAppState.getInstance().getInvariantDeviceProfile();
        int position = grid.numRows * grid.numColumns - 1;
        int newX, newY, rank;
        rank = grid.numRows * grid.numColumns - 1;

        for (int i=0; i<list.size(); i++){
        	View v = list.get(i);
        	if (v != null){
//        		CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
                newX = position % grid.numColumns;
                newY = position / grid.numColumns;
                ItemInfo info = (ItemInfo) v.getTag();

                if (blackList != null){
                	while(blackList.contains(position + "")){
                		newX --;
                        rank --;
                        position --;
                		if (newX < 0 ){
                			newY--;
                			newX = 3;

                		}
                	}
                }
                
                if (info.cellX != newX || info.cellY != newY || info.rank != rank) {
                   
                	if (info.spanX == 1 && info.spanY == 1 & info.neadReoder ){
                		info.cellX = newX;
	                    info.cellY = newY;
	                    info.rank = rank;
	                    if (animateChildToPosition(v, info.cellX, info.cellY, REORDER_ANIMATION_DURATION, 0, true, true)){
		                    LauncherModel.modifyItemInDatabase(getContext(), info,
	                                info.container, info.screenId, info.cellX, info.cellY, info.spanX, info.spanY);
	                    }
                	} else {
                        rank ++;
                        position ++;
                	}
                }
        	}
            rank --;
            position--;
        }
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值