Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.util.DisplayMetrics;
import android.util.TypedValue;

public class Main {
    /**
     * Create bitmap image from resource
     *
     * @param context
     * @param iconName
     * @param widthDip
     * @param heightDip
     * @return
     */
    public static Bitmap resizeMapIcons(Context context, String iconName, int widthDip, int heightDip) {
        int widthPx = (int) dipToPixels(context, widthDip);
        int heightPx = (int) dipToPixels(context, heightDip);

        Bitmap imageBitmap = BitmapFactory.decodeResource(context.getResources(),
                context.getResources().getIdentifier(iconName, "drawable", context.getPackageName()));
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap, widthPx, heightPx, false);
        return resizedBitmap;
    }

    /**
     * Convert density pixel value to pixel for accurate scaling on different
     * screen densities
     * @param context
     * @param dipValue
     * @return
     */
    public static float dipToPixels(Context context, float dipValue) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics);
    }
}