/* Copyright (c) 2009-2011 Matthias Kaeppler
*
* 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 gws.grottworkshop.gwsholmeswatson;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
/**
* The Class GWSScreens, borrowed from Ignition(Kaeppler).
* Modifications by:
* @author fredgrott
*/
public class GWSScreens {
public static final int SCREEN_DENSITY_LOW = 120;
public static final int SCREEN_DENSITY_MEDIUM = 160;
public static final int SCREEN_DENSITY_TV = 213;
public static final int SCREEN_DENSITY_HIGH = 240;
public static final int SCREEN_DENSITY_XHIGH = 320;
private static int screenDensity = -1;
public static int dipToPx(Context context, int dip) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (int) (dip * displayMetrics.density + 0.5f);
}
@SuppressWarnings("deprecation")
public static Drawable scaleDrawable(Context context, int drawableResourceId, int width,
int height) {
Bitmap sourceBitmap = BitmapFactory.decodeResource(context.getResources(),
drawableResourceId);
return new BitmapDrawable(Bitmap.createScaledBitmap(sourceBitmap, width, height, true));
}
/**
* Provides a save (i.e. backwards compatible) of accessing the device's screen density.
*
* @param context
* the current context
* @return the screen density in dots-per-inch
*/
public static int getScreenDensity(Context context) {
if (screenDensity == -1) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
screenDensity = displayMetrics.densityDpi;
}
return screenDensity;
}
}