2012-01-03 34 views
22

Tôi muốn Xoay hình ảnh theo một góc độ cụ thể trong android, một số điều như một la bàn ...quay hình ảnh trên một canvas trong android

tôi có mã này ... nó hoạt động trên drawPath() nhưng tôi muốn thay thế đường dẫn và điều Vẽ bằng hình ảnh .. Tôi đã cố gắng tạo một hình ảnh bitmap, DrawBitmapImage, nhưng hình ảnh không Xoay như đường dẫn..Bất cứ trợ giúp nào?

public void draw(Canvas canvas) { 
    double angle = calculateAngle(currentLongitude, currentLatitude, targetLongitude, targetLatitude); 

    //Correction; 
    angle-=90; 

    //Correction for azimuth 
    angle-=azimuth; 

    if((getContext() instanceof Activity) && ((Activity)getContext()).getWindowManager().getDefaultDisplay().getOrientation()==Configuration.ORIENTATION_PORTRAIT)angle-=90; 

    while(angle<0)angle=angle+360; 

    Rect rect = canvas.getClipBounds(); 

    int height = rect.bottom-rect.top; 
    int width = rect.right-rect.left; 
    int left = rect.left; 
    int top = rect.top; 

    if(height>width){ 
     top+=(height-width)/2; 
     height=width; 
    } 
    if(width>height){ 
     left+=(width-height)/2; 
     width=height; 
    } 

    float centerwidth = width/2f; 
    float centerheight = height/2f; 

    Paint p = new Paint(); 
    p.setColor(color); 
    p.setStyle(Paint.Style.FILL); 
    p.setAntiAlias(true); 

    float startX = left+(float)(centerwidth+Math.cos(deg2rad(angle))*width/3.0); 
    float startY = top+(float)(centerheight+Math.sin(deg2rad(angle))*height/3.0); 

    Path path = new Path(); 
    path.moveTo(
      startX, 
      startY); 
    path.lineTo(
      left+(float)(centerwidth+Math.cos(deg2rad(angle+140))*width/4.0), 
      top+(float)(centerheight+Math.sin(deg2rad(angle+140))*height/4.0)); 
    path.lineTo(
      left+(float)centerwidth, 
      top+(float)centerheight 
      ); 
    path.lineTo(
      left+(float)(centerwidth+Math.cos(deg2rad(angle+220))*width/4.0), 
      top+(float)(centerheight+Math.sin(deg2rad(angle+220))*height/4.0) 
      ); 

    path.lineTo(
      startX, 
      startY 
      ); 




    canvas.drawPath(path, p); 
} 

Trả lời

48

Bạn có thể xoay bitmap của bạn khi bạn vẽ nó bằng cách sử dụng một ma trận:

Matrix matrix = new Matrix(); 
matrix.setRotate(angle, imageCenterX, imageCenterY); 
yourCanvas.drawBitmap(yourBitmap, matrix, null); 

Bạn cũng có thể làm điều đó bằng cách xoay canvas trước khi vẽ:

yourCanvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated. 
yourCanvas.rotate(-angle); 
yourCanvas.drawBitmap(yourBitmap, left, top, null); 
yourCanvas.restore(); 

Chọn loại phù hợp nhất với bạn.

+0

@Jave, tôi nghĩ cách tiếp cận đầu tiên có thể được sử dụng, cụ thể là bạn muốn xoay hình ảnh hoặc bitmap xung quanh trục x và/hoặc trục y vì 'Canvas' không có phương thức xoay khác hiện nó trong mặt phẳng XY. Tôi nghĩ rằng người ta có thể nhận được một đối tượng 'Camera' và thực hiện các phép biến đổi 3D cần thiết và cuối cùng sử dụng' getMatrix' để truyền thể hiện 'Matrix' để có được biểu diễn' Matrix' tương ứng. Cuối cùng, chuyển phương thức 'Matrix' sang' drawBitmap' này. Cụ thể, [ở đây] (http://stackoverflow.com/questions/32554925/android-animate-rotation-of-map-marker-around-x-and-y-axis): Bất kỳ suy nghĩ nào. Cảm ơn! –

3

Bạn phải xoay canvas trước và sau đó vẽ bất kỳ thứ gì bạn muốn. Sau đó, đối tượng được vẽ sẽ xuất hiện như xoay trên màn hình.

canvas.rotate(45); // degrees to rotate 

hãy thử cách này tốt.

Kiểm tra hướng dẫn này bạn sẽ nhận được thông tin về làm thế nào để vẽ bitmap và làm thế nào để xoay canvas

Check complete tutorial

+0

Xoay canvas theo góc? Vậy thì sao? Chỉ cần tạo Bitmap và vẽ nó – Reham

+0

có vẻ như bạn không đọc thông tin có sẵn trên trang web của Nhà phát triển Android. Trong lớp Canvas, nhiều chức năng có sẵn để vẽ Bitmap. Bạn phải gọi bất kỳ drwaBitmap() sau canvas.rotate() theo yêu cầu của bạn. –

+0

@Reham kiểm tra câu trả lời mới –

1

@Reham: Nhìn vào ví dụ mã này dưới đây,

public class bitmaptest extends Activity { 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    LinearLayout linLayout = new LinearLayout(this); 

    // load the origial BitMap (500 x 500 px) 
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
      R.drawable.android); 

    int width = bitmapOrg.width(); 
    int height = bitmapOrg.height(); 
    int newWidth = 200; 
    int newHeight = 200; 

    // calculate the scale - in this case = 0.4f 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    // createa matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 
    // rotate the Bitmap 
    matrix.postRotate(45); 

    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
         width, height, matrix, true); 

    // make a Drawable from Bitmap to allow to set the BitMap 
    // to the ImageView, ImageButton or what ever 
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 

    ImageView imageView = new ImageView(this); 

    // set the Drawable on the ImageView 
    imageView.setImageDrawable(bmd); 

    // center the Image 
    imageView.setScaleType(ScaleType.CENTER); 

    // add ImageView to the Layout 
    linLayout.addView(imageView, 
      new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT 
      ) 
    ); 

    // set LinearLayout as ContentView 
    setContentView(linLayout); 
} 
} 

bạn phải sử dụng ma trận để xoay hình ảnh nhìn dòng

matrix.postRotate(45); - 

này sẽ xoay hình ảnh 45 độ

Hy vọng điều này sẽ giúp bạn ... thx

+0

Tôi nghĩ bạn đã gõ 'bitmapOrg.width() 'cho' bitmapOrg.getWidth() '. –

1

Sử dụng mã sau. nó hoạt động cho tôi

xoay vòng = 30.0f;

 Bitmap bitmap = your bitmap 
    Rect rect = new Rect(100,100,bitmap.width, bitmap.height); 
    Matrix matrix = new Matrix(); 
    float px = rect.exactCenterX(); 
    float py = rect.exactCenterY(); 
    matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2); 
    matrix.postRotate(rotation); 
    matrix.postTranslate(px, py); 
    canvas.drawBitmap(bitmap, matrix, null); 
    matrix.reset(); 
    invalidate(); 
+0

Điều này thật tuyệt. Chỉ cần thực hiện sửa đổi nhỏ để làm cho nó bắt đầu từ 0,0 – MSaudi

+0

Ya chắc chắn. Rect rect = new Rect (0, 0, bitmap.width, bitmap.height); – Sakthi

1

Đây là người duy nhất làm việc cho tôi mà không gặp vấn đề gì.

private Bitmap rotateBitmap(Bitmap bitmap, int rotationAngleDegree){ 

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

     int 

newW=w, newH=h; 
    if (rotationAngleDegree==90 || rotationAngleDegree==270){ 
     newW = h; 
     newH = w; 
    } 
    Bitmap rotatedBitmap = Bitmap.createBitmap(newW,newH, bitmap.getConfig()); 
    Canvas canvas = new Canvas(rotatedBitmap); 

    Rect rect = new Rect(0,0,newW, newH); 
    Matrix matrix = new Matrix(); 
    float px = rect.exactCenterX(); 
    float py = rect.exactCenterY(); 
    matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2); 
    matrix.postRotate(rotationAngleDegree); 
    matrix.postTranslate(px, py); 
    canvas.drawBitmap(bitmap, matrix, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG)); 
    matrix.reset(); 

    return rotatedBitmap; 
} 
Các vấn đề liên quan