新浪博客

Android View 拖动效果

2012-08-22 11:13阅读:

android-robot

用心热爱技术,用技术改变生活

关注
Android拖动实现很简单,主要就是实现OnTouchListener事件监听,获取到触碰点之后,重新设置View的位置即可。
下面是这个例子的描述:
首先点击create按钮,将会产生一个新的ImageView对象,然后移动新的ImageView到任何你想要去的地方。如下图:
Android <wbr>View <wbr>拖动效果
Android <wbr>View <wbr>拖动效果
Android <wbr>View <wbr>拖动效果

下面是源码:
创建一个新的View,并添加到Window中:
package com.mandr.test;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class CreatView {
public static final String Tag = 'CreatView';

private Context mContext;
protected static CreatView mCreatView;
private int[] location;
private ImageView mImageView;
public WindowManager mWm;
public WindowManager.LayoutParams layoutParams;

public CreatView(Context context) {
this.mContext = context;
mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams();
}

public synchronized static CreatView getCreatView(Context context) {
Log.d(Tag, 'getCreatView');
if (mCreatView == null) {
mCreatView = new CreatView(context);
}
return mCreatView;
}

public void setLocation(int[] location) {
this.location = location;
}

private View createNewView() {
Log.d(Tag, 'createNewView');
ImageView mImageView = new ImageView(mContext);
mImageView.setImageDrawable(mContext.getResources().getDrawable(
R.drawable.apple));
mImageView.setLayoutParams(new LinearLayout.LayoutParams(40, 40));
return mImageView;
}

public void addViewToScreen() {
Log.d(Tag, 'addViewToScreen');
mImageView = (ImageView) createNewView();
mImageView.setOnTouchListener(new TouchListener());

layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
layoutParams.x = location[0] + 30; //偏移量x
layoutParams.y = location[1]; //偏移量y
layoutParams.width = 40;
layoutParams.height = 40;
layoutParams.alpha = 1.0f;
mWm.addView(mImageView, layoutParams);
}

class TouchListener implements OnTouchListener {
int lastX;
int lastY;
int screenWidth;
int screenHeight;

public TouchListener() {
DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
Log.d(Tag, 'screen width =' + screenWidth + ',screen height='
+ screenHeight);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(Tag, 'TouchListener -- onTouch');
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
Log.d(Tag, 'down x=' + lastX + ', y=' + lastY);
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
Log.d(Tag, 'move dx=' + dx + ', dy=' + dy);
int left = v.getLeft() + dx;
int top = v.getTop() + dy;
int right = v.getRight() + dx;
int bottom = v.getBottom() + dy;
Log.d(Tag, 'view left=' + left + ', top=' + top + ', right='
+ right + ',bottom=' + bottom);
// set bound
if (left < 0) {
left = 0;
right = left + v.getWidth();
}
if (right > screenWidth) {
right = screenWidth;
left = right - v.getWidth();
}
if (top < 0) {
top = 0;
bottom = top + v.getHeight();
}
if (bottom > screenHeight) {
bottom = screenHeight;
top = bottom - v.getHeight();
}

v.layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
layoutParams.x = lastX;
layoutParams.y = lastY-30;
mWm.updateViewLayout(v, layoutParams);

lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}
}

测试的Activity:
package com.mandr.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
public static final String Tag = 'MainActivity';

private Button creatorBtn;
private CreatView mCreatView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCreatView = CreatView.getCreatView(this);

creatorBtn = (Button)this.findViewById(R.id.button1);
creatorBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Log.d(Tag, 'creatorBtn click');
int[] location = new int[2];
v.getLocationInWindow(location);
Log.d(Tag, 'x = '+location[0]+',y = '+location[1]);
mCreatView.setLocation(location);
mCreatView.addViewToScreen();
}
});
}
}


触控事件的处理,例如照片的放大和缩小,桌面ICON的删除等等,应用也是很广泛的,希望我能和大家一起学习,成长。

我的更多文章

下载客户端阅读体验更佳

APP专享