如何裁剪位图中心的像ImageView的? [英] How to crop Bitmap Center like imageview?

查看:106
本文介绍了如何裁剪位图中心的像ImageView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  如何裁剪在Android上的解析图像?

怎样一种作物的相同方式机器人的ImageView

 安卓scaleType =centerCrop
 

解决方案

你的问题是有点短信息,你想要完成的任务,但我猜你有一个位图,并希望以该到一个新的大小和该比例应作为centerCrop适用于ImageViews。

从<一个href="http://developer.android.com/reference/android/widget/ImageView.ScaleType.html%20about%20centerCrop%3a">Docs

  

缩放图像均匀(保持图像的长宽比),使   图像的两个尺寸(宽度和高度)将等于或   比视图(负填充)的对应尺寸更大的

据我所知,目前还没有一个班轮做到这一点(请纠正我,如果我错了),但你可以写你自己的方法来做到这一点。下面的方法计算出如何缩放原始位图到新的大小,并绘制它的生成位图的中心。

希望它能帮助!

 公共位图scaleCenterCrop(位图​​源,诠释newHeight,诠释newWidth){
    INT sourceWidth = source.getWidth();
    INT sourceHeight = source.getHeight();

    //计算缩放因子,以适应新的高度和宽度,分别。
    //为了覆盖最终图像,最终的缩放将是更大的
    //这两个。
    浮xScale等=(浮点)newWidth / sourceWidth;
    浮动yScale =(浮点)newHeight / sourceHeight;
    浮规模= Math.max(xScale等,yScale);

    //现在得到的源位图的大小缩放时
    浮scaledWidth =规模* sourceWidth;
    浮scaledHeight =规模* sourceHeight;

    //让我们看看左上角的坐标,如果缩放位图
    //应该由这些参数给新的大小为中心
    左浮动=(newWidth  -  scaledWidth)/ 2;
    浮顶=(newHeight  -  scaledHeight)/ 2;

    //为源位图新,图像的缩放目标矩形将现
    // 是
    RectF targetRect =新RectF(左,上,左+ scaledWidth,顶部+ scaledHeight);

    //最后,我们创建指定大小的新位图,并得出我们新的,
    //位图缩放到它。
    位图DEST = Bitmap.createBitmap(newWidth,newHeight,source.getConfig());
    帆布油画=新的Canvas(DEST);
    canvas.drawBitmap(源,空,targetRect,NULL);

    返回DEST;
}
 

Possible Duplicate:
How to crop the parsed image in android?

How does one crop the same way as Androids ImageView is doing

android:scaleType="centerCrop"

解决方案

Your question is a bit short of information on what you want to accomplish, but I guess you have a Bitmap and want to scale that to a new size and that the scaling should be done as "centerCrop" works for ImageViews.

From Docs

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).

As far as I know, there is no one-liner to do this (please correct me, if I'm wrong), but you could write your own method to do it. The following method calculates how to scale the original bitmap to the new size and draw it centered in the resulting Bitmap.

Hope it helps!

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();

    // Compute the scaling factors to fit the new height and width, respectively.
    // To cover the final image, the final scaling will be the bigger 
    // of these two.
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    // Now get the size of the source bitmap when scaled
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    // Let's find out the upper left coordinates if the scaled bitmap
    // should be centered in the new size give by the parameters
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;

    // The target rectangle for the new, scaled version of the source bitmap will now
    // be
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

    // Finally, we create a new bitmap of the specified size and draw our new,
    // scaled bitmap onto it.
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
    Canvas canvas = new Canvas(dest);
    canvas.drawBitmap(source, null, targetRect, null);

    return dest;
}

这篇关于如何裁剪位图中心的像ImageView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆