MiniLauncher代码

来源:互联网 发布:上海网站排名优化 编辑:程序博客网 时间:2024/05/29 19:05
package com.android.launcherEx;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.Toast;


public class MiniLauncher extends ViewGroup implements
View.OnLongClickListener, DropTarget, DragController.DragListener,
DragSource {


private static final int HORIZONTAL = 1;
private static final int VERTICAL = 0;
private Launcher mLauncher;
// private View mDeleteView;
private int mOrientation = HORIZONTAL;
private int mNumCells = 4;
private int mCellWidth = 20;
private int mCellHeight = 20;
private TransitionDrawable mBackground;


private Scroller mScroller;
private float mLastMotionX;
private float mLastMotionY;
private final static int TOUCH_STATE_REST = 0;
private final static int TOUCH_STATE_SCROLLING = 1;
private static final int TOUCH_STATE_DOWN = 2;
private int mTouchState = TOUCH_STATE_REST;
private int mTouchSlop;
private final int mScrollingSpeed = 600;
private boolean mScrollAllowed = false;
private DragController mDragger;
// private final FlingGesture mFlingGesture = new FlingGesture();
private int mCurrentIndex = 0;


private DragController mDragController;
private Workspace mWorkspace;
private View mDragView;
private LinearLayout mItemHolder;
private View mScrollView;
private View mSelectedView;


public MiniLauncher(Context arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}


public MiniLauncher(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}


public MiniLauncher(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);


setHapticFeedbackEnabled(true);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MiniLauncher, defStyle, 0);
mOrientation = a.getInt(R.styleable.MiniLauncher_orientation,
mOrientation);
mNumCells = 4;
// mNumCells=a.getInt(R.styleable.MiniLauncher_cells, mNumCells);
mCellWidth = a.getDimensionPixelSize(
R.styleable.MiniLauncher_cellWidth, mCellWidth);
mCellHeight = a.getDimensionPixelSize(
R.styleable.MiniLauncher_cellHeight, mCellHeight);
mScroller = new Scroller(getContext());
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
// mFlingGesture.setListener(this);
}


@Override
public void setDragController(DragController dragger) {
// TODO Auto-generated method stub


}


@Override
public void onDropCompleted(View target, boolean success) {
// TODO Auto-generated method stub


}


@Override
public void onDragStart(DragSource source, Object info, int dragAction) {
// TODO Auto-generated method stub
Log.i("hmg", "DockBar->onDragStart");
}


@Override
public void onDragEnd() {
// TODO Auto-generated method stub


}


@Override
public void onDrop(DragSource source, int x, int y, int xOffset,
int yOffset, DragView dragView, Object dragInfo) {
// TODO Auto-generated method stub
boolean accept = true;
ItemInfo info = (ItemInfo) dragInfo;
switch (info.itemType) {
case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
// we do accept those
break;
case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Toast t = Toast.makeText(getContext(), "Not Supported",
Toast.LENGTH_SHORT);
t.show();
accept = false;
return;
default:
Toast t2 = Toast.makeText(getContext(), "Unknown Item",
Toast.LENGTH_SHORT);
t2.show();
accept = false;
return;
}
if (accept) {
info.cellX = FindItemDropPos(x, y);
addItemInDockBar(info);
}
}


@Override
public void onDragEnter(DragSource source, int x, int y, int xOffset,
int yOffset, DragView dragView, Object dragInfo) {
// TODO Auto-generated method stub
Log.i("hmg", "DockBar->onDragEnter");
setBackgroundResource(R.drawable.dock_bg_press);
}


@Override
public void onDragOver(DragSource source, int x, int y, int xOffset,
int yOffset, DragView dragView, Object dragInfo) {
// TODO Auto-generated method stub


}


@Override
public void onDragExit(DragSource source, int x, int y, int xOffset,
int yOffset, DragView dragView, Object dragInfo) {
// TODO Auto-generated method stub
setBackgroundResource(R.drawable.dock_bg);
// mItemHolder.removeView(mDragView);
removeView(mDragView);
Log.i("hmg", "DockBar->onDragExit");
}


@Override
public boolean acceptDrop(DragSource source, int x, int y, int xOffset,
int yOffset, DragView dragView, Object dragInfo) {
// TODO Auto-generated method stub
Log.i("hmg", "DockBar->acceptDrop");
final ItemInfo item = (ItemInfo) dragInfo;
if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET
|| item.itemType == LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER
|| item.itemType == LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER
|| item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME
|| item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH
|| item.itemType == LauncherSettings.Favorites.ITEM_TYPE_WIDGET_CLOCK) {
return false;
}
return true;
}


@Override
public Rect estimateDropLocation(DragSource source, int x, int y,
int xOffset, int yOffset, DragView dragView, Object dragInfo,
Rect recycle) {
// TODO Auto-generated method stub
return null;
}


@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Log.e("+++++++++++++++++++++++", "MiniLauncher - on LongClick");
if (mLauncher.isAllAppsVisible())
mLauncher.closeAllApps(false);
mSelectedView = v;
mDragController.startDrag(v, this, v.getTag(),
DragController.DRAG_ACTION_MOVE);
// removeSelectedItem();
removeView(v);
return true;


}


//
// private void removeSelectedItem() {
// if (mSelectedView == null)
// return;
// mItemHolder.removeView(mSelectedView);
//
// }


private int FindItemDropPos(int x, int y) {
final boolean horizontal = (mOrientation == HORIZONTAL);
final int count = getChildCount();
int margin = horizontal ? ((getMeasuredWidth()) / 2)
- (((count * mCellWidth) / 2)) : ((getMeasuredHeight()) / 2)
- (((count * mCellHeight) / 2));
if (margin < 0)
margin = 0;
// check for drop on an item
int dropPos = horizontal ? (x + getScrollX()) : (y + getScrollY());


for (int i = 0; i < count; i++) {
View child = getChildAt(i);
ItemInfo item = (ItemInfo) child.getTag();
if (child.getVisibility() != GONE) {


int bound1 = margin
+ (item.cellX * (horizontal ? mCellWidth : mCellHeight));
int bound2 = bound1 + (horizontal ? mCellWidth : mCellHeight);


if (dropPos < bound1 && item.cellX == 0) // before the first
// item
return 0;
if (dropPos >= bound2 && item.cellX == (count - 1))
return count;


if (dropPos >= bound1 && dropPos < bound2) {
int pos = item.cellX;
for (int j = 0; j < item.cellX; j++) {
View chld = getChildAt(j);
if (chld.getVisibility() == GONE)
pos++;
}


int middle = bound1
+ (((mOrientation == HORIZONTAL) ? mCellWidth
: mCellHeight) / 2);
if (dropPos >= middle)
return pos + 1;
return pos;
}


}
}
// something went wrong...
return getChildCount();
}


public void addItemInDockBar(ItemInfo itemInfo) {
// View view = null;
// switch (info.itemType) {
// case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
// case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
// if (info.container == NO_ID) {
// // Came from all apps -- make a copy
// info = new ApplicationInfo((ApplicationInfo) info);
// }
// view = mLauncher.createSmallShortcut(R.layout.small_application,
// this, (ApplicationInfo) info);
// break;
// case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
// view = mLauncher.createSmallLiveFolder(R.layout.small_application,
// this, (LiveFolderInfo) info);
// break;
// case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
// view = mLauncher.createSmallFolder(R.layout.small_application,
// this, (UserFolderInfo) info);
// break;
// case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
// // Toast t=Toast.makeText(getContext(),
// // "Widgets not supported... sorry :-)", Toast.LENGTH_SHORT);
// // t.show();
// return;
// default:
// // Toast t2=Toast.makeText(getContext(),
// // "Unknown item. We can't add unknown item types :-)",
// // Toast.LENGTH_SHORT);
// // t2.show();
// return;
// // throw new IllegalStateException("Unknown item type: " +
// // info.itemType);
// }
// view.setLongClickable(true);
// view.setOnLongClickListener(this);
// // view.setBackgroundDrawable(IconHighlights.getDrawable(mLauncher,
// // IconHighlights.TYPE_DOCKBAR));
// addView(view);
// invalidate();


View view = null;
switch (itemInfo.itemType) {
case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
ShortcutInfo shortcutInfo;
// ��קͼ��������app list
if (itemInfo.container == NO_ID
&& itemInfo instanceof ApplicationInfo) {
// �����������ͼ�����Ϣ��һ����忴Դ��
shortcutInfo = new ShortcutInfo((ApplicationInfo) itemInfo);
} else
shortcutInfo = (ShortcutInfo) itemInfo; // ��קͼ����������
// ����Launcher�е�createSmallShortcut���һ��imageView
view = mLauncher.CreateDockShortcut(shortcutInfo);
view.setOnLongClickListener(this);
break;
case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
break;
default:
throw new IllegalStateException("Unknown item type: "
+ itemInfo.itemType);
}
view.setLongClickable(true);
view.setOnLongClickListener(this);
addView(view);
invalidate();
}


void setLauncher(Launcher launcher) {
mLauncher = launcher;
}


public void setDragger(DragController dragger) {
mDragger = dragger;
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int count = getChildCount();
Log.e("++++++++++++++++++", "MiniLauncher - onLauout");
Log.e("++++++++++++++++++", "getChildCount: " + count);
int marginLeft = ((getMeasuredWidth()) / 2)
- (((count * mCellWidth) / 2));
int marginTop = ((getMeasuredHeight()) / 2)
- (((count * mCellHeight) / 2));
if (getChildCount() > mNumCells) {
if (mOrientation == HORIZONTAL) {
marginLeft = 0;
mCellWidth = (getMeasuredWidth() / mNumCells);
} else {
marginTop = 0;
mCellHeight = (getMeasuredHeight() / mNumCells);
}
mScrollAllowed = true;
snapScroll();
} else {
mScrollAllowed = false;
scrollTo(0, 0);
}
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
ItemInfo item = (ItemInfo) child.getTag();
if (child.getVisibility() != GONE) {
int childLeft = (mOrientation == HORIZONTAL) ? marginLeft
+ (item.cellX * mCellWidth) : 0;
int childTop = (mOrientation == VERTICAL) ? marginTop
+ (item.cellX * mCellHeight) : 0;
int childRight = childLeft + mCellWidth;
int childBottom = childTop + mCellHeight;
child.layout(childLeft, childTop, childRight, childBottom);
Log.e("++++++++++++++++++", "  " + childLeft + childTop
+ childRight + childBottom);
}
}


}


private void snapScroll() {
final int actualScroll = (mOrientation == HORIZONTAL) ? getScrollX()
: getScrollY();
final int cellSize = (mOrientation == HORIZONTAL) ? mCellWidth
: mCellHeight;
final int actualLimit = (mOrientation == HORIZONTAL) ? getWidth()
: getHeight();
int position = actualScroll - (actualScroll % cellSize);
if (position < 0)
position = 0;
if (position > ((getChildCount()) * cellSize) - actualLimit)
position = (getChildCount() * cellSize) - actualLimit;
mCurrentIndex = position / cellSize;
final int delta = position - actualScroll;
if (mOrientation == HORIZONTAL) {
mScroller.startScroll(actualScroll, 0, delta, 0, mScrollingSpeed);
} else {
mScroller.startScroll(0, actualScroll, 0, delta, mScrollingSpeed);
}
invalidate();
}


@Override
public void setBackgroundDrawable(Drawable d) {
// TODO Auto-generated method stub
// if (mBackground != null)
// mBackground.setCallback(null);
super.setBackgroundDrawable(d);
// mBackground = (TransitionDrawable) d;
// mBackground.setCrossFadeEnabled(true);


}


}



Launcher中添加

mMiniLauncher = (MiniLauncher) findViewById(R.id.mini_content);

mMiniLauncher.setLauncher(this);
// mMiniLauncher.setDragController(dragController);
mMiniLauncher.setDragger(dragController);
mMiniLauncher.setOnLongClickListener(this);
dragController.setDragListener(mMiniLauncher);
dragController.setDockDragListener(mMiniLauncher);
dragController.addDropTarget(mMiniLauncher);



Layout

<com.android.launcherEx.DockBar
android:id="@+id/dockbar" android:layout_width="fill_parent"
android:layout_height="@dimen/button_bar_height" android:orientation="vertical"
android:layout_gravity="bottom|center_horizontal">
<com.android.launcherEx.MiniLauncher
android:background="@drawable/dock_bg" android:id="@+id/mini_content"
android:layout_width="fill_parent" android:layout_height="fill_parent"
launcher:cellWidth="50dip" launcher:cellHeight="55dip"
launcher:cells="6" launcher:orientation="horizontal" android:padding="0dip" />
</com.android.launcherEx.DockBar>

原创粉丝点击