Java Code Examples for android.util.TypedValue#COMPLEX_UNIT_PX

The following examples show how to use android.util.TypedValue#COMPLEX_UNIT_PX . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DimensionParser.java    From android-cassowary-layout with Apache License 2.0 6 votes vote down vote up
private static int getUnitFromString(String unitString) {

        int unit = TypedValue.COMPLEX_UNIT_DIP;
        if ("px".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_PX;
        } else if ("dp".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_DIP;
        } else if ("sp".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_SP;
        } else if ("pt".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_PT;
        } else if ("in".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_IN;
        } else if ("mm".equals(unitString)) {
            unit = TypedValue.COMPLEX_UNIT_MM;
        }
        return unit;
    }
 
Example 2
Source File: MixtureTextView.java    From MixtureTextView with Apache License 2.0 6 votes vote down vote up
public void setTextSize(int unit, int size)
{
    switch (unit)
    {
        case TypedValue.COMPLEX_UNIT_PX:
            mTextSize = size;
            break;
        case TypedValue.COMPLEX_UNIT_DIP:
            mTextSize = dp2px(size);
            break;
        case TypedValue.COMPLEX_UNIT_SP:
            mTextSize = sp2px(size);
            break;
    }
    mTextPaint.setTextSize(mTextSize);
    requestLayout();
    invalidate();
}
 
Example 3
Source File: SizeUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * Converts an unpacked complex data value holding a dimension to its final floating
 * point value. The two parameters <var>unit</var> and <var>value</var>
 * are as in {@link TypedValue#TYPE_DIMENSION}.
 *
 * @param value The value to apply the unit to.
 * @param unit  The unit to convert from.
 * @return The complex floating point value multiplied by the appropriate
 * metrics depending on its unit.
 */
public static float applyDimension(final float value, final int unit) {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    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);
    }
    return 0;
}
 
Example 4
Source File: SizeUtils.java    From Matisse with Apache License 2.0 6 votes vote down vote up
/**
 * 各种单位转换
 * <p>该方法存在于TypedValue</p>
 * @param unit    单位
 * @param value   值
 * @param metrics DisplayMetrics
 * @return 转换结果
 */
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);
    }
    return 0;
}
 
Example 5
Source File: RxTextAutoZoom.java    From RxTools-master with Apache License 2.0 6 votes vote down vote up
private void adjustTextSize() {
    if (!_initiallized)
        return;
    final int startSize = Math.round(_minTextSize);
    final int heightLimit = getMeasuredHeight()
            - getCompoundPaddingBottom() - getCompoundPaddingTop();
    _widthLimit = getMeasuredWidth() - getCompoundPaddingLeft()
            - getCompoundPaddingRight();
    if (_widthLimit <= 0)
        return;
    _availableSpaceRect.right = _widthLimit;
    _availableSpaceRect.bottom = heightLimit;
    super.setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            efficientTextSizeSearch(startSize, (int) _maxTextSize,
                    _sizeTester, _availableSpaceRect));
}
 
Example 6
Source File: TwitterLoginButton.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setupButton() {
    final Resources res = getResources();
    super.setCompoundDrawablesWithIntrinsicBounds(
            res.getDrawable(R.drawable.tw__ic_logo_default), null, null, null);
    super.setCompoundDrawablePadding(
            res.getDimensionPixelSize(R.dimen.tw__login_btn_drawable_padding));
    super.setText(R.string.tw__login_btn_txt);
    super.setTextColor(res.getColor(R.color.tw__solid_white));
    super.setTextSize(TypedValue.COMPLEX_UNIT_PX,
            res.getDimensionPixelSize(R.dimen.tw__login_btn_text_size));
    super.setTypeface(Typeface.DEFAULT_BOLD);
    super.setPadding(res.getDimensionPixelSize(R.dimen.tw__login_btn_left_padding), 0,
            res.getDimensionPixelSize(R.dimen.tw__login_btn_right_padding), 0);
    super.setBackgroundResource(R.drawable.tw__login_btn);
    super.setOnClickListener(new LoginClickListener());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        super.setAllCaps(false);
    }
}
 
Example 7
Source File: AutoResizeTextView.java    From ADP with MIT License 6 votes vote down vote up
private void adjustTextSize(String string) {
    if (!mInitiallized) {
        return;
    }
    int startSize = (int) mMinTextSize;
    int heightLimit = getMeasuredHeight() - getCompoundPaddingBottom()
            - getCompoundPaddingTop();
    mWidthLimit = getMeasuredWidth() - getCompoundPaddingLeft()
            - getCompoundPaddingRight();
    mAvailableSpaceRect.right = mWidthLimit;
    mAvailableSpaceRect.bottom = heightLimit;
    super.setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            efficientTextSizeSearch(startSize, (int) mMaxTextSize,
                    mSizeTester, mAvailableSpaceRect));
}
 
Example 8
Source File: DimensionResource.java    From external-resources with Apache License 2.0 5 votes vote down vote up
protected static int getTypeFromString(@NonNull String str) {
  String[] split = str.trim().split("[0-9]");

  if (split.length > 0) {
    final String typeStr = split[split.length - 1];

    switch (typeStr) {
      case TYPE_DP:
      case TYPE_DIP:
        return TypedValue.COMPLEX_UNIT_DIP;
      case TYPE_SP:
        return TypedValue.COMPLEX_UNIT_SP;
      case TYPE_PT:
        return TypedValue.COMPLEX_UNIT_PT;
      case TYPE_IN:
        return TypedValue.COMPLEX_UNIT_IN;
      case TYPE_MM:
        return TypedValue.COMPLEX_UNIT_MM;
      case TYPE_PX:
        return TypedValue.COMPLEX_UNIT_PX;
      default:
        throw new IllegalArgumentException(typeStr + " is not a valid type dimension format.");
    }
  } else {
    return TypedValue.COMPLEX_UNIT_PX;
  }
}
 
Example 9
Source File: AutoResizeTextView.java    From oneHookLibraryAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Reset the text to the original size
 */
public void resetTextSize() {
    if (mTextSize > 0) {
        super.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
        mMaxTextSize = mTextSize;
    }
}
 
Example 10
Source File: DimenUtils.java    From AndroidAutoLayout with Apache License 2.0 5 votes vote down vote up
public static boolean isPxVal(TypedValue val)
{
    if (val != null && val.type == TypedValue.TYPE_DIMENSION &&
            getComplexUnit(val.data) == TypedValue.COMPLEX_UNIT_PX)
    {
        return true;
    }
    return false;
}
 
Example 11
Source File: PixelValue.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public final float getPxValue() {
    switch (unit) {
        case EM:
            return this.value / 16.f;
        case TypedValue.COMPLEX_UNIT_PX:
            return this.value;
        case TypedValue.COMPLEX_UNIT_SP:
            return ParametersUtils.spToPx(this.value);
        case TypedValue.COMPLEX_UNIT_DIP:
            return ParametersUtils.dpToPx(this.value);
        default:
            return value;
    }
}
 
Example 12
Source File: PixelValue.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public PixelValue(float value, @PixelUnit int unit) {
    if (unit == EM) {
        this.value = value * 16;
        this.unit = TypedValue.COMPLEX_UNIT_PX;
    } else {
        this.value = value;
        this.unit = unit;
    }
}
 
Example 13
Source File: Button.java    From Carbon with Apache License 2.0 5 votes vote down vote up
private void adjustTextSize() {
    if (autoSizeText == AutoSizeTextMode.None || minTextSize <= 0 || maxTextSize <= 0 || getMeasuredWidth() == 0 || getMeasuredHeight() == 0)
        return;
    if (autoSizeStepPresets == null)
        initAutoSize();
    availableSpaceRect.right = getMeasuredWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
    availableSpaceRect.bottom = getMeasuredHeight() - getCompoundPaddingBottom() - getCompoundPaddingTop();
    super.setTextSize(TypedValue.COMPLEX_UNIT_PX, binarySearch(availableSpaceRect));
}
 
Example 14
Source File: GestureTextView.java    From GestureViews with Apache License 2.0 5 votes vote down vote up
protected void applyState(State state) {
    float size = origSize * state.getZoom();
    float maxSize = origSize * controller.getStateController().getMaxZoom(state);
    size = Math.max(origSize, Math.min(size, maxSize));

    // Bigger text size steps for smoother scaling
    size = Math.round(size);

    if (!State.equals(this.size, size)) {
        this.size = size;
        super.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }
}
 
Example 15
Source File: XmlToClassAttribHandler.java    From RxAndroidBootstrap with Apache License 2.0 5 votes vote down vote up
public int gettextSizeUnit() {

        String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT_SIZE );

        if (value==null)
            return TypedValue.COMPLEX_UNIT_SP;

        try{
            String type=value.substring(value.length()-2, value.length());

            if (type.equals("dp"))
                return TypedValue.COMPLEX_UNIT_DIP;
            else if (type.equals("sp"))
                return TypedValue.COMPLEX_UNIT_SP;
            else if (type.equals("pt"))
                return TypedValue.COMPLEX_UNIT_PT;
            else if (type.equals("mm"))
                return TypedValue.COMPLEX_UNIT_MM;
            else if (type.equals("in"))
                return TypedValue.COMPLEX_UNIT_IN;
            else if (type.equals("px"))
                return TypedValue.COMPLEX_UNIT_PX;
        }
        catch(Exception e){
            return -1;
        }

        return -1;
    }
 
Example 16
Source File: EditText.java    From Carbon with Apache License 2.0 5 votes vote down vote up
private void adjustTextSize() {
    if (autoSizeText == AutoSizeTextMode.None || minTextSize <= 0 || maxTextSize <= 0 || getMeasuredWidth() == 0 || getMeasuredHeight() == 0)
        return;
    if (autoSizeStepPresets == null)
        initAutoSize();
    availableSpaceRect.right = getMeasuredWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
    availableSpaceRect.bottom = getMeasuredHeight() - getCompoundPaddingBottom() - getCompoundPaddingTop();
    super.setTextSize(TypedValue.COMPLEX_UNIT_PX, binarySearch(availableSpaceRect));
}
 
Example 17
Source File: FontFitTextView.java    From Twire with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reset the text to the original size
 */
public void resetTextSize() {
    if (mTextSize > 0) {
        super.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
        mMaxTextSize = mTextSize;
    }
}
 
Example 18
Source File: SizeAdjustingTextView.java    From MaterialCalendar with Apache License 2.0 4 votes vote down vote up
public void resetTextSize() {
    if (mTextSize > 0) {
        super.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
        mMaxTextSize = mTextSize;
    }
}
 
Example 19
Source File: AutoFitTextView.java    From android_gisapp with GNU General Public License v3.0 4 votes vote down vote up
private void superSetTextSize(int startSize)
{
    super.setTextSize(TypedValue.COMPLEX_UNIT_PX,efficientTextSizeSearch(startSize,(int)_maxTextSize,_sizeTester,_availableSpaceRect));
}
 
Example 20
Source File: EmojiTextView.java    From deltachat-android with GNU General Public License v3.0 4 votes vote down vote up
@Override public void setText(@Nullable CharSequence text, BufferType type) {
  EmojiProvider             provider   = EmojiProvider.getInstance(getContext());
  EmojiParser.CandidateList candidates = provider.getCandidates(text);

  if (scaleEmojis && candidates != null && candidates.allEmojis) {
    int   emojis = candidates.size();
    float scale  = 1.0f;

    if (emojis <= 8) scale += 0.25f;
    if (emojis <= 6) scale += 0.25f;
    if (emojis <= 4) scale += 0.25f;
    if (emojis <= 2) scale += 0.25f;

    super.setTextSize(TypedValue.COMPLEX_UNIT_PX, originalFontSize * scale);
  } else if (scaleEmojis) {
    super.setTextSize(TypedValue.COMPLEX_UNIT_PX, originalFontSize);
  }

  if (unchanged(text, overflowText, type)) {
    return;
  }

  previousText         = text;
  previousOverflowText = overflowText;
  previousBufferType   = type;
  useSystemEmoji       = useSystemEmoji();

  if (useSystemEmoji || candidates == null || candidates.size() == 0) {
    super.setText(new SpannableStringBuilder(Optional.fromNullable(text).or("")).append(Optional.fromNullable(overflowText).or("")), BufferType.NORMAL);

    if (getEllipsize() == TextUtils.TruncateAt.END && maxLength > 0) {
      ellipsizeAnyTextForMaxLength();
    }
  } else {
    CharSequence emojified = provider.emojify(candidates, text, this);
    super.setText(new SpannableStringBuilder(emojified).append(Optional.fromNullable(overflowText).or("")), BufferType.SPANNABLE);

    // Android fails to ellipsize spannable strings. (https://issuetracker.google.com/issues/36991688)
    // We ellipsize them ourselves by manually truncating the appropriate section.
    if (getEllipsize() == TextUtils.TruncateAt.END) {
      if (maxLength > 0) {
        ellipsizeAnyTextForMaxLength();
      } else {
        ellipsizeEmojiTextForMaxLines();
      }
    }
  }
}