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

标签 android image

我想将图片旋转 90 度,还想裁剪从手机图库中取出的图片。我如何在 Android 中以编程方式执行此操作?

最佳答案

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

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

对于从图库中截取的图像,请使用以下代码 fragment :

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

关于android - 如何在 Android 中以编程方式裁剪和旋转图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6571509/

相关文章:

android - 创建一个从右到左的进度条

javascript - 无法捕获 Android Tv webview 应用程序上的键码(HTML+Javascript)

javascript - 点击后将图像更改为另一个图像

image - 如何检查完整屏幕截图上是否有特定图像?

Javascript Image() 无法加载或绘制

java - Swing PixelGrabber - 数据排序

java - 为什么从 ViewModel 进行调用时,Retrofit enqueue 不起作用?

Android Ndk(未找到 xxxxx 的实现)

android - Google Play 上传问题 : "Your Android App Bundle needs to have the package name com.x.x.base"

asp.net - 图像未从主题布局中显示(Orchard CMS)