Rotating Image on A canvas in android

前端 未结 6 1154
心在旅途
心在旅途 2020-11-27 13:33

I want to Rotate Image according to a specific angle in android ,some thing like a compass...

I have this code...it works on drawPath() but i want to replace the pat

6条回答
  •  暖寄归人
    2020-11-27 14:25

    This is the only one that worked for me with no problem.

        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;
    }
    

提交回复
热议问题