package com.drag.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
public class DragTestActivity extends Activity {
private final static String TAG="MyDebug";
private final static int COLUMN=2;
private final static int ROW=3;
private int containerWidth;
private int containerHeight;
private int row_width;
private int column_height;
private int unit_width;
private int unit_height;
private RelativeLayout drag_layout;
private Map<Position,View> dragViewsMap;
private List<Position> positions;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DisplayMetrics dm = this.getResources().getDisplayMetrics();
this.containerWidth = dm.widthPixels;
this.containerHeight = dm.heightPixels-70;
this.row_width=this.containerWidth/COLUMN;
this.column_height=this.containerHeight/ROW;
Log.i(TAG, "this.containerWidth = "+this.containerWidth);
Log.i(TAG, "this.containerHeight = "+this.containerHeight);
Log.i(TAG, "this.row_width = "+this.row_width);
Log.i(TAG, "this.column_height = "+this.column_height);
this.drag_layout = (RelativeLayout) this.findViewById(R.id.relativeLayout_scenes);
initDragViews();
locateViews();
}
private void locateViews()
{
for(Position pos : this.positions)
{
this.locateView(null, pos);
}
}
private void initDragViews() {
this.positions = new ArrayList<Position>();
List<Bitmap> bmps = new ArrayList<Bitmap>();
bmps.add(((BitmapDrawable) (this.getResources().getDrawable(R.drawable.scene_bedroom))).getBitmap());
bmps.add(((BitmapDrawable) (this.getResources().getDrawable(R.drawable.scene_bathroom))).getBitmap());
bmps.add(((BitmapDrawable) (this.getResources().getDrawable(R.drawable.scene_diningroom))).getBitmap());
bmps.add(((BitmapDrawable) (this.getResources().getDrawable(R.drawable.scene_guestroom))).getBitmap());
bmps.add(((BitmapDrawable) (this.getResources().getDrawable(R.drawable.scene_kidsroom))).getBitmap());
bmps.add(((BitmapDrawable) (this.getResources().getDrawable(R.drawable.scene_kitchenroom))).getBitmap());
this.unit_height = bmps.get(0).getHeight() + 90;
this.unit_width = bmps.get(0).getWidth();
Log.d(TAG, "unit_height = " + unit_height);
Log.d(TAG, "unit_width = " + unit_width);
this.dragViewsMap = new HashMap<Position, View>();
for (int i = 0; i < bmps.size(); i++) {
Bitmap bmp = bmps.get(i);
int row = i / COLUMN;
int clmn = i % COLUMN;
Position pos = new Position(row, clmn);
final RelativeLayout unit_layout = (RelativeLayout) this.getLayoutInflater().inflate(R.layout.scene_unit_layout,null);
ImageView drag_img = (ImageView) unit_layout.findViewById(R.id.imgview_drag_img);
drag_img.setImageBitmap(bmp);
TextView drag_txt = (TextView) unit_layout.findViewById(R.id.txtview_drag_text);
drag_txt.setText("drag test " + i);
unit_layout.setOnTouchListener(new OnTouchListener() {
int lastX, lastY;
Position originPos=Position.getEmptyPosition();
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
Log.i("TAG", "Touch:" + action);
// Toast.makeText(DraftTest.this, "位置:"+x+","+y,
// Toast.LENGTH_SHORT).show();
if (action == MotionEvent.ACTION_DOWN)
{
Log.v("Drag", "MotionEvent.ACTION_DOWN");
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
originPos=getTouchPos(lastX, lastY);
originPos=mappingPosition(originPos);
Log.v("Drag", "lastX = " + lastX + " lastY = " + lastY);
// v.layout(lastX, lastX/2, lastY, lastY/2);
}
/**
* layout(l,t,r,b) l Left position, relative to parent t Top
* position, relative to parent r Right position, relative
* to parent b Bottom position, relative to parent
* */
else if(action== MotionEvent.ACTION_MOVE)
{
Log.v("Drag", "MotionEvent.ACTION_MOVE");
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
Log.v("Drag", "dx = " + dx + " dy = " + dy);
int left = v.getLeft() + dx;
int top = v.getTop() + dy;
int right = v.getRight() + dx;
int bottom = v.getBottom() + dy;
if (left < 0) {
left = 0;
right = left + v.getWidth();
}
if (right > containerWidth) {
right = containerWidth;
left = right - v.getWidth();
}
if (top < 0) {
top = 0;
bottom = top + v.getHeight();
}
if (bottom > containerHeight) {
bottom = containerHeight;
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom);
Log.v("Drag", "position:" + left + ", " + top + ", "
+ right + ", " + bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
}
else if(action== MotionEvent.ACTION_UP)
{
Position pos = getTouchPos(lastX, lastY);
move(unit_layout, originPos, pos);
}
return true;
}
});
this.positions.add(pos);
Log.i(TAG, "put "+unit_layout.toString()+" in the dragViewsMap with the position : "+pos.toString());
this.dragViewsMap.put(pos, unit_layout);
}
}
/**根据点击位置获取点击的控件坐标*/
private Position getTouchPos(int tx,int ty)
{
int pos_row=ty/this.column_height;
int pos_clmn=tx/this.row_width;
if(pos_row >= ROW-1)
{
pos_row=ROW-1;
}
if(pos_clmn>=COLUMN-1)
{
pos_clmn=COLUMN-1;
}
return new Position(pos_row, pos_clmn);
}
/**
* 控件换位
* */
private void move(View view,Position fromPos,Position toPos)
{
Log.v(TAG, "move().....topos : "+toPos.toString());
toPos=this.mappingPosition(toPos);
//判断换位顺序:是往上换位还是往下换位
int from_index=-1;
int to_index=-1;
for(int i=0;i<this.positions.size();i++)
{
Position pos = this.positions.get(i);
if(pos.getRow()==fromPos.getRow()&&pos.getClmn()==fromPos.getClmn())
{
from_index=i;
}
if(pos.getRow()==toPos.getRow()&&pos.getClmn()==toPos.getClmn())
{
to_index=i;
}
}
if(to_index<from_index) //往上交换
{
for(int i=from_index-1;i>=to_index;i--)
{
View moving_view = this.dragViewsMap.get(this.positions.get(i));
this.locateView(moving_view, this.positions.get(i+1));
this.dragViewsMap.put(this.positions.get(i+1), moving_view);
}
//将拖动的控件放到目标坐标位置
this.locateView(view, toPos);
this.dragViewsMap.put(toPos, view);
}
else //往下交换
{
for(int i=from_index+1;i<=to_index;i++)
{
View moving_view = this.dragViewsMap.get(this.positions.get(i));
this.locateView(moving_view, this.positions.get(i-1));
this.dragViewsMap.put(this.positions.get(i-1), moving_view);
}
//将拖动的控件放到目标坐标位置
this.locateView(view, toPos);
this.dragViewsMap.put(toPos, view);
}
}
private Position mappingPosition(Position position)
{
for(Position pos : this.positions)
{
if(pos.getRow()==position.getRow()&&pos.getClmn()==position.getClmn())
{
return pos;
}
}
return null;
}
/**定位指定的控件*/
private void locateView(View view,Position pos)
{
Log.v(TAG, "locatView()..."+pos.toString());
boolean isInit=false;
RelativeLayout.LayoutParams la
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
DragTest.zip (40个子文件)
DragTest
bin
classes.dex 11KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
scene_bedroom.png 7KB
scene_diningroom.png 11KB
scene_kitchenroom.png 6KB
scene_bathroom.png 10KB
scene_guestroom.png 9KB
scene_kidsroom.png 12KB
drawable-mdpi
ic_launcher.png 2KB
DragTest.apk 71KB
classes
com
drag
activity
R$layout.class 432B
R$id.class 526B
R$drawable.class 637B
DragTestActivity$1.class 3KB
DragTestActivity.class 8KB
R$attr.class 337B
R.class 524B
R$string.class 424B
Position.class 1KB
resources.ap_ 63KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
scene_bedroom.png 10KB
scene_diningroom.png 14KB
scene_kitchenroom.png 9KB
scene_bathroom.png 12KB
scene_guestroom.png 11KB
scene_kidsroom.png 15KB
values
strings.xml 182B
drawable-mdpi
ic_launcher.png 3KB
layout
scene_unit_layout.xml 1KB
main.xml 465B
assets
gen
com
drag
activity
R.java 1KB
proguard.cfg 1KB
.settings
org.eclipse.jdt.core.prefs 208B
src
com
drag
activity
DragTestActivity.java 8KB
Position.java 795B
.project 844B
.classpath 364B
project.properties 377B
AndroidManifest.xml 739B
共 40 条
- 1
夏天的小虫
- 粉丝: 3
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页