将手指触摸上的textview移动为拖放:android

时间:2012-05-23 05:20:33

标签: android

我的朋友.. 我能够在视图上使用手指触摸移动图像,但现在我想移动文本视图而不是图像但无法做到这一点..我可以将文本视图视为图像..意思是如果生病了能够转换文本视图到图像然后我将传递图像对象..我在视图中移动图像的代码如下:

      public class MainGamePanel extends SurfaceView implements
    SurfaceHolder.Callback {

private static final String TAG = MainGamePanel.class.getSimpleName();

private MainThread thread;
private Droid droid;

public MainGamePanel(Context context) {
    super(context);
    // adding the callback (this) to the surface holder to intercept events
    getHolder().addCallback(this);

    // create droid and load bitmap
    droid = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.droid_1), 50, 50);

    // create the game loop thread
    thread = new MainThread(getHolder(), this);

    // make the GamePanel focusable so it can handle events
    setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // at this point the surface is created and
    // we can safely start the game loop
    thread.setRunning(true);
    thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d(TAG, "Surface is being destroyed");
    // tell the thread to shut down and wait for it to finish
    // this is a clean shutdown
    boolean retry = true;
    while (retry) {
        try {
            thread.join();
            retry = false;
        } catch (InterruptedException e) {
            // try again shutting down the thread
        }
    }
    Log.d(TAG, "Thread was shut down cleanly");
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        // delegating event handling to the droid
        droid.handleActionDown((int)event.getX(), (int)event.getY());

        // check if in the lower part of the screen we exit
        if (event.getY() > getHeight() - 50) {
            thread.setRunning(false);
            ((Activity)getContext()).finish();
        } else {
            Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
        }
    } if (event.getAction() == MotionEvent.ACTION_MOVE) {
        // the gestures
        if (droid.isTouched()) {
            // the droid was picked up and is being dragged
            droid.setX((int)event.getX());
            droid.setY((int)event.getY());
        }
    } if (event.getAction() == MotionEvent.ACTION_UP) {
        // touch was released
        if (droid.isTouched()) {
            droid.setTouched(false);
        }
    }
    return true;
}

@Override
protected void onDraw(Canvas canvas) {
    // fills the canvas with black
    canvas.drawColor(Color.BLACK);
    droid.draw(canvas);
}

}

2 个答案:

答案 0 :(得分:9)

我可以通过向OnTouchListener添加TextView,将视图移动到移动事件的位置来实现此目的。

moveableText.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            // Offsets are for centering the TextView on the touch location
            v.setX(event.getRawX() - v.getWidth() / 2.0f);
            v.setY(event.getRawY() - v.getHeight() / 2.0f);
        }

        return true;
    }

});

这是一个没有标题栏的全屏活动,所以我想你需要从y坐标中减去标题栏的高度(可能还有通知栏?)。

答案 1 :(得分:1)

我想让一个 TextView 只水平移动,上面的代码确实帮助了我,但它看起来有点不稳定,因为 textview 的中间总是到手指。所以我稍微修改了一下代码,让它看起来更自然:

float diffFingerMove = 0;
private void moveViewOnFinger(View view, MotionEvent event){
    float fingerPos = event.getRawX();

    if(event.getAction() == MotionEvent.ACTION_DOWN){
        diffFingerMove = fingerPos - view.getX();
    }
    view.setX(fingerPos - diffFingerMove);
}