我正在尝试使用 android 2.1 (sdk 2.1) 构建拖放功能。我在网上搜索,只能使用 android 4.0+ 找到。我有一个使用此代码动态创建和设置的 ImageView(包装在具有一些属性和属性的类中)。
public void setTileImage(Drawable tileImage) {
if (tileImage == null) {
Log.d("Tiles", "Null");
}
this.tileImage = new ImageView(this.context);
this.tileImage.setImageDrawable(tileImage);
this.tileImage.setAdjustViewBounds(true);
this.tileImage.setScaleType(ImageView.ScaleType.FIT_START);
this.createImageMargins();
}
public void createImageMargins() {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(this.imageSizeX, this.imageSizeY);
this.tileImage.setLayoutParams(layoutParams);
MarginLayoutParams marginParams = new MarginLayoutParams(this.tileImage.getLayoutParams());
marginParams.setMargins(this.initialPositionX, this.initialPositionY, this.initialPositionX + imageSizeX, this.initialPositionY + this.imageSizeY);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginParams);
this.tileImage.setLayoutParams(layoutParams2);
this.tileImage.requestLayout();
}
我将此 ImageView 添加到 RelativeLayout。
RelativeLayout layout = (RelativeLayout) findViewById(R.id.playscreen);
Tiles t1 = this.createTitle();
layout.addView(t1.getTileImage());
对于创建的每个 ImageView(有几个),我添加了 OnTouchListener(对所有这些都是唯一的)
private static TouchListener listener = new TouchListener();;
t1.createDragHandler(listener);
public void createDragHandler(TouchListener listener){
this.tileImage.setOnTouchListener(listener);
}
这是实现 OnTouchListener 方法的类。此类将 ImageView 的位置(更新边距 - 类之后的方法)更改为它接收到的事件的位置。
public class TouchListener implements OnTouchListener{
public boolean onTouch(View v, MotionEvent motionEvent) {
Tiles currentTile = Tiles.searchTileByView(Play.getAvailableTiles(), (ImageView)v);
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
return true;
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
Tiles t = Play.checkCollision(currentTile, currentTile.getPositionX(), currentTile.getPositionY());
if(t == null){
if(currentTile.getPositionX() == currentTile.getInitialPositionX() && currentTile.getPositionY() == currentTile.getInitialPositionY()){
Log.d("Log","This was a click");
}else{
currentTile.setPositionX(currentTile.getInitialPositionX());
currentTile.setPositionY(currentTile.getInitialPositionY());
currentTile.createImageMargins();
}
}else{
if(t.getId() == currentTile.getId()){
Play.updateScore(t.getPoints()*t.getSpecialPoints()+currentTile.getPoints()*currentTile.getSpecialPoints());
t.setActive(false);
currentTile.setActive(false);
t.getTileImage().setVisibility(View.INVISIBLE);
currentTile.getTileImage().setVisibility(View.INVISIBLE);
Log.d("Log","Tile Removed");
}
}
return true;
}else if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){
Log.d("Log:" + this.toString() + ":" + v,"Moved -> X: " + motionEvent.getX() + " Y: " + motionEvent.getY());
currentTile.setPositionX((int) motionEvent.getX());
currentTile.setPositionY((int) motionEvent.getY());
currentTile.updateImageMargins();
return true;
}else{
return false;
}
}
public void updateImageMargins(){
MarginLayoutParams marginParams = new MarginLayoutParams(this.tileImage.getLayoutParams());
marginParams.setMargins(this.positionX, this.positionY, this.positionX + imageSizeX, this.positionY + this.imageSizeY);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginParams);
this.tileImage.setLayoutParams(layoutParams2);
}
我想问题是我在 OnTouchListener.onTouch 方法中接收到两个事件。第一个是使用RelativeLayout的坐标(所以它从屏幕的左上角开始)和其他使用ImageView的坐标(它从ImageView的左上角开始)所以当我点击并移动我收到对于具有两个不同事件坐标的事件,如下所示:
TouchListener@44f3bf80:android.widget.ImageView@44f1b4c8 Moved -> X: 59.0 Y: 24.000015
TouchListener@44f3bf80:android.widget.ImageView@44f1b4c8 Moved -> X: 100.0 Y: 148.00002
换句话说,第一行从我在图像视图中点击的位置接收坐标(如果我点击靠近任何边框,这个值会改变),第二行是我在相对布局中点击的位置。因此,当我移动光标时,图像开始从一个点闪烁到另一个点。当我将光标移向任何方向时,这两个值都会按比例变化。