Android Open Source - mclib Screen Helper






From Project

Back to project page mclib.

License

The source code is released under:

Apache License, Version 2.0 FoundationProjectsPeopleGet InvolvedDownloadSupport ApacheHome ? Licenses Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITION...

If you think the Android project mclib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
   Copyright 2012 Mikhail Chabanov//  w ww. j  a  v  a 2s  .  c  o  m

   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 mc.lib.screen;

import android.content.Context;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class ScreenHelper
{
    public static boolean isLarge(Context context)
    {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    public static boolean isNormal(Context context)
    {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL;
    }

    public static boolean isSmall(Context context)
    {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL;
    }

    public static boolean isPortrait(Context context)
    {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }

    public static boolean isLandscape(Context context)
    {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    }

    public static boolean isSqare(Context context)
    {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_SQUARE;
    }

    public static int getWidth(Context context)
    {
        return getWidthHeight(context)[0];
    }

    public static int getHeight(Context context)
    {
        return getWidthHeight(context)[1];
    }

    /**
     * Get screen height, considering current orientation. If screen in landscape his current height equals width and current
     * width equals height
     * 
     * 
     * @param context
     * @return screen height
     */
    public static int getCurrentHeight(Context context)
    {
        if(isPortrait(context))
        {
            return getWidthHeight(context)[1];
        }
        else
        {
            return getWidthHeight(context)[0];
        }
    }

    /**
     * Get screen width, considering current orientation. If screen in landscape his current height equals width and current
     * width equals height
     * 
     * 
     * @param context
     * @return screen width
     */
    public static int getCurrentWidth(Context context)
    {
        if(isPortrait(context))
        {
            return getWidthHeight(context)[0];
        }
        else
        {
            return getWidthHeight(context)[1];
        }
    }

    public static int[] getWidthHeight(Context context)
    {
        int[] wh = new int[2];
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        Display d = wm.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        d.getMetrics(dm);
        if(dm.widthPixels < dm.heightPixels)
        {
            wh[0] = dm.widthPixels;
            wh[1] = dm.heightPixels;
        }
        else
        {
            wh[1] = dm.widthPixels;
            wh[0] = dm.heightPixels;
        }
        return wh;
    }

    /**
     * Helper method to get DisplayMetrics.
     * 
     * @param context
     * @return DisplayMetrics
     */
    public static DisplayMetrics getDisplayMetrics(Context context)
    {
        Display d = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayMetrics res = new DisplayMetrics();
        d.getMetrics(res);
        return res;
    }

    public static boolean isLowDensity(Context context)
    {
        return getDisplayMetrics(context).densityDpi == DisplayMetrics.DENSITY_LOW;
    }

    public static boolean isMediumDensity(Context context)
    {
        return getDisplayMetrics(context).densityDpi == DisplayMetrics.DENSITY_MEDIUM;
    }

    public static boolean isHighDensity(Context context)
    {
        return getDisplayMetrics(context).densityDpi == DisplayMetrics.DENSITY_HIGH;
    }

    public static float getDensityCoefficient(Context context)
    {
        int d = getDisplayMetrics(context).densityDpi;
        switch(d)
        {
        case DisplayMetrics.DENSITY_LOW:
            return 0.75f;
        case DisplayMetrics.DENSITY_MEDIUM:
            return 1;
        case DisplayMetrics.DENSITY_HIGH:
            return 1.5f;
        default:
            return 2;
        }
    }

    public static int pxToDip(Context context, int px)
    {
        return (int)(px * getDensityCoefficient(context));
    }
}




Java Source Code List

mc.lib.archives.ArchivesHelper.java
mc.lib.assets.AssetsHelper.java
mc.lib.audio.AudioSource.java
mc.lib.audio.IAudioPlayer.java
mc.lib.audio.MediaAudioPlayer.java
mc.lib.files.FileHelper.java
mc.lib.files.FileReadingHelper.java
mc.lib.files.PermissionHelper.java
mc.lib.identity.IdentityHelper.java
mc.lib.image.BitmapTools.java
mc.lib.image.ImageHelper.java
mc.lib.image.InputStreamFullSkip.java
mc.lib.interfaces.OnCompleteListener.java
mc.lib.interfaces.OnProgressListener.java
mc.lib.interfaces.OnStartListener.java
mc.lib.io.KeyboardHelper.java
mc.lib.io.ToastHelper.java
mc.lib.location.LocationHelper.java
mc.lib.location.OnLocationChangeListener.java
mc.lib.log.Log.java
mc.lib.network.AsyncFileDownloader.java
mc.lib.network.AsyncNetworkHelper.java
mc.lib.network.EmailHelper.java
mc.lib.network.NetworkHelper.java
mc.lib.regexp.PopularRegexp.java
mc.lib.regexp.Validator.java
mc.lib.screen.ScreenHelper.java
mc.lib.stream.AdvancedStreamHelper.java
mc.lib.stream.StreamHelper.java
mc.lib.video.BasicVideoPlayer.java
mc.lib.video.IVideoPlayer.java
mc.lib.video.VideoViewPlayer.java