2

我的朋友..我可以在视图上使用手指触摸来移动图像,但现在我想移动文本视图而不是图像但无法这样做..我可以将文本视图视为图像..意味着如果不能转换text-view in to image 然后我将传递图像对象..我在视图中移动图像的代码如下:

      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);
}

}

4

2 回答 2

11

我可以通过添加一个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 坐标中减去标题栏(可能还有通知栏?)的高度。

于 2013-04-05T03:03:07.090 回答
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);
}
于 2021-02-22T09:49:17.640 回答