package com.cn.theodore.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
public class DragGrid extends GridView {
private int dragPosition;
private int dropPosition;
private int holdPosition;
private int startPosition;
private int specialPosition = -1;
private int leftBottomPosition = -1;
private int nColumns = 3;
private int nRows;
private int Remainder;
private int itemTotalCount;
private int halfItemWidth;
private ImageView dragImageView = null;
private ViewGroup dragItemView = null;
private WindowManager windowManager = null;
private WindowManager.LayoutParams windowParams = null;
private int mLastX,xtox;
private int mLastY,ytoy;
private int specialItemY;
private int leftBtmItemY;
private String LastAnimationID;
private boolean isCountXY = false;
private boolean isMoving = false;
//private ArrayList<ViewGroup> mItemViewList ;
public DragGrid(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DragGrid(Context context) {
super(context);
}
boolean flag = false;
public void setLongFlag(boolean temp) {
flag = temp;
}
public boolean setOnItemLongClickListener(final MotionEvent ev) {
this.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int x = (int) ev.getX();
int y = (int) ev.getY();
mLastX=x;
mLastY=y;
startPosition = dragPosition = dropPosition = arg2;
if (dragPosition == AdapterView.INVALID_POSITION) {
}
ViewGroup itemView = (ViewGroup) getChildAt(dragPosition
- getFirstVisiblePosition());
if(!isCountXY){
halfItemWidth = itemView.getWidth()/2;
int rows;
itemTotalCount = getCount();
rows = itemTotalCount/nColumns;
Remainder = itemTotalCount%nColumns;
nRows = Remainder == 0 ? rows : rows + 1;
specialPosition = itemTotalCount - 1 - Remainder;
if(Remainder!=1)
leftBottomPosition = nColumns*(nRows-1);
if(Remainder == 0 || nRows == 1)
specialPosition = -1;
isCountXY = true;
}
if(specialPosition != dragPosition && dragPosition != -1){
specialItemY = getChildAt(specialPosition).getTop();
}else{
specialItemY = -1;
}
if(leftBottomPosition != dragPosition && dragPosition != -1){
leftBtmItemY = getChildAt(leftBottomPosition).getTop();
}else{
leftBtmItemY = -1;
}
dragItemView = itemView;
itemView.destroyDrawingCache();
itemView.setDrawingCacheEnabled(true);
itemView.setDrawingCacheBackgroundColor(0x000000);
Bitmap bm = Bitmap.createBitmap(itemView.getDrawingCache(true));
Bitmap bitmap = Bitmap.createBitmap(bm, 8, 8, bm.getWidth()-8, bm.getHeight()-8);
startDrag(bitmap, x, y);
hideDropItem();
itemView.setVisibility(View.INVISIBLE);
isMoving = false;
return false;
};
});
return super.onInterceptTouchEvent(ev);
}
public void GetItemShadow(int x,int y){
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
return setOnItemLongClickListener(ev);
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (dragImageView != null
&& dragPosition != AdapterView.INVALID_POSITION) {
int x = (int) ev.getX();
int y = (int) ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_MOVE:
if(!isCountXY) {
xtox = x-mLastX;
ytoy = y-mLastY;
isCountXY= true;
}
onDrag(x, y);
if(!isMoving )
OnMove(x,y);
break;
case MotionEvent.ACTION_UP:
stopDrag();
onDrop(x, y);
break;
}
}
return super.onTouchEvent(ev);
}
private void startDrag(Bitmap bm, int x, int y) {
stopDrag();
windowParams = new WindowManager.LayoutParams();
windowParams.gravity = Gravity.TOP | Gravity.LEFT;
windowParams.x = dragItemView.getLeft() + 8;
windowParams.y = dragItemView.getTop()+(int)(45*Configure.screenDensity);
windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.alpha = 0.8f;
ImageView iv = new ImageView(getContext());
iv.setImageBitmap(bm);
windowManager = (WindowManager) getContext().getSystemService(
Context.WINDOW_SERVICE);
windowManager.addView(iv, windowParams);
dragImageView = iv;
}
public void OnMove(int x, int y){
int TempPosition = pointToPosition(x,y);
int sOffsetY = specialItemY == -1 ? y - mLastY : y - specialItemY - halfItemWidth;
int lOffsetY = leftBtmItemY == -1 ? y - mLastY : y - leftBtmItemY - halfItemWidth;
if(TempPosition != AdapterView.INVALID_POSITION && TempPosition != dragPosition){
dropPosition = TempPosition;
}else if(specialPosition != -1 && dragPosition == specialPosition && sOffsetY >= halfItemWidth){
dropPosition = (itemTotalCount - 1);
}else if(leftBottomPosition != -1 && dragPosition == leftBottomPosition && lOffsetY >= halfItemWidth){
dropPosition = (itemTotalCount - 1);
}
if(dragPosition != startPosition)
dragPosition = startPosition;
int MoveNum = dropPosition - dragPosition;
if(dragPosition != startPosition && dragPosition == dropPosition)
MoveNum = 0;
if(MoveNum != 0){
int itemMoveNum = Math.abs(MoveNum);
float Xoffset,Yoffset;
for (int i = 0;i < itemMoveNum;i++){
if(MoveNum > 0){
holdPosition = dragPosition + 1;
Xoffset = (dragPosition/nColumns == holdPosition/nColumns) ? (-1) : (nColumns -1);
Yoffset = (dragPosition/nColumns == holdPosition/nColumns) ? 0 : (-1);
}else{
holdPosition = dragPosition - 1;
Xoffset = (dragPosition/nColumns == holdPosition/nColumns) ? 1 : (-(nColumns-1));
Yoffset = (dragPosition/nColumns == holdPosition/nColumns) ? 0 : 1;
}
ViewGroup moveView = (ViewGroup)getChildAt(holdPosition);
Animation animation = getMoveAnimation(Xoffset,Yoffset);
moveView.startAnimation(animation);
dragPosition = holdPosition;
if(dragPosition == dropPosition)
LastAnimationID = animation.toString();
final DateAdapter adapter = (DateAdapter)this.getAdapter();
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
isMoving = true;
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
String animaionID = animation.toString();
if(animaionID.equalsIgnoreCase(LastAnimationID)){
adapter.exchange(startPosition, dropPosition);
startPosition = dropPosition;
isMoving = false;
}
}
});
}
}
}
private void onDrop(int x,int y){
final DateAdapter adapter = (DateAdapter) this.getAdapter();
adapter.showDropItem(true);
adapter.notifyDataSetChanged();
}
private void onDrag(int x, int y) {
if (dragImageView != null) {
windowParams.alpha = 0.8f;
windowParams.x = (x-mLastX-xtox)+dragItemView.getLeft(
没有合适的资源?快使用搜索试试~ 我知道了~
GridVIew拖动item,其它item自动移位效果
共41个文件
class:12个
png:12个
java:5个
4星 · 超过85%的资源 需积分: 50 213 下载量 8 浏览量
2012-06-01
16:35:34
上传
评论 5
收藏 2.54MB RAR 举报
温馨提示
实现了类似小米launcher中,长按item,移动到其它item时,其它item自动移位的效果
资源推荐
资源详情
资源评论
收起资源包目录
AniGridView(2).rar (41个子文件)
bin
classes.dex 15KB
res
drawable-ldpi
ic_launcher.png 2KB
notesbackground.png 315KB
drawable-hdpi
ic_launcher.png 4KB
notesbackground.png 315KB
drawable-mdpi
ic_launcher.png 2KB
notesbackground.png 315KB
AniGridView.apk 966KB
classes
com
cn
theodore
R$layout.class 413B
R$id.class 484B
R$drawable.class 437B
R$attr.class 331B
R.class 510B
AniGridViewActivity.class 1KB
R$string.class 418B
util
Configure.class 1KB
DragGrid.class 9KB
DragGrid$1.class 4KB
DragGrid$2.class 2KB
DateAdapter.class 3KB
resources.ap_ 956KB
res
drawable-ldpi
ic_launcher.png 2KB
notesbackground.png 318KB
drawable-hdpi
ic_launcher.png 4KB
notesbackground.png 318KB
values
strings.xml 188B
drawable-mdpi
ic_launcher.png 3KB
notesbackground.png 318KB
layout
item.xml 937B
main.xml 758B
assets
gen
com
cn
theodore
R.java 1KB
proguard.cfg 1KB
.settings
org.eclipse.core.resources.prefs 116B
src
com
cn
theodore
AniGridViewActivity.java 815B
util
DragGrid.java 9KB
Configure.java 604B
DateAdapter.java 2KB
.project 847B
.classpath 364B
project.properties 361B
AndroidManifest.xml 740B
共 41 条
- 1
hxdanya
- 粉丝: 36
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页