如何在android中以编程方式裁剪和旋转图像?

6 android image

我想将图像旋转90度,还想裁剪从手机画廊中取出的图像.如何在android中以编程方式执行此操作?

Nik*_*kki 12

要执行图像的旋转,您可以使用以下代码:

Bitmap bMap = BitmapFactory.decodeResource(getResources(),R.drawable.test);
Matrix mat = new Matrix();
mat.postRotate(90);
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,
                             bMap.getWidth(), bMap.getHeight(), mat, true);
BitmapDrawable bmd = new BitmapDrawable(bMapRotate);
image.setImageBitmap(bMapRotate);
image.setImageDrawable(bmd);
Run Code Online (Sandbox Code Playgroud)

对于从图库中获取的图像裁剪,请使用以下代码片段:

    Intent viewMediaIntent = new Intent();
    viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
    File file = new File("/image/*");    
    viewMediaIntent.setDataAndType(Uri.fromFile(file), "image/*");   
    viewMediaIntent.putExtra("crop","true");
    viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivityForResult(viewMediaIntent,1);
Run Code Online (Sandbox Code Playgroud)

希望,这对你有所帮助.