您的位置:首页 > 移动开发 > Android开发

Android2.2 Luncher2扩展(仿Ipad DockBar、HTC预览图、批量添加快捷方式、启用3D效果等)

2011-08-23 12:49 603 查看

1、给Launcher添加DockBar

2、修改Launcher长安AllApps的预览图

(1、2两点请参考何明桂何兄的博客,地址分别为:http://blog.csdn.net/hmg25/article/details/6289438 http://blog.csdn.net/hmg25/article/details/6289725)DockBar的实现,何兄只提供了竖屏,我在其博客的评论中看到不少人说是要实现横屏,下面我将我的实现帖出来。主要是修改layout-land/launcher.xml文件,以下是这个文件的全部代码:
<com.android.launcherex.DragLayer
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"

    android:id="@+id/drag_layer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/all_apps" />

    <!-- The workspace contains 3 screens of cells -->
    <com.android.launcherex.Workspace
        android:id="@+id/workspace"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal"
        android:fadeScrollbars="true"
        launcher:defaultScreen="2">

        <include android:id="@+id/cell1" layout="@layout/workspace_screen" />
        <include android:id="@+id/cell2" layout="@layout/workspace_screen" />
        <include android:id="@+id/cell3" layout="@layout/workspace_screen" />
        <include android:id="@+id/cell4" layout="@layout/workspace_screen" />
        <include android:id="@+id/cell5" layout="@layout/workspace_screen" />

    </com.android.launcherex.Workspace>

	<RelativeLayout 
		android:id="@+id/dock_layout"
		android:layout_gravity="bottom|left"
		android:layout_width="match_parent"
	    android:layout_height="95dip"
		>
	    <com.android.launcherex.ClippedImageView
	        android:id="@+id/previous_screen"
	        android:layout_width="93dip"
	        android:layout_height="@dimen/button_bar_height"
	        android:layout_alignParentTop="true"
			android:layout_alignParentLeft="true"
	        android:layout_marginLeft="6dip"
	        android:scaleType="center"
	        android:src="@drawable/home_arrows_left"
	        android:onClick="previousScreen"
	        launcher:ignoreZone="56dip"
	        android:focusable="true"
	        android:clickable="true" />
	
	    <com.android.launcherex.ClippedImageView
	        android:id="@+id/next_screen"
	        android:layout_width="93dip"
	        android:layout_height="@dimen/button_bar_height"
	        android:layout_alignParentTop="true"
			android:layout_alignParentRight="true"
	        android:layout_marginRight="6dip"
	        android:scaleType="center"
	        android:src="@drawable/home_arrows_right"
	        android:onClick="nextScreen"
	        launcher:ignoreZone="-56dip"
	        android:focusable="true"
	        android:clickable="true" />
	
	    <com.android.launcherex.DeleteZone
	        android:id="@+id/delete_zone"
	        android:layout_width="@dimen/delete_zone_size"
	        android:layout_height="@dimen/delete_zone_size"
	        android:paddingLeft="@dimen/delete_zone_padding"
	        android:layout_marginBottom="@dimen/dock_bar_height"
	        android:layout_alignParentBottom="true"
			android:layout_centerHorizontal="true"
	
	        android:scaleType="center"
	        android:src="@drawable/delete_zone_selector"
	        android:visibility="invisible"
	        launcher:direction="vertical"
	        />
	
	    <RelativeLayout
	        android:id="@+id/all_apps_button_cluster"
	        android:layout_height="fill_parent"
	        android:layout_width="@dimen/button_bar_height_portrait"
	        android:layout_gravity="bottom|center_vertical"
	        android:layout_marginBottom="@dimen/half_status_bar_height"
	        >
	
	       <!-- <com.android.launcherex.HandleView
	            style="@style/HotseatButton"
	            android:id="@+id/all_apps_button"
	            android:layout_centerVertical="true"
	            android:layout_alignParentRight="true"
	
	            android:src="@drawable/all_apps_button"
	            launcher:direction="vertical"
	            />-->
	<!-- android:layout_below="@id/all_apps_button" -->
	<!--  android:layout_above="@id/all_apps_button" -->
	        <ImageView
	            android:id="@+id/hotseat_left"
	            style="@style/HotseatButton.Left"
	
	            android:src="@drawable/hotseat_phone"
				android:visibility="gone"
	            android:onClick="launchHotSeat"
	            />
	
	        <ImageView
	            android:id="@+id/hotseat_right"
	            style="@style/HotseatButton.Right"
	           android:visibility="gone"
	            android:src="@drawable/hotseat_browser"
	            android:onClick="launchHotSeat"
	            />
	    </RelativeLayout>
	    <com.android.launcherex.DockBar
			android:id="@+id/dockbar" 
			android:layout_width="fill_parent"
			android:layout_height="@dimen/button_bar_height"
			android:background="@drawable/dock_bg"
			android:layout_alignParentBottom="true"
			launcher:direction="horizontal">
			
			<HorizontalScrollView 
				android:id="@+id/dock_scroll_view"
				android:scrollbars="none"
				android:fadingEdge="none"
				android:saveEnabled="false" 
				android:layout_width="fill_parent"
				android:layout_height="fill_parent">
				<LinearLayout 
					android:orientation="horizontal"
					android:id="@+id/dock_item_holder" 
					android:saveEnabled="false"
					android:layout_width="fill_parent" 
					android:layout_height="fill_parent">
					<com.android.launcherex.HandleView
						android:id="@+id/all_apps_button" 
						android:layout_centerVertical="true"
						android:src="@drawable/all_apps_button" 
						launcher:direction="horizontal"
						android:layout_width="fill_parent" 
						android:layout_height="fill_parent"
						android:focusable="true" 
						android:clickable="true" />
				</LinearLayout>
			</HorizontalScrollView>
		</com.android.launcherex.DockBar>
	</RelativeLayout>
</com.android.launcherex.DragLayer>


3、启用3D效果

3D效果启动很简单,只需要将all_apps.xml文件下的

<include layout="@layout/all_apps_2d" />改为:

<include layout="@layout/all_apps_3d" />

即可。当然你可能还需要对图标做一些调整。我主要是调整了一下几个参数:

public static final int ICON_WIDTH_PX = 64;
        public static final int ICON_TEXTURE_WIDTH_PX = 74;
        public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;

        public static final int ICON_HEIGHT_PX = 64;
        public static final int ICON_TEXTURE_HEIGHT_PX = 74;
        public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;


4、替换应用程序图标

主要是在IconCache中做了以下修改。修改的只是Launcher上显示的图标,长按Home键显示最近开启的应用时,图标还是原先默认的图标,这些就自己去改吧。

private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) {
        CacheEntry entry = mCache.get(componentName);
        if (entry == null) {
            entry = new CacheEntry();
            mCache.put(componentName, entry);
            entry.title = info.loadLabel(mPackageManager).toString();
            if (entry.title == null) {
                entry.title = info.activityInfo.name;
            }
           // entry.icon = Utilities.createIconBitmap(info.activityInfo.loadIcon(mPackageManager), mContext);
            
            entry.icon = suBIcon(info);
        }
        return entry;
    }
    
    /**
     * Replace the default Launcher icon
     * @param info
     * @return
     */
    private Bitmap suBIcon(ResolveInfo info){
    	Drawable drawable = null;
    	String pname = info.activityInfo.packageName;
    	
        if(pname.equals(Constant.PACKAGE_VIDEOPLAYER)){
        	drawable = mResources.getDrawable(R.drawable.lu_videoplayer);
        }else if(pname.equals(Constant.PACKAGENAME_BROWSER)){
        	drawable = mResources.getDrawable(R.drawable.lu_browser);
        }else if(pname.equals(Constant.PACKAGE_CALCULATOR)){
        	drawable = mResources.getDrawable(R.drawable.lu_calculator2);
        }else if(pname.equals(Constant.PACKAGE_CALENDAR)){
        	drawable = mResources.getDrawable(R.drawable.lu_calendar);
        }else if(pname.equals(Constant.PACKAGE_CAMERA)){
        	drawable = mResources.getDrawable(R.drawable.lu_camera);
        }else if(pname.equals(Constant.PACKAGE_CLOCK)){
        	drawable = mResources.getDrawable(R.drawable.lu_alarmclock);
        }else if(pname.equals(Constant.PACKAGE_EMAIL)){
        	drawable = mResources.getDrawable(R.drawable.lu_email);
        }else if(pname.equals(Constant.PACKAGE_GALLERY)){
        	drawable = mResources.getDrawable(R.drawable.lu_gallery);
        }else if(pname.equals(Constant.PACKAGE_MUSIC)){
        	drawable = mResources.getDrawable(R.drawable.lu_music);
        }else if(pname.equals(Constant.PACKAGE_SETTINGS)){
        	drawable = mResources.getDrawable(R.drawable.lu_settings);
        }
        
        if(drawable!=null){
        	return Utilities.createIconBitmap(drawable, mContext);
        }else{
        	return Utilities.createIconBitmap(info.activityInfo.loadIcon(mPackageManager), mContext);
        }
    }


5、批量添加快捷方式

操作:长按桌面 -- Add to Home Screen点击Applications即显示所有安装应用程序列表。可任意选择多个应用程序的快捷方式添加到Launcher上。

1、Activity类:AddShortcutsActivity 主要用于显示安装应用程序信息和添加操作界面

2、列表适配: AddShortcutsAdapter 继承BaseAdapter。

3、快捷方式实体类:ShortcutsEntity

4、add_shortcuts_adapter.xml

5、add_shortcuts.xml

直接上代码:

package com.android.launcherex;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.android.launcher.R;

public class AddShortcutsActivity extends Activity implements OnClickListener,OnItemClickListener{

	private AddShortcutsAdapter mShortcutsAdapter;
	
	private final static String TAG = "AddShortcutsActivity";
	
	private ListView lstShortcuts;
	private Button btnAddShortcuts;
	private Button btnCancelShortcuts;
	
	private ArrayList<ShortcutsEntity> mShortcutsEntity = new ArrayList<ShortcutsEntity>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_shortcuts);
		
		setupViews();
	}
	
	private void setupViews(){
		lstShortcuts = (ListView) findViewById(R.id.shortcuts_list);
//		lstShortcuts.setItemsCanFocus(false);
		lstShortcuts.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
		
		btnAddShortcuts = (Button) findViewById(R.id.shortcuts_add);
		btnCancelShortcuts=(Button) findViewById(R.id.shortcuts_cancel);
		
		btnAddShortcuts.setOnClickListener(this);
		btnCancelShortcuts.setOnClickListener(this);
		lstShortcuts.setOnItemClickListener(this);
		
		mShortcutsEntity = getAllShortcuts();
		
		mShortcutsAdapter = new AddShortcutsAdapter(mShortcutsEntity,this);
		lstShortcuts.setAdapter(mShortcutsAdapter);
		
	}

	public void onClick(View arg0) {
		switch(arg0.getId()){
		case R.id.shortcuts_add:
			
			ArrayList<Intent> intents = getSelectedShortcuts();
			Intent intent = getIntent();
	        // Read base intent from extras, otherwise assume default
	        Parcelable parcel = intent.getParcelableExtra(Intent.EXTRA_INTENT);
			
	        if (parcel instanceof Intent) {
	        	intent = (Intent) parcel;
	        } else {
	        	intent = new Intent(Intent.ACTION_MAIN, null);
	        	intent.addCategory(Intent.CATEGORY_DEFAULT);
	        }
	        
	        Bundle bundle = new Bundle();
            bundle.putSerializable("intents", intents);
            intent.putExtras(bundle);
            
			setResult(Activity.RESULT_OK,intent);
			
			
			finish();
			
			break;
		case R.id.shortcuts_cancel:
			finish();
			break;
		}
	}
	
	/**
	 * Get all the applications
	 * @return
	 */
	private ArrayList<ShortcutsEntity> getAllShortcuts(){
		
		ArrayList<ShortcutsEntity> entitys = new ArrayList<ShortcutsEntity>();
		
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		
		PackageManager mPackageManager = this.getPackageManager();
		List<ResolveInfo> mAllApps = mPackageManager.queryIntentActivities(intent, 0);
		
		int size = mAllApps.size();
		
		for(int i=0;i<size;i++){
			
			ShortcutsEntity entity = new ShortcutsEntity();
			ResolveInfo resolveInfo = mAllApps.get(i);
			entity.Title = resolveInfo.loadLabel(mPackageManager);
			entity.Icon = resolveInfo.loadIcon(mPackageManager);
			entity.Info = resolveInfo;
			entitys.add(entity);
			
			Log.i(TAG, "entity.Title="+resolveInfo.loadLabel(mPackageManager));
		}
		
		//TODO: sort
		
		return entitys;
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		mShortcutsAdapter.setSelectItem(arg2);
		mShortcutsAdapter.notifyDataSetChanged();
	}
	
	/**
	 * Been selected shortcuts
	 * @return
	 */
	private ArrayList<Intent> getSelectedShortcuts(){
		
		ArrayList<Intent> selecteds = new ArrayList<Intent>();
		
		ArrayList<Boolean> isCheck = AddShortcutsAdapter.isChecked;
		int checkSize =  isCheck.size();
		for(int i=0;i<checkSize;i++){
			if(isCheck.get(i)){
				ShortcutsEntity entity = mShortcutsEntity.get(i);
				ActivityInfo activityInfo = entity.Info.activityInfo;
				Intent intent = new Intent();
				intent.setClassName(activityInfo.applicationInfo.packageName,activityInfo.name);
				selecteds.add(intent);
				Log.i(TAG, "selected item.name="+mShortcutsEntity.get(i).Title);
			}
		}
		
		return selecteds;
	}
}


package com.android.launcherex;

import java.util.ArrayList;

import com.android.launcher.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class AddShortcutsAdapter extends BaseAdapter {

	private ArrayList<ShortcutsEntity> mShortcuts;
	private final LayoutInflater mInflater;
	private Context mContext;
	public static ArrayList<Boolean> isChecked;
	
	public AddShortcutsAdapter(ArrayList<ShortcutsEntity> shortcuts,Context context)
	{
		mShortcuts = shortcuts;
		mContext = context;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		isChecked = new ArrayList<Boolean>();
		
		int size = mShortcuts.size();
		for(int i=0;i<size;i++){
			isChecked.add(false);
		}
	}
	
	public int getCount() {
		if(mShortcuts!=null){
			return mShortcuts.size();
		}
		return 0;
	}

	public Object getItem(int arg0) {
		if(mShortcuts!=null){
			return mShortcuts.get(arg0);
		}
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		ShortcutsEntity entity = mShortcuts.get(position);
		
		if(convertView==null){
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.add_shortcuts_adapter, parent, false);
			
			holder.imageView = (ImageView) convertView.findViewById(R.id.sc_icon);
			holder.textView = (TextView) convertView.findViewById(R.id.sc_title);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.sc_checkbox);
			
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		if(entity!=null){
			holder.textView.setText(entity.Title);
			//TODO: Modify the picture size.
			holder.imageView.setBackgroundDrawable(Utilities.getDrawable(entity.Icon));
		}
		
		if(isChecked!=null && !isChecked.isEmpty()){
			holder.checkBox.setChecked(isChecked.get(position));
		}
		
		return convertView;
	}
	
	public void setSelectItem(int item){
		if(isChecked.get(item)){
			isChecked.set(item, false);
		}else{
			isChecked.set(item,true);
		}
	}
	
	private class ViewHolder{
		ImageView imageView;
		TextView textView;
		CheckBox checkBox;
	}
}


package com.android.launcherex;

import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;

public class ShortcutsEntity {

	public CharSequence Title;
	public Drawable Icon;
	public ResolveInfo Info;
}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <ImageView 
    	android:id="@+id/sc_icon"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_alignParentTop="true"
		android:layout_alignParentLeft="true"
		android:layout_marginLeft="6dip"/>
    <TextView 
    	android:id="@+id/sc_title"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_alignParentTop="true"
    	android:layout_toRightOf="@+id/sc_icon"
    	android:layout_marginLeft="4dip"/>
    <CheckBox
    	android:id="@+id/sc_checkbox"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_alignParentTop="true"
		android:layout_alignParentRight="true"
		android:focusable="false"
		android:layout_marginRight="6dip"/>
</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    
    <ListView 
    	android:id="@+id/shortcuts_list"
    	android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:layout_alignParentLeft="true"
    	android:layout_alignParentTop="true"
    	android:layout_above="@+id/shortcuts_layout"
    	/>
    <LinearLayout 
    	android:id="@+id/shortcuts_layout"
    	android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:orientation="horizontal"
    	android:layout_alignParentLeft="true"
    	android:layout_alignParentBottom="true"
    	>
    	<Button 
    		android:id="@+id/shortcuts_add"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_weight="1"
    		android:text="@string/shortcuts_add_text"/>
    		
    	<Button 
    		android:id="@+id/shortcuts_cancel"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_weight="1"
    		android:text="@string/shortcuts_cancel_text"
    		/>
    </LinearLayout>
</RelativeLayout>


最后在Launcher.java的onActivityResult方法中添加如下代码,即大功告成
Bundle bundle = data.getExtras();
					ArrayList<Intent> intents = (ArrayList<Intent>) bundle.getSerializable("intents");
					
					if (intents != null && !intents.isEmpty()) {
						int size = intents.size();
						for (int i = 0; i < size; i++) {
							mAddItemCellInfo = new CellLayout.CellInfo(); // Shortcut to solve the problem of overlapping
							completeAddApplication(this, intents.get(i),mAddItemCellInfo);
						}
					}
                	break;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: