Android Open Source - android-placeholder Metrics Converter






From Project

Back to project page android-placeholder.

License

The source code is released under:

Apache License

If you think the Android project android-placeholder 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

package se.lightside.placeholder.util;
/*from ww  w.  j  ava 2 s  . c om*/
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;

import java.util.Locale;

/**
 * Converter to turn a pixel value into a typed dimension for display
 */
public class MetricsConverter {

    public static final String UNIT_DP   = "dp";
    public static final String UNIT_PX   = "px";

    private DisplayMetrics mDm;
    private String mConvertUnit;


    public MetricsConverter(@NonNull Context ctx, String convertUnit) {
        mDm = ctx.getResources().getDisplayMetrics();
        mConvertUnit = convertUnit;
    }


    /**
     * Returns a string representation of the provided pixel dimensions converted with this MetricsConverter's
     * convertUnit
     *
     * @param widthPx width in pixels
     * @param heightPx height in pixels
     * @return a String of the format "12dp x 42dp" or whichever unit you're using
     */
    public String asString(int widthPx, int heightPx) {
        return String.format(Locale.US, "%s x %s", asString(widthPx), asString(heightPx));
    }

    public String asString(int pixels) {
        switch (mConvertUnit) {
            case UNIT_DP:
                return String.format(Locale.US, "%ddp", (int) (pixels / mDm.density));
            default:
                return ""+pixels+"px";
        }
    }
}




Java Source Code List

se.lightside.placeholder.ApplicationTest.java
se.lightside.placeholder.PlaceholderBuilder.java
se.lightside.placeholder.graphics.drawable.AbstractPlaceholderDrawable.java
se.lightside.placeholder.graphics.drawable.CrossDrawable.java
se.lightside.placeholder.graphics.drawable.DimensionsDrawable.java
se.lightside.placeholder.graphics.drawable.GridDrawable.java
se.lightside.placeholder.sample.DemoFragment.java
se.lightside.placeholder.sample.DemoHostActivity.java
se.lightside.placeholder.text.method.BlockTransformationMethod.java
se.lightside.placeholder.util.MetricsConverter.java
se.lightside.placeholder.util.ViewCompat.java
se.lightside.placeholder.view.RatioLineOverlay.java
se.lightside.placeholder.view.RatioLine.java
se.lightside.placeholder.widget.BlockedOutTextView.java
se.lightside.placeholder.widget.PlaceholderView.java