2010-11-23 112 views
15

我正在處理一些示例,我想要在Android中拖動對應於觸摸的圖像。有人知道我該怎麼做嗎?在Android中觸摸並拖動圖像

+0

如果您嘗試使用本教程.... http://www.anddev.org/basic_drag_and_drop-t3095.html – Noman 2011-12-13 10:08:58

+0

一個`SurfaceView`你可以嘗試按照我的教程部分。 http://www.droidnova.com/playing-with-graphics-in-android-part-vi,209.html在本部分中,我將向您介紹如何在移動觸摸時添加和更改SurfaceView上的位置。之後的部分是一個短小的遊戲,在短期的遊戲中實現所有學到的東西。我認爲,應該幫助你,並告訴你如何做到這一點... – WarrenFaith 2010-11-23 13:12:16

回答

28
public class TouchBall extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    int w=getWindowManager().getDefaultDisplay().getWidth()-25; 
    int h=getWindowManager().getDefaultDisplay().getHeight()-25; 

    BallView ballView=new BallView(this,w,h); 
    setContentView(ballView); 
} 


} 
public class BallView extends SurfaceView implements SurfaceHolder.Callback { 

    private Bitmap bitmap ; 
    private MyThread thread; 
    private int x=20,y=20;int width,height; 

    public BallView(Context context,int w,int h) { 
     super(context); 

     width=w; 
     height=h; 
     thread=new MyThread(getHolder(),this); 
     getHolder().addCallback(this); 
     setFocusable(true); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ball_green); 
     canvas.drawColor(Color.BLUE);//To make background 
     canvas.drawBitmap(bitmap,x-(bitmap.getWidth()/2),y-(bitmap.getHeight()/2),null); 


    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     x=(int)event.getX(); 
     y=(int)event.getY(); 

     if(x<25) 
       x=25; 
     if(x> width) 
       x=width; 
     if(y <25) 
       y=25; 
     if(y > 405) 
       y=405;  
     return true; 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 

     thread.startrun(true); 
     thread.start(); 

    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 


     thread.startrun(false); 
     thread.stop(); 

    } 
} 

螺紋:

public class MyThread extends Thread { 

private SurfaceHolder msurfaceHolder; 
private BallView mballView; 
private boolean mrun =false; 

public MyThread(SurfaceHolder holder, BallView ballView) { 

    msurfaceHolder = holder; 
    mballView=ballView; 
} 

public void startrun(boolean run) { 

    mrun=run; 
} 

@Override 
public void run() { 

    super.run(); 
    Canvas canvas; 
    while (mrun) { 
     canvas=null; 
     try { 
      canvas = msurfaceHolder.lockCanvas(null); 
       synchronized (msurfaceHolder) { 
       mballView.onDraw(canvas); 
      } 
     } finally { 
       if (canvas != null) { 
       msurfaceHolder.unlockCanvasAndPost(canvas); 
      } 
     } 
    } 
    } 

} 
12

作爲一個輕微修改的TouchBall答案 - 如果你真的沒有一個遊戲循環 - 換言之,在屏幕上的唯一變化是直接因用戶輸入 - 那麼離開線程可能更有意義。否則,即使沒有任何變化,它也只是不斷循環和重繪。所以:

public class TouchBall extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     int w=getWindowManager().getDefaultDisplay().getWidth()-25; 
     int h=getWindowManager().getDefaultDisplay().getHeight()-25; 

     BallView ballView=new BallView(this,w,h); 
     setContentView(ballView); 
    } 
} 

public class BallView extends SurfaceView implements SurfaceHolder.Callback { 
    private Bitmap bitmap ; 
    private int x=20,y=20;int width,height; 

    public BallView(Context context,int w,int h) { 
     super(context); 

     width=w; 
     height=h; 
     getHolder().addCallback(this); 
     setFocusable(true); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ball_green); 
     canvas.drawColor(Color.BLUE);//To make background 
     canvas.drawBitmap(bitmap,x-(bitmap.getWidth()/2),y-(bitmap.getHeight()/2),null); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     x=(int)event.getX(); 
     y=(int)event.getY(); 

     if(x<25) 
      x=25; 
     if(x> width) 
      x=width; 
     if(y <25) 
      y=25; 
     if(y > 405) 
      y=405; 

     updateBall(); 

     return true; 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
    } 

    private void updateBall() { 
     Canvas canvas = null; 
     try { 
      canvas = getHolder().lockCanvas(null); 
      synchronized (getHolder()) { 
       this.onDraw(canvas); 
      } 
     } 
     finally { 
      if (canvas != null) { 
       getHolder().unlockCanvasAndPost(canvas); 
      } 
     } 
    } 
} 

無可否認,我是Android開發新手,所以我可能會在這裏丟失一些東西。