Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;

import android.util.Log;

public class Main {
    public static final float STANDARD_DENSITYDPI = 240f;
    private static final int APP_ICON_WIDTH = 80;
    private static final int APP_ICON_HEIGHT = 80;

    public static BitmapDrawable loadAppIcon(String iconPath, Context context) {
        try {
            Bitmap bitmap = null;
            Bitmap newBitmap = null;
            if (iconPath != null && !"".equals(iconPath)) {
                bitmap = BitmapFactory.decodeFile(iconPath);
            }

            if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
                Log.e("RecommAppsUtils", iconPath + " is not exist");
                return null;
            }
            int densityDpi = context.getResources().getDisplayMetrics().densityDpi;
            float scale = densityDpi / STANDARD_DENSITYDPI;
            newBitmap = zoomBitmap(bitmap, (int) (APP_ICON_WIDTH * scale), (int) (APP_ICON_HEIGHT * scale));
            // Log.d("RecommAppsUtils", "densityDpi value : " + densityDpi);
            return new BitmapDrawable(context.getResources(), newBitmap);
        } catch (OutOfMemoryError error) {
            error.printStackTrace();
        }
        return null;
    }

    public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidht = ((float) w) / width;
        float scaleHeight = ((float) h) / height;
        matrix.postScale(scaleWidht, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        return newbmp;
    }
}