Scale & rotate Bitmap using Matrix in Android

前端 未结 8 1684
名媛妹妹
名媛妹妹 2020-12-12 19:26

I\'m trying to scale and rotate in single operation before creting the final bitmap but the preRotate, postConcat doesn\'t seem to work.

Bitmap bmp = ... ori         


        
相关标签:
8条回答
  • 2020-12-12 20:10
    Matrix rotateMatrix = new Matrix();
    rotateMatrix.postRotate(rotation);
    rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,loadedImage.getWidth(), loadedImage.getHeight(),rotateMatrix, false);
    
    0 讨论(0)
  • 2020-12-12 20:12

    Refer to the following code, seems to work. In your code you are defining Matrix as m but referring to it as matrix

    public class FourthActivity extends Activity {
    private static final int WIDTH = 50;
    private static final int HEIGHT = 50;
    private static final int STRIDE = 64;  
    
    private static int[] createColors() {
        int[] colors = new int[STRIDE * HEIGHT];
        for (int y = 0; y < HEIGHT; y++) {
            for (int x = 0; x < WIDTH; x++) {
                int r = x * 255 / (WIDTH - 1);
                int g = y * 255 / (HEIGHT - 1);
                int b = 255 - Math.min(r, g);
                int a = Math.max(r, g);
                colors[y * STRIDE + x] = (a << 24) | (r << 16) | (g << 8) | b;
            }
        }
        return colors;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        final ImageView view1 = (ImageView) findViewById(R.id.imageView1);
    
        int[] colors = createColors();
        final Bitmap bmp1 = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,
        Bitmap.Config.ARGB_8888);
        view1.setImageBitmap(bmp1);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {   
                Matrix matrix = new Matrix();
                matrix.setScale(2, 2);
                matrix.preRotate(45, (float) WIDTH / 2, (float) HEIGHT / 2);
    
                Bitmap bmp2 = Bitmap.createBitmap(bmp1, 0, 0, 
          bmp1.getWidth(), bmp1.getHeight(), matrix, true);
                ImageView view2 = (ImageView) findViewById(R.id.imageView2);
                view2.setImageBitmap(bmp2);
            }
        });
    }
    }
    
    0 讨论(0)
提交回复
热议问题