2011-11-15 12 views
5

voglio che il mio bitmap nel centro della mia screen..im cercando in questo modo, ma la sua non funziona ...Come allineare centrare una bitmap?

Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.compass); 

      int w = canvas.getWidth(); 
      int h = canvas.getHeight(); 

      int bw=myBitmap.getWidth(); 
      int bh=myBitmap.getHeight(); 

      int cx = w/2; 
      int cy = h/2; 

      Display d = getWindowManager().getDefaultDisplay(); 
      int x = d.getWidth(); 
      int y = d.getHeight(); 
      int dx = x/2; 
      int dw = y /2; 
      canvas.translate(cx, cy);  
      if (mValues != null) { 
       canvas.rotate(-mValues[0]); 
      } 

      int centreX = (x - bw) /2; 

       int centreY = (cy - bh) /2; 
      //canvas.drawPath(mPath, mPaint); 
      canvas.drawBitmap(myBitmap, centreX,centreY, null); 
+0

Qual è il risultato finale con questo codice? (Come è allineata l'immagine) –

+0

In quale classe e funzione usi questo codice? (Attività/Visualizza, onDraw ...) –

+0

lo uso in un vuoto protetto suDraw (Canvas canvas). il risultato è la parte superiore/sinistra della mia immagine nello 0,0 –

risposta

9

Ecco il codice necessario a suo avviso:

private int mWidth; 
private int mHeight; 
private float mAngle; 

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{ 
    mWidth = View.MeasureSpec.getSize(widthMeasureSpec); 
    mHeight = View.MeasureSpec.getSize(heightMeasureSpec); 

    setMeasuredDimension(mWidth, mHeight); 
} 

@Override protected void onDraw(Canvas canvas) 
{ 
    super.onDraw(canvas); 
    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.compass); 

    // Here's the magic. Whatever way you do it, the logic is: 
    // space available - bitmap size and divide the result by two. 
    // There must be an equal amount of pixels on both sides of the image. 
    // Therefore whatever space is left after displaying the image, half goes to 
    // left/up and half to right/down. The available space you get by subtracting the 
    // image's width/height from the screen dimensions. Good luck. 

    int cx = (mWidth - myBitmap.getWidth()) >> 1; // same as (...)/2 
    int cy = (mHeight - myBitmap.getHeight()) >> 1; 

    if (mAngle > 0) { 
     canvas.rotate(mAngle, mWidth >> 1, mHeight >> 1); 
    } 

    canvas.drawBitmap(myBitmap, cx, cy, null); 
} 

Screenshot solo per divertimento: (le linee diagonali non fanno parte del codice pubblicato qui)

MODIFICA: aggiunta la soluzione di NickT.
EDIT 2: ha cambiato i valori m [0] in mAngle e lo ha reso condizionale. Divisione modificata per 2 operazioni a bitshift. Rimuovi il codice di rotazione se non ti serve.

+1

Dovrebbe funzionare, ma il punto di rotazione per la rotazione avrà bisogno di passare a canvas.rotate (-mValues ​​[0], mWidth/2, altezza/2), altrimenti sarà ruotare su un angolo, non al centro – NickT

+0

grazie !! questo sta funzionando bene! un'ultima risposta per favore.non c'è problema im non usando canvas.translate (cx, cy); if (mValues! = Null) { canvas.rotate (-mValues ​​[0]); } –

+0

Grazie a @NickT per la rotazione. Modificato nella risposta. –

3

Si può provare questo:

 int width = containerBitmap.getWidth(); 
     int height = containerBitmap.getHeight(); 
     float centerX = (width - centeredBitmap.getWidth()) * 0.5f; 
     float centerY = (height- centeredBitmap.getHeight()) * 0.5f; 

Si può usare per disegnare una bitmap al centro di un'altra bitmap.

Problemi correlati