我試圖建立一個使用android 2.1(sdk 2.1)的拖放功能。我搜索了網頁,只能使用android 4.0+。 我有一個ImageView(用一些屬性和屬性包裝的類)創建並使用此代碼動態設置。imageView與OnTouchListener的雙重事件Android
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的左上角開始),所以當我點擊並移動時,我會收到與事件的兩個不同的座標,像這樣的活動:
[email protected]:[email protected] Moved -> X: 59.0 Y: 24.000015
[email protected]:[email protected] Moved -> X: 100.0 Y: 148.00002
換句話說,第一行收到我點擊的位置座標在ImageView的(如果我點擊越靠近任何邊界這個值會改變),第二個是我在相關佈局中點擊的位置。所以當我移動光標時,圖像開始從一點閃爍到另一點。當我將光標向任何方向移動時,兩個值都會成比例變化。
您是否找到任何解決方案?我有同樣的問題 – user1512094