2011-06-17 158 views
3

我是Android新手。我想從左到右,從右到左移動圖像。當用戶觸摸圖像的右側圖像時,它會向右側移動。當用戶觸摸圖像的右側時,會發生一些事情。但圖像將移動預定義點橫跨x軸。如何在Android中將圖像從左到右從右向左移動?

例如:圖像將依次移動這些點上的p1(100,100),p2(150,100),p3(200,100),p4(250,100)。如果用戶觸摸p1的左側,它將保持當前位置。在p4將發生所有事情。我可以將圖像從p1移動到p2,將p2移動到p1。當我添加p3和p4時,它不像我預期的那樣工作。

這是我的GameView類。

public class GameView extends SurfaceView{ 
    private Bitmap BG_image; 
    private SurfaceHolder holder; 
    //private GameLoopThread gameLoopThread;  
    int x; 
    int y; 
    private int srcX=100; 
    private int srcY=100; 
    int replaceX=0,areaThreshold=50; 
    int distance=50; 

    public GameView(Context context) { 

     super(context); 
     //gameLoopThread = new GameLoopThread(this); 
     holder = getHolder(); 
     holder.addCallback(new SurfaceHolder.Callback() { 

      @Override 
      public void surfaceDestroyed(SurfaceHolder holder) { 

/*    boolean retry = true; 
       gameLoopThread.setRunning(false); 

       while (retry) { 

        try { 

         gameLoopThread.join(); 
         retry = false; 

        } catch (InterruptedException e) { 

        } 
       }*/ 
      } 

      @Override 
      public void surfaceCreated(SurfaceHolder holder) { 

/*    gameLoopThread.setRunning(true); 
       gameLoopThread.start();*/ 
       Canvas c = holder.lockCanvas(null); 
       onDraw(c); 
       holder.unlockCanvasAndPost(c); 
      } 

      @Override 
      public void surfaceChanged(SurfaceHolder holder, int format, 

        int width, int height) { 
      } 
     }); 
     BG_image = BitmapFactory.decodeResource(getResources(), R.drawable.icon); 
     Bitmap.createScaledBitmap(BG_image, BG_image.getWidth(), BG_image.getHeight(), false); 
    } 
    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     // TODO Auto-generated method stub 
       // x = (int) ev.getX(); 
       // y = (int) ev.getY(); 
       updateX(srcX); 
       replaceX=(int)ev.getX(); 
       int StartX=0, EndX=0; 
       StartX=replaceX-areaThreshold; 
       EndX=replaceX+areaThreshold; 
       if(StartX<=100){ 
        SurfaceHolder holder=getHolder(); 
        Canvas myCanvas=holder.lockCanvas(null); 
        onDraw(myCanvas); 
        holder.unlockCanvasAndPost(myCanvas); 
        srcX=100; 
       } 
       else if(StartX>100 && StartX<250){ 
        SurfaceHolder holder=getHolder(); 
        Canvas myCanvas=holder.lockCanvas(null); 
        onDraw(myCanvas); 
        holder.unlockCanvasAndPost(myCanvas); 
        srcX=srcX+distance; 
       } 
       if(EndX>100 && EndX<250){ 
        SurfaceHolder holder=getHolder(); 
        Canvas myCanvas=holder.lockCanvas(null); 
        onDraw(myCanvas); 
        holder.unlockCanvasAndPost(myCanvas); 
        srcX=srcX-distance; 
       } 
       else if(EndX>250){ 
        SurfaceHolder holder=getHolder(); 
        Canvas myCanvas=holder.lockCanvas(null); 
        onDraw(myCanvas); 
        holder.unlockCanvasAndPost(myCanvas); 
        srcX=250; 
       } 

     return super.onTouchEvent(ev); 
    } 
    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     updateX(srcX); 
     Paint paint = new Paint(); 
     canvas.drawColor(Color.BLACK); 
     canvas.drawRect(new Rect(0,0,getWidth(),getHeight()),paint); 
     canvas.drawBitmap(BG_image, srcX, srcY, null); 
    } 

    private int updateX(int srcX){ 
     this.srcX =srcX; 
     return srcX ; 

    } 
} 

請仔細閱讀我的代碼,給你解決我的問題提寶貴意見。 希望我會盡快回復!

+0

您的代碼註釋掉和混亂,你的解釋有邏輯上的缺陷的詳細信息。你可以做得更好。 – Anko 2011-06-18 12:46:35

+0

同意Aku。另外,你不應該直接調用'onDraw',你應該設置srcX值並調用'invalidate',它告訴系統安排一次調用'onDraw'。而'updateX'方法似乎沒有任何用途。我認爲你可以在這裏消除大約一半的代碼。 – DocDude 2011-06-21 17:32:25

回答

0

現在,我明白我的問題。非常感謝您提出您的建議。 這是我更新的代碼。

public class GameView extends SurfaceView{ 
    private Bitmap BG_image; 
    private SurfaceHolder holder; 
    int x=100; 
    private int srcX=100; 
    private int srcY=100; 
    int distance=50; 

    public GameView(Context context) { 

     super(context); 
     holder = getHolder(); 
     holder.addCallback(new SurfaceHolder.Callback() { 

      @Override 
      public void surfaceDestroyed(SurfaceHolder holder) { 

      } 

      @Override 
      public void surfaceCreated(SurfaceHolder holder) { 

       Canvas c = holder.lockCanvas(null); 
       onDraw(c); 
       holder.unlockCanvasAndPost(c); 
      } 

      @Override 
      public void surfaceChanged(SurfaceHolder holder, int format, 

        int width, int height) { 
      } 
     }); 
     BG_image = BitmapFactory.decodeResource(getResources(), R.drawable.icon); 
     Bitmap.createScaledBitmap(BG_image, BG_image.getWidth(), BG_image.getHeight(), false); 
    } 
    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     // TODO Auto-generated method stub 
     x = (int) ev.getX();   
     if(x-srcX>0) 
      srcX += distance; 
     else if(x-srcX<0) 
      srcX -= distance; 

     if(srcX<=100) 
      srcX = 100; 
     else if(srcX>250) 
      srcX = 250;  
     Log.w("ev.getX : srcX",x+" : "+srcX); 
     SurfaceHolder holder=getHolder(); 
     Canvas myCanvas=holder.lockCanvas(null); 
     onDraw(myCanvas); 
     holder.unlockCanvasAndPost(myCanvas); 
     return super.onTouchEvent(ev); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     Paint paint = new Paint(); 
     canvas.drawColor(Color.BLACK); 
     canvas.drawRect(new Rect(0,0,getWidth(),getHeight()),paint); 
     canvas.drawBitmap(BG_image, srcX-BG_image.getWidth()/2, srcY, null); 
    } 
} 
4

這是很容易與Android TranslateAnimation

ImageView img_animation = (ImageView) findViewById(R.id.img_animation); 

TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f, 
      0.0f, 0.0f);   // new TranslateAnimation(xFrom,xTo, yFrom,yTo) 
    animation.setDuration(5000); // animation duration 
    animation.setRepeatCount(5); // animation repeat count 
    animation.setRepeatMode(2); // repeat animation (left to right, right to left) 
    //animation.setFillAfter(true);  

    img_animation.startAnimation(animation); // start animation 

找到這裏move an image from left to right and right to left in android