本文介绍了移动的TextView上手指触摸的拖放:机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的朋友..
我能够用手指触摸在一个视图移动的图像,但现在我想移动文本视图,而不是图像,但无法做到that..can我将文本视图图像..means如果病能够转换文本视图到图像,然后我会把图像对象..my code表示,鉴于移动图像是如下:

i friends..i am able to move image using finger touch over a view but now i want to move text view rather then image but unable to do that..can i treat text view as image ..means if ill be able to convert text-view in to image then i will pass image object ..my code for moving image in view are below:

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

}

推荐答案

我能够通过添加 OnTouchListener 来做到这一点我的的TextView ,移动以移动事件的位置。

I was able to accomplish this by adding an OnTouchListener to my TextView, moving the view to the location of move events.

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坐标。

This was in a fullscreen activity with no title bar, so I suppose that you would need to subtract the height of the title bar (and possibly notification bar?) from the y-coordinate otherwise.

这篇关于移动的TextView上手指触摸的拖放:机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:53