/*
 * Copyright (C) 2015, 程序亦非猿
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yifeiyuan.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.annotation.NonNull;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by 程序亦非猿 on 15/12/26.
 *
 * 存放跟Bitmap相关的util
 */
public class BitmapUtil {

    /**
     * 将Bitmap保存到file中
     *
     * @param file    目标文件
     * @param bitmap  目标Bitmap
     * @param format  保存的格式,具体可看{@link android.graphics.Bitmap.CompressFormat}
     * @param quality 0-100
     * @return 保存是否成功
     */
    public static boolean saveBitmapToFile(@NonNull File file, @NonNull Bitmap bitmap, @NonNull Bitmap.CompressFormat format, int quality) {

        OutputStream os = null;
        boolean isSuccess = false;
        try {
            os = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(format, quality, os);
            os.flush();
            isSuccess = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.closeSilently(os);
        }
        return isSuccess;
    }

    /**
     * 获取bitmap的大小
     *
     * @param bitmap
     * @return bitmap的大小
     */
    public static int getBitmapSize(@NonNull Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    //API 19
            return bitmap.getAllocationByteCount();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
            return bitmap.getByteCount();
        }
        return bitmap.getRowBytes() * bitmap.getHeight();                //earlier version
    }

    /**
     * 计算inSampleSize,该方法源自Android官网
     *
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
}

@@ -705,16 +705,207 @@
 oid.
-os.Build
+graphics.Canvas;%0Aimport android.graphics.ColorFilter;%0Aimport android.graphics.LightingColorFilter;%0Aimport android.graphics.Paint;%0Aimport android.os.Build;%0Aimport android.support.annotation.IntegerRes
 ;%0Aim
@@ -3569,10 +3569,632 @@
 ;%0A    %7D%0A
+%0A%0A%0A    /**%0A     * %E7%BB%99bitmap%E7%9D%80%E8%89%B2%0A     * @param sourceBitmap dest%0A     * @param color  %E9%A2%9C%E8%89%B2%E8%B5%84%E6%BA%90id%0A     * @return%0A     */%0A    public static Bitmap changeBitmapColor(@NonNull Bitmap sourceBitmap, @IntegerRes int color) %7B%0A%0A        Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0,%0A                sourceBitmap.getWidth() - 1, sourceBitmap.getHeight() - 1);%0A        Paint p = new Paint();%0A        ColorFilter filter = new LightingColorFilter(color, 1);%0A        p.setColorFilter(filter);%0A%0A        Canvas canvas = new Canvas(resultBitmap);%0A        canvas.drawBitmap(resultBitmap, 0, 0, p);%0A        return resultBitmap;%0A    %7D%0A%0A
 %7D%0A

add changeBitmapColor