Example usage for android.content.res Resources getDisplayMetrics

List of usage examples for android.content.res Resources getDisplayMetrics

Introduction

In this page you can find the example usage for android.content.res Resources getDisplayMetrics.

Prototype

public DisplayMetrics getDisplayMetrics() 

Source Link

Document

Return the current display metrics that are in effect for this resource object.

Usage

From source file:codepath.watsiapp.utils.Util.java

public static float getPixels(Activity activity, float dp) {
    Resources r = activity.getResources();
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
    return px;//from  ww  w  .  j  a v  a  2s. c  o  m
}

From source file:com.gdgdevfest.android.apps.devfestbcn.ui.PlusStreamRowViewBinder.java

public static ImageLoader getPlusStreamImageLoader(FragmentActivity activity, Resources resources) {
    DisplayMetrics metrics = resources.getDisplayMetrics();
    int largestWidth = metrics.widthPixels > metrics.heightPixels ? metrics.widthPixels : metrics.heightPixels;

    // Create list of placeholder drawables (this ImageLoader requires two different
    // placeholder images).
    ArrayList<Drawable> placeHolderDrawables = new ArrayList<Drawable>(2);
    placeHolderDrawables.add(PLACEHOLDER_USER_IMAGE, resources.getDrawable(drawable.person_image_empty));
    placeHolderDrawables.add(PLACEHOLDER_MEDIA_IMAGE,
            new ColorDrawable(resources.getColor(R.color.plus_empty_image_background_color)));

    // Create ImageLoader instance
    return new ImageLoader(activity, placeHolderDrawables).setMaxImageSize(largestWidth);
}

From source file:com.jarklee.materialdatetimepicker.Utils.java

public static String getStringFromLocale(@NonNull Context context, @StringRes int strRes, Locale locale) {
    if (locale == null) {
        return context.getString(strRes);
    }/*from  w ww  .  j  a v a  2  s.  co m*/
    Resources standardResources = context.getResources();
    AssetManager assets = standardResources.getAssets();
    DisplayMetrics metrics = standardResources.getDisplayMetrics();
    Configuration config = new Configuration(standardResources.getConfiguration());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(locale);
    } else {
        config.locale = locale;
    }
    Resources defaultResources = new Resources(assets, metrics, config);
    try {
        return defaultResources.getString(strRes);
    } catch (Exception e) {
        return context.getString(strRes);
    }
}

From source file:Main.java

/**
 * Setting {@link Locale} for {@link Context}.
 *//*from w ww  . ja  v a2s  . c  om*/
public static Context applyLanguageForContext(Context context, Locale locale) {
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocale(locale);
        return context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        return context;
    }
}

From source file:eu.faircode.netguard.ServiceJob.java

private static void submit(Rule rule, PersistableBundle bundle, Context context) {
    PackageManager pm = context.getPackageManager();
    JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    // Get english application label
    String label = null;//w  w w  .  j  a  v a 2s . co  m
    try {
        Configuration config = new Configuration();
        config.setLocale(new Locale("en"));
        Resources res = pm.getResourcesForApplication(rule.info.packageName);
        res.updateConfiguration(config, res.getDisplayMetrics());
        label = res.getString(rule.info.applicationInfo.labelRes);
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        CharSequence cs = rule.info.applicationInfo.loadLabel(pm);
        if (cs != null)
            label = cs.toString();
    }

    // Add application data
    bundle.putInt("uid", rule.info.applicationInfo.uid);
    bundle.putString("package", rule.info.packageName);
    bundle.putInt("version_code", rule.info.versionCode);
    bundle.putString("version_name", rule.info.versionName);
    bundle.putString("label", label);
    bundle.putInt("system", rule.system ? 1 : 0);
    try {
        bundle.putString("installer", pm.getInstallerPackageName(rule.info.packageName));
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        bundle.putString("installer", null);
    }

    // Cancel overlapping jobs
    for (JobInfo pending : scheduler.getAllPendingJobs()) {
        String type = pending.getExtras().getString("type");
        if (type != null && type.equals(bundle.getString("type"))) {
            if (type.equals("rule")) {
                int uid = pending.getExtras().getInt("uid");
                if (uid == bundle.getInt("uid")) {
                    Log.i(TAG, "Canceling id=" + pending.getId());
                    scheduler.cancel(pending.getId());
                }
            } else if (type.equals("host")) {
                int uid = pending.getExtras().getInt("uid");
                int version = pending.getExtras().getInt("version");
                int protocol = pending.getExtras().getInt("protocol");
                String daddr = pending.getExtras().getString("daddr");
                int dport = pending.getExtras().getInt("dport");
                if (uid == bundle.getInt("uid") && version == bundle.getInt("version")
                        && protocol == bundle.getInt("protocol") && daddr != null
                        && daddr.equals(bundle.getString("daddr")) && dport == bundle.getInt("dport")) {
                    Log.i(TAG, "Canceling id=" + pending.getId());
                    scheduler.cancel(pending.getId());
                }
            }
        }
    }

    // Schedule job
    ComponentName serviceName = new ComponentName(context, ServiceJob.class);
    JobInfo job = new JobInfo.Builder(++id, serviceName).setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setMinimumLatency(Util.isDebuggable(context) ? 10 * 1000 : 60 * 1000).setExtras(bundle)
            .setPersisted(true).build();
    if (scheduler.schedule(job) == JobScheduler.RESULT_SUCCESS)
        Log.i(TAG, "Scheduled job=" + job.getId() + " success");
    else
        Log.e(TAG, "Scheduled job=" + job.getId() + " failed");
}

From source file:com.nextgis.maplibui.util.ControlHelper.java

public static int dpToPx(int dp, Resources resources) {
    DisplayMetrics dm = resources.getDisplayMetrics();
    return Math.round(dp * (dm.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

From source file:com.metinkale.prayerapp.vakit.WidgetService.java

private static Bitmap getIconFromMinutes(long left) {
    String text = left + "";
    Resources r = App.getContext().getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, r.getDisplayMetrics());
    Bitmap b = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_4444);
    Canvas c = new Canvas(b);
    Paint paint = new Paint();
    final float testTextSize = 48f;
    paint.setTextSize(testTextSize);//from   w w  w .  j a  v a  2  s . c o m
    Rect bounds = new Rect();
    paint.getTextBounds(text.length() == 1 ? "0" + text : text, 0, text.length() == 1 ? 2 : text.length(),
            bounds);
    float desiredTextSize = testTextSize * (px * 0.9f) / bounds.width();
    paint.setTextSize(desiredTextSize);
    paint.setColor(0xFFFFFFFF);
    paint.setTextAlign(Paint.Align.CENTER);
    int yPos = (int) ((c.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
    c.drawText(text, px / 2, yPos, paint);
    c.drawText(text, px / 2, yPos, paint);
    return b;
}

From source file:com.agenmate.lollipop.util.ViewUtils.java

/**
 * Determine if the navigation bar will be on the bottom of the screen, based on logic in
 * PhoneWindowManager./* w  w  w .  j  a  v a2s  .  c o m*/
 */
public static boolean isNavBarOnBottom(@NonNull Context context) {
    final Resources res = context.getResources();
    final Configuration cfg = context.getResources().getConfiguration();
    final DisplayMetrics dm = res.getDisplayMetrics();
    boolean canMove = (dm.widthPixels != dm.heightPixels && cfg.smallestScreenWidthDp < 600);
    return (!canMove || dm.widthPixels < dm.heightPixels);
}

From source file:kr.ac.ajou.ajouinoclient.util.ImageDownloader.java

/**
 * get dpi scaling factor for each devices
 *//*from  w ww  .  j a  v a  2 s.c  o  m*/
public static float getImageFactor(Resources r) {
    DisplayMetrics metrics = r.getDisplayMetrics();
    float multiplier = metrics.density / 3f;
    return multiplier;
}

From source file:Main.java

public static DisplayMetrics getDisplayMetrics(Context context) {
    Resources mResources;
    if (context == null) {
        mResources = Resources.getSystem();

    } else {/*from www.j a v  a2 s. c o  m*/
        mResources = context.getResources();
    }
    //DisplayMetrics{density=1.5, width=480, height=854, scaledDensity=1.5, xdpi=160.421, ydpi=159.497}
    //DisplayMetrics{density=2.0, width=720, height=1280, scaledDensity=2.0, xdpi=160.42105, ydpi=160.15764}
    DisplayMetrics mDisplayMetrics = mResources.getDisplayMetrics();
    return mDisplayMetrics;
}