SurfaceView,绘制图像并缩小尺寸,旋转图像

0 人关注

我想在surfaceview上画一个图像(从相机得到的)。在画完图像后,我想缩小图像的大小,用触摸的方式改变图像在表面视图上的位置,旋转图像。请大家帮助我。

android
android-canvas
user1595266
user1595266
发布于 2014-01-20
2 个回答
Madhusudan
Madhusudan
发布于 2022-02-20
已采纳
0 人赞同
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);

使用上述代码,在surfaceview中旋转和缩放图像。 如果缩放系数为1.0f,意味着图像的大小将保持不变。如果你想减小尺寸,请使用0.5f。 旋转角度是0到360

farhad.kargaran
farhad.kargaran
发布于 2022-02-20
0 人赞同

这更灵活,你可以根据缩放或特定的宽度和高度来绘制位图。

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());
        matrix.postScale(scalingfactor, scalingfactor, bitmap.getWidth()/2, bitmap.getHeight()/2);
    dstRectF.set(x, y, x + width, y + height);