美文网首页Android知识点点滴汇聚
ImageView长图截取部分展示

ImageView长图截取部分展示

作者: 幸福的程序媛 | 来源:发表于2017-05-08 16:01 被阅读52次

开发中有需求,类似百度贴吧发图时,图片的备选面板,对于特别长的图,如果不加以处理因图片太大,会出现加载不出来,解决策略是对于长图先根据宽度缩放比,等比缩放到期望尺寸,注意这样等比缩放,是防止图片变形。
缩放好后根据需要高度截取需要的高度图片。代码如下

  /**
     * 根据期望宽高度截取图片
     *
     * @param sourceBitmap
     * @param targetWidth  目标宽度
     * @param targetHeight  目标高度             
     * @return
     */
    public static Bitmap scaledBitmapByWidth(Bitmap sourceBitmap, int targetWidth, int targetHeight) {
        int bitmapWidth = sourceBitmap.getWidth();
        int bitmapHeight = sourceBitmap.getHeight();
        if (bitmapHeight == 0) {
            return null;
        }

        float widthRatio = targetWidth * 1.0f / bitmapWidth * 1.0f;
        Bitmap scaleBitmap = null;
        try {
            scaleBitmap = Bitmap.createScaledBitmap(sourceBitmap, targetWidth, (int) (bitmapHeight * widthRatio), true); //先根据宽度比,等比缩放,然后截取需要的部分

        } catch (Exception e) {
            LogUtil.e("--e is " + e);
            return null;
        }
        int sourceWidth = scaleBitmap.getWidth();
        int soureHeight = scaleBitmap.getHeight();
        LogUtil.d("--width radio is " + widthRatio + " targeWidth is " + targetWidth + " bitmapWidth is " + bitmapWidth);
        int needHeight = targetHeight > soureHeight ? soureHeight : targetHeight;//如果需要的高度比图片原图的高度大,则用原图高度,否则使用期望的高度
        Bitmap resultBitmap = null;
        try {
            resultBitmap = Bitmap.createBitmap(scaleBitmap, 0, 0, sourceWidth, needHeight);
            scaleBitmap.recycle();
        } catch (Exception e) {
            LogUtil.e("----e is " + e);
        }
        return resultBitmap;


    }

探究下Bitmap.createScaledBitmap的源码。

/**
     * Creates a new bitmap, scaled from an existing bitmap, when possible. If the
     * specified width and height are the same as the current width and height of
     * the source bitmap, the source bitmap is returned and no new bitmap is
     * created.
     *
     * @param src       The source bitmap.
     * @param dstWidth  The new bitmap's desired width.(目标宽度)
     * @param dstHeight The new bitmap's desired height.(目标高度)
     * @param filter    true if the source should be filtered.
     * @return The new scaled bitmap or the source bitmap if no scaling is required.
     * @throws IllegalArgumentException if width is <= 0, or height is <= 0
     */
    public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,
            boolean filter) {
        Matrix m;
        synchronized (Bitmap.class) {
            // small pool of just 1 matrix
            m = sScaleMatrix;
            sScaleMatrix = null;
        }

        if (m == null) {
            m = new Matrix();
        }

        final int width = src.getWidth();
        final int height = src.getHeight();
        final float sx = dstWidth  / (float)width;
        final float sy = dstHeight / (float)height;
        m.setScale(sx, sy);//(此处直接使用matrix.setScale)
        Bitmap b = Bitmap.createBitmap(src, 0, 0, width, height, m, filter);

        synchronized (Bitmap.class) {
            // do we need to check for null? why not just assign everytime?
            if (sScaleMatrix == null) {
                sScaleMatrix = m;
            }
        }

        return b;
    }

Bitmap.createBitmap的参数含义

   /
     * 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.
     *
     * @param source   The bitmap we are subsetting
     * @param x        The x coordinate of the first pixel in source(x方向上从哪个像素点开始截取图片)
     * @param y        The y coordinate of the first pixel in source(y方向上从哪个像素点开始截取图片)
     * @param width    The number of pixels in each row 
     * @param height   The number of rows
     * @return A copy of a subset of the source bitmap or the source bitmap itself.
     * @throws IllegalArgumentException if the x, y, width, height values are
     *         outside of the dimensions of the source bitmap, or width is <= 0,
     *         or height is <= 0
     */
    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) {
        return createBitmap(source, x, y, width, height, null, false);
    }

width参数注意不应该超过sourceBitmap的getWidth大小,否则会报错。
height参数注意不应该超过sourceBitmap的getHeight大小,若height=sourece.getHeight,则截取整个bitmap的高度,若height=source.getHeight()/2,则截取图片高度的一半,截取到的bitmap只能展示原图的一半。

相关文章

  • ImageView长图截取部分展示

    开发中有需求,类似百度贴吧发图时,图片的备选面板,对于特别长的图,如果不加以处理因图片太大,会出现加载不出来,解决...

  • iOS 截图、截长图、图片拼接

    图片拼接 截图 UIScroolView、UITableView截取长图 截取长图部分我采用的逻辑是,首先获取co...

  • ios截取图片

    最近有朋友遇到要截取图片中间部分展示在imageView上的问题,想着社交应用还是很可能遇到这种情况的就写下来。 ...

  • 04-10 iOS开发截图 全

    截取指定的View: (1).截取制定范围(非长图) (2).截取长图,如TableView,Collection...

  • iOS-评分图片处理(可以小数)

    图片处理。思路是把红色图片截取,然后截取出的图片放到imageView上。imageView的width = 图片...

  • iOS 截屏,截取长图,图片拼接

    1.截取制定范围(非长图) 2.截取长图,如TableView,CollectionView 3.图片拼接 将两张...

  • 安卓开发入门教程-UI控件_ImageView

    什么是ImageView ImageView是用于显示图片的UI控件. 基础样例 1.展示本地图片 效果图 代码 ...

  • android Glide 在fragment出现第一次加载模

    背景: 修改前: 展示代码: 为了达到居中并截取显示,默认Imageview显示都是正常,但是却是在第一次加载的时...

  • ImageView显示长图

    显示长图失败 我们知道,ImageView经常用来显示一些位图,但是不是所有的图片都能够正常显示的,很明显不是的,...

  • react-native Android截屏长图

    之前公司项目需要做截取长图,网上找了半天全是ios的,可能我比较蠢没找到android截取长图的react- ...

网友评论

    本文标题:ImageView长图截取部分展示

    本文链接:https://www.haomeiwen.com/subject/smomtxtx.html