ImageView長圖擷取部分展示

weixin_34041003發表於2017-05-08

開發中有需求,類似百度貼吧發圖時,圖片的備選皮膚,對於特別長的圖,如果不加以處理因圖片太大,會出現載入不出來,解決策略是對於長圖先根據寬度縮放比,等比縮放到期望尺寸,注意這樣等比縮放,是防止圖片變形。
縮放好後根據需要高度擷取需要的高度圖片。程式碼如下

  /**
     * 根據期望寬高度擷取圖片
     *
     * @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只能展示原圖的一半。

相關文章