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

android 1.6 launcher研究之Drag&Drop模型

2011-09-07 22:34 239 查看
参考:http://blog.csdn.net/stonecao/archive/2011/06/02/6462357.aspx

http://blog.csdn.net/hmg25/archive/2011/03/02/6217659.aspx

研究了launcher的拖拽模型 里面重要的类 如下:

DragLayer:

public class DragLayer extends FrameLayout implements DragController { }

DragController:

/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * *
Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language
governing permissions and * limitations under the License. */ package com.lp.launcher; import android.view.View; /** * Interface for initiating a drag within a view or across multiple views. * */ public interface DragController { /** *
Interface to receive notifications when a drag starts or stops */ interface DragListener { /** * A drag has begun * * @param v The view that is being dragged * @param source An object representing
where the drag originated * @param info The data associated with the object that is being dragged * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE} * or {@link DragController#DRAG_ACTION_COPY}
*/ void onDragStart(View v, DragSource source, Object info, int dragAction); /** * The drag has eneded */ void onDragEnd(); } /** * Indicates the drag is a move. */ public
static int DRAG_ACTION_MOVE = 0; /** * Indicates the drag is a copy. */ public static int DRAG_ACTION_COPY = 1; /** * Starts a drag * * @param v The view that is being dragged * @param source An object representing
where the drag originated * @param info The data associated with the object that is being dragged * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or * {@link #DRAG_ACTION_COPY} */ void startDrag(View v,
DragSource source, Object info, int dragAction); /** * Sets the drag listner which will be notified when a drag starts or ends. */ void setDragListener(DragListener l); /** * Remove a previously installed drag listener.
*/ void removeDragListener(DragListener l); }

DragSource:

/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * *
Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language
governing permissions and * limitations under the License. */ package com.lp.launcher; import android.view.View; /** * Interface defining an object that can originate a drag. * */ public interface DragSource { void setDragger(DragController dragger);
void onDropCompleted(View target, boolean success); }

DropTarget:

/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * *
Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language
governing permissions and * limitations under the License. */ package com.lp.launcher; import android.graphics.Rect; /** * Interface defining an object that can receive a drag. * */ public interface DropTarget { /** * Handle an object being
dropped on the DropTarget * * @param source DragSource where the drag started * @param x X coordinate of the drop location * @param y Y coordinate of the drop location * @param xOffset Horizontal offset with the object being dragged
where the original touch happened * @param yOffset Vertical offset with the object being dragged where the original touch happened * @param dragInfo Data associated with the object being dragged * */ void onDrop(DragSource source,
int x, int y, int xOffset, int yOffset, Object dragInfo); void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo); void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo);
void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo); /** * Check if a drop action can occur at, or near, the requested location. * This may be called repeatedly during a drag, so any calls should return
* quickly. * * @param source DragSource where the drag started * @param x X coordinate of the drop location * @param y Y coordinate of the drop location * @param xOffset Horizontal offset with the object being dragged where the
* original touch happened * @param yOffset Vertical offset with the object being dragged where the * original touch happened * @param dragInfo Data associated with the object being dragged * @return True if the
drop will be accepted, false otherwise. */ boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo); /** * Estimate the surface area where this object would land if dropped at the * given location.
* * @param source DragSource where the drag started * @param x X coordinate of the drop location * @param y Y coordinate of the drop location * @param xOffset Horizontal offset with the object being dragged where the *
original touch happened * @param yOffset Vertical offset with the object being dragged where the * original touch happened * @param dragInfo Data associated with the object being dragged * @param recycle {@link Rect} object
to be possibly recycled. * @return Estimated area that would be occupied if object was dropped at * the given location. Should return null if no estimate is found, * or if this target doesn't provide estimations. */ Rect
estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo, Rect recycle); }

这几个类 是必须的类

大致的处理流程 我以把应用拖到桌面上为例 :



2011-8-15 09:16:30 上传
下载附件
(0 Bytes)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: