Example usage for android.util TypedValue COMPLEX_UNIT_PX

List of usage examples for android.util TypedValue COMPLEX_UNIT_PX

Introduction

In this page you can find the example usage for android.util TypedValue COMPLEX_UNIT_PX.

Prototype

int COMPLEX_UNIT_PX

To view the source code for android.util TypedValue COMPLEX_UNIT_PX.

Click Source Link

Document

#TYPE_DIMENSION complex unit: Value is raw pixels.

Usage

From source file:Main.java

private static int getLineCount(CharSequence text, TextPaint paint, float size, float width,
        DisplayMetrics displayMetrics) {
    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size, displayMetrics));
    StaticLayout layout = new StaticLayout(text, paint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
            true);//from  ww w .j  a  va2  s.c  om
    return layout.getLineCount();
}

From source file:Main.java

public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
    switch (unit) {
    case TypedValue.COMPLEX_UNIT_PX:
        return value;
    case TypedValue.COMPLEX_UNIT_DIP:
        return value * metrics.density;
    case TypedValue.COMPLEX_UNIT_SP:
        return value * metrics.scaledDensity;
    case TypedValue.COMPLEX_UNIT_PT:
        return value * metrics.xdpi * (1.0f / 72);
    case TypedValue.COMPLEX_UNIT_IN:
        return value * metrics.xdpi;
    case TypedValue.COMPLEX_UNIT_MM:
        return value * metrics.xdpi * (1.0f / 25.4f);
    }//from  w  ww . ja  v a  2s. c  om
    return 0;
}

From source file:Main.java

private static boolean isPxVal(TypedValue val) {
    if (val != null && val.type == TypedValue.TYPE_DIMENSION
            && getComplexUnit(val.data) == TypedValue.COMPLEX_UNIT_PX) {
        return true;
    }/*from   w w  w  .j  av  a 2  s  .  c  o  m*/
    return false;
}

From source file:Main.java

/**
 * Recursive binary search to find the best size for the text.
 *///from  ww w .jav a  2  s  . c  o m
private static float getAutofitTextSize(CharSequence text, TextPaint paint, float targetWidth, int maxLines,
        float low, float high, float precision, DisplayMetrics displayMetrics) {
    float mid = (low + high) / 2.0f;
    int lineCount = 1;
    StaticLayout layout = null;

    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, displayMetrics));

    if (maxLines != 1) {
        layout = new StaticLayout(text, paint, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
                true);
        lineCount = layout.getLineCount();
    }

    if (SPEW)
        Log.d(TAG, "low=" + low + " high=" + high + " mid=" + mid + " target=" + targetWidth + " maxLines="
                + maxLines + " lineCount=" + lineCount);

    if (lineCount > maxLines) {
        // For the case that `text` has more newline characters than `maxLines`.
        if ((high - low) < precision) {
            return low;
        }
        return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
    } else if (lineCount < maxLines) {
        return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
    } else {
        float maxLineWidth = 0;
        if (maxLines == 1) {
            maxLineWidth = paint.measureText(text, 0, text.length());
        } else {
            for (int i = 0; i < lineCount; i++) {
                if (layout.getLineWidth(i) > maxLineWidth) {
                    maxLineWidth = layout.getLineWidth(i);
                }
            }
        }

        if ((high - low) < precision) {
            return low;
        } else if (maxLineWidth > targetWidth) {
            return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
        } else if (maxLineWidth < targetWidth) {
            return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
        } else {
            return mid;
        }
    }
}

From source file:Main.java

public static void setTitle(TextView titleTv, int titleColor, int titleTextSize, int messageColor,
        int messageTextSize, CharSequence title, CharSequence message) {
    titleTv.setMinHeight(titleTextSize * 3);

    if (!TextUtils.isEmpty(title) && TextUtils.isEmpty(message)) {
        titleTv.setTextColor(titleColor);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
        titleTv.getPaint().setFakeBoldText(true);
        titleTv.setText(title);//from   w w  w.  j a va  2  s  . c o m
    } else if (TextUtils.isEmpty(title) && !TextUtils.isEmpty(message)) {
        titleTv.setTextColor(messageColor);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, messageTextSize);
        titleTv.setText(message);
    } else if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(message)) {
        SpannableString titleSs = new SpannableString(title + "\n" + message);
        titleSs.setSpan(new ForegroundColorSpan(titleColor), 0, title.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleSs.setSpan(new AbsoluteSizeSpan(titleTextSize), 0, title.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleSs.setSpan(new StyleSpan(Typeface.BOLD), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        titleSs.setSpan(new ForegroundColorSpan(messageColor), title.length(), titleSs.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleSs.setSpan(new AbsoluteSizeSpan(messageTextSize), title.length(), titleSs.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleTv.setText(titleSs);
        titleTv.setLineSpacing(0.0f, 1.2f);
    } else {
        titleTv.setVisibility(View.GONE);
    }
}

From source file:it.bdsir.life_online.PagerActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pager);
    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pagerTabStrip);
    pagerTabStrip.setTextSize(TypedValue.COMPLEX_UNIT_PX,
            Integer.parseInt(getResources().getString(R.string.size_pager_text)));
    pagerTabStrip.setTextColor(Color.WHITE);
    TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(titleAdapter);
    mViewPager.setCurrentItem(0);//  w  w  w.j  a va2s .c  o m
    //titleAdapter.getItem(position);
    mViewPager.setOffscreenPageLimit(4);
}

From source file:org.homelinux.tapiri.jei.essem.AutoFitTextView.java

private void refitText(String text, int textWidth, int textHeight) {
    if (textWidth > 0) {
        // Subtract a little extra space to account for line height
        int availableWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
        int availableHeight = textHeight - this.getPaddingTop() - this.getPaddingBottom() - 20;
        String largestLine = getLargestLine(text);
        float trySize = maxTextSize;

        this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
        while ((trySize > minTextSize) && ((this.getPaint().measureText(largestLine) > availableWidth)
                || ((this.getLineHeight() * getOriginalLineCount()) > availableHeight))) {
            trySize -= 1;//from  w w  w.  ja  va2 s.c o  m
            if (trySize <= minTextSize) {
                trySize = minTextSize;
                break;
            }
            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
        }
        this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
    }
}

From source file:nl.endran.scrumpoker.fragments.cardselection.CardDisplayFragment.java

public void show(CardSelection cardSelection) {
    if (!showing) {
        showing = true;/*from w ww  . j a  v  a  2  s  .  co m*/

        int stringId = cardSelection.getCardValue().getStringId();
        if (stringId == R.string.coffee) {
            textViewNumber.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                    textViewName.getResources().getDimension(R.dimen.display_mid_text_size));
        } else {
            textViewNumber.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                    textViewName.getResources().getDimension(R.dimen.display_large_text_size));
        }

        textViewNumber.setText(stringId);
        textViewName.setText(cardSelection.getCardValue().toString().replace("_", " "));
        textViewName.setBackgroundColor(cardSelection.getColorDark());
        cardView.setCardBackgroundColor(cardSelection.getColor());

        Animation animation = animationManager.createAnimation(getContext(),
                R.anim.slide_in_bottom_center_with_bounce);
        animation.setAnimationListener(new SetVisibleOnAnimationStartListener(cardView));
        cardView.startAnimation(animation);
    }
}

From source file:com.agenthun.readingroutine.utils.TextSharedElementCallback.java

@Override
public void onSharedElementStart(List<String> sharedElementNames, List<View> sharedElements,
        List<View> sharedElementSnapshots) {
    TextView targetView = getTextView(sharedElements);
    if (targetView == null) {
        Log.w(TAG, "onSharedElementStart: No shared TextView, skipping.");
        return;/*from   w w w  .  jav a 2s .com*/
    }
    mTargetViewTextSize = targetView.getTextSize();
    mTargetViewPaddingStart = targetView.getPaddingStart();
    // Setup the TextView's start values.
    targetView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mInitialTextSize);
    ViewUtils.setPaddingStart(targetView, mInitialPaddingStart);
}

From source file:com.akingyin.librarys.widgets.BrowserLayout.java

private void init(Context context) {
    mContext = context;/* w  w w.ja  va2  s  .co m*/
    setOrientation(VERTICAL);

    mProgressBar = (ProgressBar) LayoutInflater.from(context).inflate(R.layout.progress_horizontal, null);
    mProgressBar.setMax(100);
    mProgressBar.setProgress(0);
    addView(mProgressBar, LayoutParams.MATCH_PARENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
            mBarHeight, getResources().getDisplayMetrics()));

    mWebView = new WebView(context);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    mWebView.getSettings().setBuiltInZoomControls(false);
    mWebView.getSettings().setSupportMultipleWindows(true);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setSupportZoom(false);
    mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setLoadsImagesAutomatically(true);

    LayoutParams lps = new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1);
    addView(mWebView, lps);

    mWebView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if (newProgress == 100) {
                mProgressBar.setVisibility(View.GONE);
            } else {
                mProgressBar.setVisibility(View.VISIBLE);
                mProgressBar.setProgress(newProgress);
            }
        }
    });

    mWebView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mLoadUrl = url;
        }
    });

    mBrowserControllerView = LayoutInflater.from(context).inflate(R.layout.browser_controller, null);
    mGoBackBtn = (ImageButton) mBrowserControllerView.findViewById(R.id.browser_controller_back);
    mGoForwardBtn = (ImageButton) mBrowserControllerView.findViewById(R.id.browser_controller_forward);
    mGoBrowserBtn = (ImageButton) mBrowserControllerView.findViewById(R.id.browser_controller_go);
    mRefreshBtn = (ImageButton) mBrowserControllerView.findViewById(R.id.browser_controller_refresh);

    mGoBackBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (canGoBack()) {
                goBack();
            }
        }
    });

    mGoForwardBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (canGoForward()) {
                goForward();
            }
        }
    });

    mRefreshBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            loadUrl(mLoadUrl);
        }
    });

    mGoBrowserBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!TextUtils.isEmpty(mLoadUrl)) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(mLoadUrl));
                mContext.startActivity(intent);
            }
        }
    });

    addView(mBrowserControllerView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}