package com.example.draggridview;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
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;
import android.widget.Toast;
public class dragGridView extends GridView {
// mWindowManager.updateViewLayout(mDragImageView, mWindowLayoutParams); //更新镜像的位置
private static final String TAG = "dragGridView";
private View view;
private int point;
private int downX;
private int downY;
private WindowManager.LayoutParams mWindowLayoutParams;
private Bitmap mDragBitmap;
private ImageView mDragImageView;
private WindowManager mWindowManager;
// 按下的点到 imageview的顶部距离
private int offset2Top;
private int offset2Left;
private int rawX;
private int rawY;
private int height;
private int width;
private int StatusHeight;
private int imageHeight;
private int imageWidth;
private String title;
private Bitmap bitmap;
private ImageView image;
private int scaleSize = 10;
private int defaultSize = 20;
private android.view.WindowManager.LayoutParams WindowLayoutParams;
private boolean first=true;
public dragGridView(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
public dragGridView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
}
public dragGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mWindowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics Metrics = new DisplayMetrics();
mWindowManager.getDefaultDisplay().getMetrics(Metrics);
width = Metrics.widthPixels;
height = Metrics.heightPixels;
StatusHeight = getStatusHeight(context);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = (int) ev.getX();
downY = (int) ev.getY();
rawX = (int) ev.getRawX();
rawY = (int) ev.getRawY();
point = pointToPosition(downX, downY);
if (point == AdapterView.INVALID_POSITION) {
break;
}
view = getChildAt(point - getFirstVisiblePosition());
offset2Left = downX - view.getLeft();
offset2Top = downY - view.getTop();
imageHeight = view.getLayoutParams().height;
imageWidth = view.getLayoutParams().width;
Log.e(TAG, height + "屏幕宽高" + width + "rawx" + rawX + "rawY" + rawY
+ "getleft" + view.getLeft() + "gettop" + view.getTop()
+ "dowx" + downX + "downY" + downY);
view.setDrawingCacheEnabled(true);
// 获取mDragItemView在缓存中的Bitmap对象
mDragBitmap = Bitmap.createBitmap(view.getDrawingCache());
// 这一步很关键,释放绘图缓存,避免出现重复的镜像
view.destroyDrawingCache();
// 布局动画 效果不佳
// LayoutTransition transition = new LayoutTransition();
// transition.setAnimator(LayoutTransition.DISAPPEARING,
// ObjectAnimator.ofFloat(this, "translationX",50, 200));
// this.setLayoutTransition(transition);
// view.setVisibility(View.INVISIBLE);
if(first){
createImage();
view.setVisibility(View.INVISIBLE);
first=false;
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
// TODO Auto-generated method stub
return super.dispatchTouchEvent(ev);
}
private void createImage() {
// windowParams(0, 0);
//
// // Bitmap.createScaledBitmap(mDragBitmap, 200, 200, false);
// mDragImageView = new ImageView(getContext());
//
// mDragImageView.setImageBitmap(Bitmap.createScaledBitmap(mDragBitmap,
// mDragBitmap.getWidth() + defaultSize, mDragBitmap.getHeight()
// + defaultSize, false));
// mWindowManager.addView(mDragImageView, mWindowLayoutParams);
//效果不佳
View preView = getPreView();
ObjectAnimator//
.ofFloat(preView, "translationX", 0.0F, 100.0F)//
.setDuration(500)//
.start();
//下面这个就不行
// ObjectAnimator animotion=ObjectAnimator.ofInt(preView, "alpha", 0, 1);
//
// animotion.setDuration(2000);
// animotion.start();
// View preView = getPreView();
// preView.setVisibility(View.INVISIBLE);
// preView.setDrawingCacheEnabled(true);
// // 获取mDragItemView在缓存中的Bitmap对象
// bitmap = Bitmap.createBitmap(preView.getDrawingCache());
// // 这一步很关键,释放绘图缓存,避免出现重复的镜像
// preView.destroyDrawingCache();
// image = new ImageView(getContext());
// image.setImageBitmap(bitmap);
//
// mWindowManager.addView(image, mWindowLayoutParams);
}
/**会多次调用
* @param dex 距离旁边 view的偏移 0 0 是本view
* @param dey
*/
private void windowParams(int dex, int dey) {
// 旁边view的坐标变化
int defaultDetaX = dex;
int defaultDetaY = dey;
// 屏幕坐标点 - 在控件点击的坐标-所点击控件view的padding 也就是左边距
// 得到点击view里的
// 坐标到坐的距离
// 当前按下view在 windowmange 挂载的坐标
int mCurrentViewX = rawX - offset2Left - scaleSize;
int mcurrentViewY = rawY - offset2Top - StatusHeight - scaleSize;
mWindowLayoutParams = new WindowManager.LayoutParams();
mWindowLayoutParams.format = PixelFormat.TRANSLUCENT; // 图片之外的其他地方透明
mWindowLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
mWindowLayoutParams.x = mCurrentViewX + defaultDetaX;
mWindowLayoutParams.y = mcurrentViewY + defaultDetaX;
mWindowLayoutParams.alpha = 0.59f; // 透明度
mWindowLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
}
private View getPreView() {
View v = getChildAt(point - getFirstVisiblePosition() - 1);
return v;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
return super.onTouchEvent(ev);
}
/**
* 获取状态栏的高度
*
* @param context
* @return
*/
private static int getStatusHeight(Context context) {
int statusHeight = 0;
Rect localRect = new Rect();
((Activity) context).getWindow().getDecorView()
.getWindowVisibleDisplayFrame(localRect);
statusHeight = localRect.top;
if (0 == statusHeight) {
Class<?> localClass;
try {
localClass = Class.forName("com.android.internal.R$dimen");
Object localObject = localClass.newInstance();
int i5 = Integer.parseInt(localClass
.getField("status_bar_height").get(localObject)
.toString());
statusHeight = context.getResources().getDimensionPixelSize(i5);
} catch (Exception e) {
e.printStackTrace();
}
}
return statusHeight;
}
}
dragView一些知识 -未完成状态
最新推荐文章于 2024-09-13 08:17:08 发布
这是一个自定义的dragGridView控件,它实现了在GridView中长按元素时,元素变为可拖动的镜像。通过dispatchTouchEvent方法处理触摸事件,ACTION_DOWN时获取按下位置并创建拖动的Bitmap,ACTION_MOVE时移动镜像,ACTION_UP时释放。控件还包含了获取状态栏高度、窗口管理器布局参数设置等功能。
摘要由CSDN通过智能技术生成