2014-04-29 75 views
0

當我旋轉一個位圖時,它會旋轉,但它會一直在屏幕上移動並同時旋轉。我想讓它在中心旋轉?我的代碼如下。位圖定位

try { 
    c = holder.lockCanvas(); 
    // clear the canvas 
    c.drawColor(Color.BLACK); 
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG); 
    if (c != null) { 

     int width=c.getWidth(); 
     int height=c.getHeight(); 

     c.drawBitmap(backgroundImage, 0, 0, null); 
     Matrix transform = new Matrix(); 
     Matrix transform1=new Matrix(); 

     transform.setRotate(degree, width/2,height/2); 
     c.drawBitmap(image1, transform, null); 
     //canvas.rotate(-90); 

     degree+=5; 
     c.restore(); 
    } 
} 

回答

1

試試這個代碼..

  c = holder.lockCanvas(); 
      c.drawColor(Color.BLACK); 
      Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG); 
       if (c != null) 
        { 
        int width=c.getWidth(); 
        int height=c.getHeight(); 
        c.drawBitmap(backgroundImage, 0, 0, null); 

        Matrix matrix = new Matrix(); 

        float px = width/2; 

        float py = height/2; 

        matrix.postTranslate(-image1.getWidth()/2, -image1.getHeight()/2); 

        matrix.postRotate(degree); 

        matrix.postTranslate(px, py); 

        c.drawBitmap(image1, matrix, paint); 

         degree+=5; 

         c.restore(); 
      }