Android Open Source - RealTextView Size Utils






From Project

Back to project page RealTextView.

License

The source code is released under:

Apache License

If you think the Android project RealTextView 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 com.hardsoftstudio.real.textview.utils;
//ww w.  ja  va 2s .com
import android.content.Context;
import android.graphics.Point;

import com.hardsoftstudio.real.textview.exceptions.RealHtmlTextViewException;

/**
 * Created by Marcel on 07/10/2014.
 */
public class SizeUtils {

    public static float getDensity(Context context){
        float scale = context.getResources().getDisplayMetrics().density;
        return scale;
    }

    public static int convertDiptoPix(Context c, int dip){
        if (dip==0)
            return 0;
        float scale = getDensity(c);
        return (int) (dip * scale + 0.5f);
    }

    public static int convertPixtoDip(Context c, int pixel){
        if (pixel==0)
            return 0;
        float scale = getDensity(c);
        return (int)((pixel - 0.5f)/scale);
    }

    public static Point getSizeFromString(String source) throws RealHtmlTextViewException, NumberFormatException {

        if (source==null || source.isEmpty())
            return new Point(0,0);

        int index = source.lastIndexOf("width");
        if (index<0)
            throw new RealHtmlTextViewException("No width tag found for remote image");

        String sizeTag = source.substring(index,source.length());
        int end = sizeTag.indexOf("&");
        if (end<0)
            throw new RealHtmlTextViewException("Missing & in size tag");

        String width = sizeTag.substring(index+6,end);
        String height = sizeTag.substring(end+8,sizeTag.length());

        return new Point(Integer.parseInt(width),Integer.parseInt(height));
    }

    public static String removeSizeTag(String source) {
        return source.substring(0,source.lastIndexOf("width")-1);
    }
}




Java Source Code List

com.hardsoftstudio.real.textview.exceptions.RealHtmlTextViewException.java
com.hardsoftstudio.real.textview.utils.AutofitHelper.java
com.hardsoftstudio.real.textview.utils.FontManager.java
com.hardsoftstudio.real.textview.utils.HtmlTagHandler.java
com.hardsoftstudio.real.textview.utils.LocalImageGetter.java
com.hardsoftstudio.real.textview.utils.RealUrl.java
com.hardsoftstudio.real.textview.utils.SizeUtils.java
com.hardsoftstudio.real.textview.utils.UrlImageGetter.java
com.hardsoftstudio.real.textview.views.BaseTextView.java
com.hardsoftstudio.real.textview.views.RealButton.java
com.hardsoftstudio.real.textview.views.RealCheckBox.java
com.hardsoftstudio.real.textview.views.RealEditText.java
com.hardsoftstudio.real.textview.views.RealTextView.java
org.hardsoftstudio.real.example.ApplicationTest.java
org.hardsoftstudio.real.example.BaseActivity.java
org.hardsoftstudio.real.example.ExampleMainActivity.java
org.hardsoftstudio.real.example.HtmlExampleActivity.java
org.hardsoftstudio.real.example.MainActivity.java
org.hardsoftstudio.real.textview.ApplicationTest.java