Question

I wan to draw a image (get from camera) on surfaceview. After drawing the image i want to reduce the size of the image, change the image location on surface view with touch. rotate the image. currently i draw the image on surface view but how the remaining task is not done. Please some body help me.

Thanks

Was it helpful?

Solution

public void drawImage(Canvas canvas, Bitmap bitmap, int x, int y, int rotationAngle, float scalingfactor){
        Matrix matrix = new Matrix();
        matrix.postRotate(rotationAngle, bitmap.getWidth()/2, bitmap.getHeight()/2);
        matrix.postScale(scalingfactor, scalingfactor, bitmap.getWidth()/2, bitmap.getHeight()/2);
        matrix.postTranslate(x, y);
        canvas.drawBitmap(bitmap, matrix, null);
    }

Use above code to rotate and scaling image in surfaceview. if scalingfactor 1.0f means, image size will be same. If you want to reduce the size use like 0.5f. rotationAngle is 0 to 360

OTHER TIPS

This is more flexible, you can draw bitmap based on scaling or specific width and height:

public void drawImage(Bitmap bitmap, int x, int y, int rotationAngle, float scalingfactor, int width, int height){
    Matrix matrix = new Matrix();
    matrix.postRotate(rotationAngle, bitmap.getWidth()/2, bitmap.getHeight()/2);
    if(scalingfactor == 0) {
        matrix.postScale((float) width / bitmap.getWidth(), (float) height / bitmap.getHeight());
    }
    else
        matrix.postScale(scalingfactor, scalingfactor, bitmap.getWidth()/2, bitmap.getHeight()/2);
    dstRectF.set(x, y, x + width, y + height);

    matrix.postTranslate(x, y);
    canvas.drawBitmap(bitmap, matrix, null);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top