使用OnTouchListener Android的imageView中的双重事件

时间:2013-04-02 14:23:01

标签: android android-layout android-imageview ontouchlistener

我正在尝试使用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

换句话说,第一行从我在图像视图中的点击位置接收坐标(如果我点击更接近这个值将改变的任何边界),第二行是我在相对布局中点击的位置。因此,当我移动光标时,图像开始从一个点闪烁到另一个点。当我将光标移向任何方向时,两个值都会按比例变化。

1 个答案:

答案 0 :(得分:0)

尝试使用motionEvent.getRawX()motionEvent.getRawY()代替getX()getY()。您可能需要进行其他调整,以便在View内正确对齐。