裁剪图像到方形 – Android

前端之家收集整理的这篇文章主要介绍了裁剪图像到方形 – Android前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何从左右剪切矩形图像(600 x 300),以适应方形 ImageView?我不想调整图像的大小,我只想裁剪它,为300 x 300.

[解]

正如@blackbelt所言

Bitmap cropImg = Bitmap.createBitmap(src,startX,startY,dstWidth,dstHeight);

是伟大的裁剪图像.那么如何自动裁剪不同尺寸的图像.我为此创建了这个简单的代码

// From drawable
Bitmap src= BitmapFactory.decodeResource(context.getResources(),R.drawable.image);

// From URL
Bitmap src = null;
try {
    String URL = "http://www.example.com/image.jpg";
    InputStream in = new java.net.URL(URL).openStream();
    src = BitmapFactory.decodeStream(in);
} catch (Exception e) {
    e.printStackTrace();
}

int width = src.getWidth();
int height = src.getHeight();
int crop = (width - height) / 2;
Bitmap cropImg = Bitmap.createBitmap(src,crop,height,height);

ImageView.setImageBitmap(cropImg);

解决方法

您可以使用
Bitmap dst = Bitmap.createBitmap(src,dstHeight);

从文档:

Returns an immutable bitmap from the specified subset of the source
bitmap. The new bitmap may be the same object as source,or a copy may
have been made. It is initialized with the same density as the
original bitmap.

Here可以找到文档

原文链接:https://www.f2er.com/android/311294.html

猜你在找的Android相关文章