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

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文件,以下是这个文件的全部代码:

  1. <spanstyle="font-size:18px;"><com.android.launcherex.DragLayer
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
  4. android:id="@+id/drag_layer"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <includelayout="@layout/all_apps"/>
  8. <!--Theworkspacecontains3screensofcells-->
  9. <com.android.launcherex.Workspace
  10. android:id="@+id/workspace"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:scrollbars="horizontal"
  14. android:fadeScrollbars="true"
  15. launcher:defaultScreen="2">
  16. <includeandroid:id="@+id/cell1"layout="@layout/workspace_screen"/>
  17. <includeandroid:id="@+id/cell2"layout="@layout/workspace_screen"/>
  18. <includeandroid:id="@+id/cell3"layout="@layout/workspace_screen"/>
  19. <includeandroid:id="@+id/cell4"layout="@layout/workspace_screen"/>
  20. <includeandroid:id="@+id/cell5"layout="@layout/workspace_screen"/>
  21. </com.android.launcherex.Workspace>
  22. <RelativeLayout
  23. android:id="@+id/dock_layout"
  24. android:layout_gravity="bottom|left"
  25. android:layout_width="match_parent"
  26. android:layout_height="95dip"
  27. >
  28. <com.android.launcherex.ClippedImageView
  29. android:id="@+id/previous_screen"
  30. android:layout_width="93dip"
  31. android:layout_height="@dimen/button_bar_height"
  32. android:layout_alignParentTop="true"
  33. android:layout_alignParentLeft="true"
  34. android:layout_marginLeft="6dip"
  35. android:scaleType="center"
  36. android:src="@drawable/home_arrows_left"
  37. android:onClick="previousScreen"
  38. launcher:ignoreZone="56dip"
  39. android:focusable="true"
  40. android:clickable="true"/>
  41. <com.android.launcherex.ClippedImageView
  42. android:id="@+id/next_screen"
  43. android:layout_width="93dip"
  44. android:layout_height="@dimen/button_bar_height"
  45. android:layout_alignParentTop="true"
  46. android:layout_alignParentRight="true"
  47. android:layout_marginRight="6dip"
  48. android:scaleType="center"
  49. android:src="@drawable/home_arrows_right"
  50. android:onClick="nextScreen"
  51. launcher:ignoreZone="-56dip"
  52. android:focusable="true"
  53. android:clickable="true"/>
  54. <com.android.launcherex.DeleteZone
  55. android:id="@+id/delete_zone"
  56. android:layout_width="@dimen/delete_zone_size"
  57. android:layout_height="@dimen/delete_zone_size"
  58. android:paddingLeft="@dimen/delete_zone_padding"
  59. android:layout_marginBottom="@dimen/dock_bar_height"
  60. android:layout_alignParentBottom="true"
  61. android:layout_centerHorizontal="true"
  62. android:scaleType="center"
  63. android:src="@drawable/delete_zone_selector"
  64. android:visibility="invisible"
  65. launcher:direction="vertical"
  66. />
  67. <RelativeLayout
  68. android:id="@+id/all_apps_button_cluster"
  69. android:layout_height="fill_parent"
  70. android:layout_width="@dimen/button_bar_height_portrait"
  71. android:layout_gravity="bottom|center_vertical"
  72. android:layout_marginBottom="@dimen/half_status_bar_height"
  73. >
  74. <!--<com.android.launcherex.HandleView
  75. style="@style/HotseatButton"
  76. android:id="@+id/all_apps_button"
  77. android:layout_centerVertical="true"
  78. android:layout_alignParentRight="true"
  79. android:src="@drawable/all_apps_button"
  80. launcher:direction="vertical"
  81. />-->
  82. <!--android:layout_below="@id/all_apps_button"-->
  83. <!--android:layout_above="@id/all_apps_button"-->
  84. <ImageView
  85. android:id="@+id/hotseat_left"
  86. style="@style/HotseatButton.Left"
  87. android:src="@drawable/hotseat_phone"
  88. android:visibility="gone"
  89. android:onClick="launchHotSeat"
  90. />
  91. <ImageView
  92. android:id="@+id/hotseat_right"
  93. style="@style/HotseatButton.Right"
  94. android:visibility="gone"
  95. android:src="@drawable/hotseat_browser"
  96. android:onClick="launchHotSeat"
  97. />
  98. </RelativeLayout>
  99. <com.android.launcherex.DockBar
  100. android:id="@+id/dockbar"
  101. android:layout_width="fill_parent"
  102. android:layout_height="@dimen/button_bar_height"
  103. android:background="@drawable/dock_bg"
  104. android:layout_alignParentBottom="true"
  105. launcher:direction="horizontal">
  106. <HorizontalScrollView
  107. android:id="@+id/dock_scroll_view"
  108. android:scrollbars="none"
  109. android:fadingEdge="none"
  110. android:saveEnabled="false"
  111. android:layout_width="fill_parent"
  112. android:layout_height="fill_parent">
  113. <LinearLayout
  114. android:orientation="horizontal"
  115. android:id="@+id/dock_item_holder"
  116. android:saveEnabled="false"
  117. android:layout_width="fill_parent"
  118. android:layout_height="fill_parent">
  119. <com.android.launcherex.HandleView
  120. android:id="@+id/all_apps_button"
  121. android:layout_centerVertical="true"
  122. android:src="@drawable/all_apps_button"
  123. launcher:direction="horizontal"
  124. android:layout_width="fill_parent"
  125. android:layout_height="fill_parent"
  126. android:focusable="true"
  127. android:clickable="true"/>
  128. </LinearLayout>
  129. </HorizontalScrollView>
  130. </com.android.launcherex.DockBar>
  131. </RelativeLayout>
  132. </com.android.launcherex.DragLayer>
  133. </span>

3、启用3D效果

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

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

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

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

  1. <spanstyle="font-size:18px;">publicstaticfinalintICON_WIDTH_PX=64;
  2. publicstaticfinalintICON_TEXTURE_WIDTH_PX=74;
  3. publicstaticfinalintSELECTION_TEXTURE_WIDTH_PX=74+20;
  4. publicstaticfinalintICON_HEIGHT_PX=64;
  5. publicstaticfinalintICON_TEXTURE_HEIGHT_PX=74;
  6. publicstaticfinalintSELECTION_TEXTURE_HEIGHT_PX=74+20;</span>

4、替换应用程序图标

主要是在IconCache中做了以下修改。修改的只是Launcher上显示的图标,长按Home键显示最近开启的应用时,图标还是原先默认的图标,这些就自己去改吧。
  1. <spanstyle="font-size:18px;">privateCacheEntrycacheLocked(ComponentNamecomponentName,ResolveInfoinfo){
  2. CacheEntryentry=mCache.get(componentName);
  3. if(entry==null){
  4. entry=newCacheEntry();
  5. mCache.put(componentName,entry);
  6. entry.title=info.loadLabel(mPackageManager).toString();
  7. if(entry.title==null){
  8. entry.title=info.activityInfo.name;
  9. }
  10. //entry.icon=Utilities.createIconBitmap(info.activityInfo.loadIcon(mPackageManager),mContext);
  11. entry.icon=suBIcon(info);
  12. }
  13. returnentry;
  14. }
  15. /**
  16. *ReplacethedefaultLaunchericon
  17. *@paraminfo
  18. *@return
  19. */
  20. privateBitmapsuBIcon(ResolveInfoinfo){
  21. Drawabledrawable=null;
  22. Stringpname=info.activityInfo.packageName;
  23. if(pname.equals(Constant.PACKAGE_VIDEOPLAYER)){
  24. drawable=mResources.getDrawable(R.drawable.lu_videoplayer);
  25. }elseif(pname.equals(Constant.PACKAGENAME_BROWSER)){
  26. drawable=mResources.getDrawable(R.drawable.lu_browser);
  27. }elseif(pname.equals(Constant.PACKAGE_CALCULATOR)){
  28. drawable=mResources.getDrawable(R.drawable.lu_calculator2);
  29. }elseif(pname.equals(Constant.PACKAGE_CALENDAR)){
  30. drawable=mResources.getDrawable(R.drawable.lu_calendar);
  31. }elseif(pname.equals(Constant.PACKAGE_CAMERA)){
  32. drawable=mResources.getDrawable(R.drawable.lu_camera);
  33. }elseif(pname.equals(Constant.PACKAGE_CLOCK)){
  34. drawable=mResources.getDrawable(R.drawable.lu_alarmclock);
  35. }elseif(pname.equals(Constant.PACKAGE_EMAIL)){
  36. drawable=mResources.getDrawable(R.drawable.lu_email);
  37. }elseif(pname.equals(Constant.PACKAGE_GALLERY)){
  38. drawable=mResources.getDrawable(R.drawable.lu_gallery);
  39. }elseif(pname.equals(Constant.PACKAGE_MUSIC)){
  40. drawable=mResources.getDrawable(R.drawable.lu_music);
  41. }elseif(pname.equals(Constant.PACKAGE_SETTINGS)){
  42. drawable=mResources.getDrawable(R.drawable.lu_settings);
  43. }
  44. if(drawable!=null){
  45. returnUtilities.createIconBitmap(drawable,mContext);
  46. }else{
  47. returnUtilities.createIconBitmap(info.activityInfo.loadIcon(mPackageManager),mContext);
  48. }
  49. }</span>

5、批量添加快捷方式

操作:长按桌面 -- Add to Home Screen点击Applications即显示所有安装应用程序列表。可任意选择多个应用程序的快捷方式添加到Launcher上。
1、Activity类:AddShortcutsActivity 主要用于显示安装应用程序信息和添加操作界面
2、列表适配: AddShortcutsAdapter 继承BaseAdapter。
3、快捷方式实体类:ShortcutsEntity
4、add_shortcuts_adapter.xml
5、add_shortcuts.xml

直接上代码:

  1. <spanstyle="font-size:18px;">packagecom.android.launcherex;
  2. importjava.util.ArrayList;
  3. importjava.util.List;
  4. importandroid.app.Activity;
  5. importandroid.content.Intent;
  6. importandroid.content.pm.ActivityInfo;
  7. importandroid.content.pm.PackageManager;
  8. importandroid.content.pm.ResolveInfo;
  9. importandroid.os.Bundle;
  10. importandroid.os.Parcelable;
  11. importandroid.util.Log;
  12. importandroid.view.View;
  13. importandroid.view.View.OnClickListener;
  14. importandroid.widget.AdapterView;
  15. importandroid.widget.AdapterView.OnItemClickListener;
  16. importandroid.widget.Button;
  17. importandroid.widget.ListView;
  18. importcom.android.launcher.R;
  19. publicclassAddShortcutsActivityextendsActivityimplementsOnClickListener,OnItemClickListener{
  20. privateAddShortcutsAdaptermShortcutsAdapter;
  21. privatefinalstaticStringTAG="AddShortcutsActivity";
  22. privateListViewlstShortcuts;
  23. privateButtonbtnAddShortcuts;
  24. privateButtonbtnCancelShortcuts;
  25. privateArrayList<ShortcutsEntity>mShortcutsEntity=newArrayList<ShortcutsEntity>();
  26. @Override
  27. protectedvoidonCreate(BundlesavedInstanceState){
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.add_shortcuts);
  30. setupViews();
  31. }
  32. privatevoidsetupViews(){
  33. lstShortcuts=(ListView)findViewById(R.id.shortcuts_list);
  34. //lstShortcuts.setItemsCanFocus(false);
  35. lstShortcuts.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  36. btnAddShortcuts=(Button)findViewById(R.id.shortcuts_add);
  37. btnCancelShortcuts=(Button)findViewById(R.id.shortcuts_cancel);
  38. btnAddShortcuts.setOnClickListener(this);
  39. btnCancelShortcuts.setOnClickListener(this);
  40. lstShortcuts.setOnItemClickListener(this);
  41. mShortcutsEntity=getAllShortcuts();
  42. mShortcutsAdapter=newAddShortcutsAdapter(mShortcutsEntity,this);
  43. lstShortcuts.setAdapter(mShortcutsAdapter);
  44. }
  45. publicvoidonClick(Viewarg0){
  46. switch(arg0.getId()){
  47. caseR.id.shortcuts_add:
  48. ArrayList<Intent>intents=getSelectedShortcuts();
  49. Intentintent=getIntent();
  50. //Readbaseintentfromextras,otherwiseassumedefault
  51. Parcelableparcel=intent.getParcelableExtra(Intent.EXTRA_INTENT);
  52. if(parcelinstanceofIntent){
  53. intent=(Intent)parcel;
  54. }else{
  55. intent=newIntent(Intent.ACTION_MAIN,null);
  56. intent.addCategory(Intent.CATEGORY_DEFAULT);
  57. }
  58. Bundlebundle=newBundle();
  59. bundle.putSerializable("intents",intents);
  60. intent.putExtras(bundle);
  61. setResult(Activity.RESULT_OK,intent);
  62. finish();
  63. break;
  64. caseR.id.shortcuts_cancel:
  65. finish();
  66. break;
  67. }
  68. }
  69. /**
  70. *Getalltheapplications
  71. *@return
  72. */
  73. privateArrayList<ShortcutsEntity>getAllShortcuts(){
  74. ArrayList<ShortcutsEntity>entitys=newArrayList<ShortcutsEntity>();
  75. Intentintent=newIntent(Intent.ACTION_MAIN);
  76. intent.addCategory(Intent.CATEGORY_LAUNCHER);
  77. PackageManagermPackageManager=this.getPackageManager();
  78. List<ResolveInfo>mAllApps=mPackageManager.queryIntentActivities(intent,0);
  79. intsize=mAllApps.size();
  80. for(inti=0;i<size;i++){
  81. ShortcutsEntityentity=newShortcutsEntity();
  82. ResolveInforesolveInfo=mAllApps.get(i);
  83. entity.Title=resolveInfo.loadLabel(mPackageManager);
  84. entity.Icon=resolveInfo.loadIcon(mPackageManager);
  85. entity.Info=resolveInfo;
  86. entitys.add(entity);
  87. Log.i(TAG,"entity.Title="+resolveInfo.loadLabel(mPackageManager));
  88. }
  89. //TODO:sort
  90. returnentitys;
  91. }
  92. publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){
  93. mShortcutsAdapter.setSelectItem(arg2);
  94. mShortcutsAdapter.notifyDataSetChanged();
  95. }
  96. /**
  97. *Beenselectedshortcuts
  98. *@return
  99. */
  100. privateArrayList<Intent>getSelectedShortcuts(){
  101. ArrayList<Intent>selecteds=newArrayList<Intent>();
  102. ArrayList<Boolean>isCheck=AddShortcutsAdapter.isChecked;
  103. intcheckSize=isCheck.size();
  104. for(inti=0;i<checkSize;i++){
  105. if(isCheck.get(i)){
  106. ShortcutsEntityentity=mShortcutsEntity.get(i);
  107. ActivityInfoactivityInfo=entity.Info.activityInfo;
  108. Intentintent=newIntent();
  109. intent.setClassName(activityInfo.applicationInfo.packageName,activityInfo.name);
  110. selecteds.add(intent);
  111. Log.i(TAG,"selecteditem.name="+mShortcutsEntity.get(i).Title);
  112. }
  113. }
  114. returnselecteds;
  115. }
  116. }
  117. </span>

  1. <spanstyle="font-size:18px;">packagecom.android.launcherex;
  2. importjava.util.ArrayList;
  3. importcom.android.launcher.R;
  4. importandroid.content.Context;
  5. importandroid.view.LayoutInflater;
  6. importandroid.view.View;
  7. importandroid.view.ViewGroup;
  8. importandroid.widget.BaseAdapter;
  9. importandroid.widget.CheckBox;
  10. importandroid.widget.ImageView;
  11. importandroid.widget.TextView;
  12. publicclassAddShortcutsAdapterextendsBaseAdapter{
  13. privateArrayList<ShortcutsEntity>mShortcuts;
  14. privatefinalLayoutInflatermInflater;
  15. privateContextmContext;
  16. publicstaticArrayList<Boolean>isChecked;
  17. publicAddShortcutsAdapter(ArrayList<ShortcutsEntity>shortcuts,Contextcontext)
  18. {
  19. mShortcuts=shortcuts;
  20. mContext=context;
  21. mInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  22. isChecked=newArrayList<Boolean>();
  23. intsize=mShortcuts.size();
  24. for(inti=0;i<size;i++){
  25. isChecked.add(false);
  26. }
  27. }
  28. publicintgetCount(){
  29. if(mShortcuts!=null){
  30. returnmShortcuts.size();
  31. }
  32. return0;
  33. }
  34. publicObjectgetItem(intarg0){
  35. if(mShortcuts!=null){
  36. returnmShortcuts.get(arg0);
  37. }
  38. returnnull;
  39. }
  40. publiclonggetItemId(intarg0){
  41. return0;
  42. }
  43. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  44. ViewHolderholder=null;
  45. ShortcutsEntityentity=mShortcuts.get(position);
  46. if(convertView==null){
  47. holder=newViewHolder();
  48. convertView=mInflater.inflate(R.layout.add_shortcuts_adapter,parent,false);
  49. holder.imageView=(ImageView)convertView.findViewById(R.id.sc_icon);
  50. holder.textView=(TextView)convertView.findViewById(R.id.sc_title);
  51. holder.checkBox=(CheckBox)convertView.findViewById(R.id.sc_checkbox);
  52. convertView.setTag(holder);
  53. }else{
  54. holder=(ViewHolder)convertView.getTag();
  55. }
  56. if(entity!=null){
  57. holder.textView.setText(entity.Title);
  58. //TODO:Modifythepicturesize.
  59. holder.imageView.setBackgroundDrawable(Utilities.getDrawable(entity.Icon));
  60. }
  61. if(isChecked!=null&&!isChecked.isEmpty()){
  62. holder.checkBox.setChecked(isChecked.get(position));
  63. }
  64. returnconvertView;
  65. }
  66. publicvoidsetSelectItem(intitem){
  67. if(isChecked.get(item)){
  68. isChecked.set(item,false);
  69. }else{
  70. isChecked.set(item,true);
  71. }
  72. }
  73. privateclassViewHolder{
  74. ImageViewimageView;
  75. TextViewtextView;
  76. CheckBoxcheckBox;
  77. }
  78. }
  79. </span>

  1. <spanstyle="font-size:18px;">packagecom.android.launcherex;
  2. importandroid.content.pm.ResolveInfo;
  3. importandroid.graphics.drawable.Drawable;
  4. publicclassShortcutsEntity{
  5. publicCharSequenceTitle;
  6. publicDrawableIcon;
  7. publicResolveInfoInfo;
  8. }
  9. </span>

  1. <spanstyle="font-size:18px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <ImageView
  8. android:id="@+id/sc_icon"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentTop="true"
  12. android:layout_alignParentLeft="true"
  13. android:layout_marginLeft="6dip"/>
  14. <TextView
  15. android:id="@+id/sc_title"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_alignParentTop="true"
  19. android:layout_toRightOf="@+id/sc_icon"
  20. android:layout_marginLeft="4dip"/>
  21. <CheckBox
  22. android:id="@+id/sc_checkbox"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_alignParentTop="true"
  26. android:layout_alignParentRight="true"
  27. android:focusable="false"
  28. android:layout_marginRight="6dip"/>
  29. </RelativeLayout>
  30. </span>

  1. <spanstyle="font-size:18px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <ListView
  8. android:id="@+id/shortcuts_list"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentLeft="true"
  12. android:layout_alignParentTop="true"
  13. android:layout_above="@+id/shortcuts_layout"
  14. />
  15. <LinearLayout
  16. android:id="@+id/shortcuts_layout"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:orientation="horizontal"
  20. android:layout_alignParentLeft="true"
  21. android:layout_alignParentBottom="true"
  22. >
  23. <Button
  24. android:id="@+id/shortcuts_add"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_weight="1"
  28. android:text="@string/shortcuts_add_text"/>
  29. <Button
  30. android:id="@+id/shortcuts_cancel"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_weight="1"
  34. android:text="@string/shortcuts_cancel_text"
  35. />
  36. </LinearLayout>
  37. </RelativeLayout>
  38. </span>

最后在Launcher.java的onActivityResult方法中添加如下代码,即大功告成
  1. <spanstyle="font-size:18px;">Bundlebundle=data.getExtras();
  2. ArrayList<Intent>intents=(ArrayList<Intent>)bundle.getSerializable("intents");
  3. if(intents!=null&&!intents.isEmpty()){
  4. intsize=intents.size();
  5. for(inti=0;i<size;i++){
  6. mAddItemCellInfo=newCellLayout.CellInfo();//Shortcuttosolvetheproblemofoverlapping
  7. completeAddApplication(this,intents.get(i),mAddItemCellInfo);
  8. }
  9. }
  10. break;</span>


你可能感兴趣的:(android)