Launcher之Dock细节篇

来源:互联网 发布:java重载和覆盖的区别 编辑:程序博客网 时间:2024/05/16 13:46
 

 

在前面一篇文章中,大致介绍了怎么仿Mac Dock效果,有的朋友问起那个梯形怎么实现的,其实这个很简单,就是一张背景图片,不过你要先理解.9图片代表的含义,这里有一片文章有介绍,不过大家最好是亲身体验下,这样的话理解更深入。 

这个图片就是我们项目中用到的图片: 
 

这个就是我显示在Launcher主界面的自定义类:

01.public class DockView extends LinearLayout implements DropTarget,DragSource, DragController.DragListener,OnClickListener, View.OnLongClickListener {
02./**
03.* @author jezz
04.*
05.*/
06. 
07.public DockView(Context context){
08.super(context);
09.}
10. 
11.public DockView(Context context, AttributeSet attrs) {
12.super(context, attrs);
13.mDockBgId = R.drawable.shortcut_selector;
14.}
15. 
16.public void init(){
17.//初始化操作
18.}
19. 
20. 
21.public boolean acceptDrop(DragSource source, int x, int y, int xOffset,int yOffset, Object dragInfo) {
22.//接受什么类型的图标
23.final ItemInfo item = (ItemInfo) dragInfo;
24.if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET
25.|| item.itemType == LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER
26.|| item.itemType == LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER
27.|| item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME
28.|| item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH
29.|| item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_CLOCK) {
30.return false;
31.}
32.}
33. 
34.public void onDragEnter(DragSource source, int x, int y, int xOffset,intyOffset, Object dragInfo) {
35.//拖入进入区域
36.setBackgroundResource(R.drawable.dock_press_bg);
37.}
38. 
39.public void onDragExit(DragSource source, int x, int y, int xOffset,int yOffset, Object dragInfo) {
40.//拖动离开区域
41.setBackgroundResource(R.drawable.dock_bg);
42.}
43. 
44.public void onDragOver(DragSource source, int x, int y, int xOffset,int yOffset, Object dragInfo) {
45.}
46. 
47.public void onDrop(DragSource source, int x, int y, int xOffset,int yOffset, Object dragInfo) {
48.//拖动释放
49.}
50. 
51.public void onDragEnd() {
52.}
53. 
54.public void onDragStart(View v, DragSource source, Object info,int dragAction) {
55.//开始拖动
56.}
57. 
58.public void onClick(View v) {
59.//单击打开app
60.ImageView view=(ImageView)v;
61.DockInfo dockInfo=(DockInfo)view.getTag();
62.try {
63.Intent i = Intent.getIntent(dockInfo.info.intent.toUri(0));
64.mLauncher.startActivitySafely(i);
65.catch (URISyntaxException e) {
66.e.printStackTrace();
67.}
68.}
69. 
70.public boolean onLongClick(View v) {
71.//长按实现拖动
72.}
73. 
74.public void onDropCompleted(View target, boolean success) {
75.//拖动释放事件
76.}
77.}

这个三个接口DropTarget,DragSource, DragController.DragListener是launcher自己定义的,你必须在Launcher启动的时候,将你的这几个接口实现在DragLayer.java里面,要注意的细节很多,你们多研究下代码就知道了。 

自定义的类放在layout-land下面,有个launcher.xml写入到文件的最下面:
1.<com.android.launcher.DockView
2.android:id="@+id/dock_view"
3.android:background="@drawable/dock_bg"
4.android:layout_width="wrap_content"
5.android:layout_height="wrap_content"
6.android:layout_gravity="bottom|center_horizontal"
7./>

其中这个@drawable/dock_bg就是.9(九宫格图片),每次添加Icon的时候都会水平自由拉伸。 

还有一个注意的地方就是我们下面Dock区域是能让任何快捷方式和Widget放到这个区域的,所以我们要设置一个禁区,不过系统已经为我们写好了,我们只需要设置一下就可以了,在同样在layout-land目录下的workspace_screen.xml文件里,内容如下:
01.<com.android.launcher.CellLayout
02.xmlns:android="http://schemas.android.com/apk/res/android"
03.xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
04. 
05.android:layout_width="fill_parent"
06.android:layout_height="fill_parent"
07. 
08.launcher:cellWidth="106dip"
09.launcher:cellHeight="73dip"
10.launcher:longAxisStartPadding="0dip"
11.launcher:longAxisEndPadding="55dip"
12.launcher:shortAxisStartPadding="0dip"
13.launcher:shortAxisEndPadding="70dip"
14.launcher:shortAxisCells="6"
15.launcher:longAxisCells="8" />
,我们只需要该launcher:shortAxisEndPadding="70dip"这个就可以,这个数值根据你的需求来即可了。 

最后来一张我们真机上的截图: 


如果还有什么不懂的,大家可以踊跃讨论。

原创粉丝点击