English 中文(简体)
手指触碰时拖放时移动文本视图: 和机器人
原标题:move textview on finger touch as drag and drop:android
  • 时间:2012-05-23 05:20:32
  •  标签:
  • android

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

时 时

问题回答

实现此目的的一个简单办法是在您的 < code> TextView 中添加 < code> onTouchliarer , 将视图移动到移动事件的位置 。

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 坐标中减去标题栏的高度(可能还有通知栏)。

我想做一个文本View 仅横向移动, 上面的代码真的帮助了我, 但是它看起来有点摇摆, 因为文本View 的中间总是指向手指。 所以我对代码做了一点修改, 让它看起来更自然:

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签