measure Bitmap Sample - Android Graphics

Android examples for Graphics:Bitmap Size

Description

measure Bitmap Sample

Demo Code


//package com.book2s;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class Main {

    public static float measureSample(Bitmap src, int maxSize) {
        return measureSample(src.getWidth(), src.getHeight(), maxSize);
    }// w ww  .  j  a v a  2s  .c o  m

    public static float measureSample(String path, int maxSize) {
        BitmapFactory.Options op = decodeBounds(path);
        return measureSample(op.outWidth, op.outHeight, maxSize);
    }

    public static int measureSample(int width, int height, int maxSize) {
        float scale = measureScale(width, height, maxSize);
        if (scale % 1f > 0) {
            scale++;
        }
        return (int) scale;
    }

    public static BitmapFactory.Options decodeBounds(String path) {
        BitmapFactory.Options opts = createOptions();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, opts);
        return opts;
    }

    public static float measureScale(Bitmap src, int maxSize) {
        return measureScale(src.getWidth(), src.getHeight(), maxSize);
    }

    public static float measureScale(String path, int maxSize) {
        BitmapFactory.Options op = decodeBounds(path);
        return measureScale(op.outWidth, op.outHeight, maxSize);
    }

    public static float measureScale(int width, int height, int maxSize) {
        int longSize = width > height ? width : height;
        if (longSize <= maxSize) {
            return 1;
        }
        return (float) maxSize / longSize;
    }

    @SuppressWarnings("deprecation")
    public static BitmapFactory.Options createOptions() {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inDither = false;
        return options;
    }

    @SuppressWarnings("deprecation")
    public static BitmapFactory.Options createOptions(
            Bitmap.Config colorConfig) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = colorConfig;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inDither = false;
        return options;
    }
}

Related Tutorials