下面列出了android.util.TypedValue#COMPLEX_UNIT_PX 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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));
}
@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);
}
}
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));
}
/**
* 各种单位转换
* <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;
}
/**
* 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;
}
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();
}
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;
}
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;
}
}
/**
* Reset the text to the original size
*/
public void resetTextSize() {
if (mTextSize > 0) {
super.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
mMaxTextSize = mTextSize;
}
}
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));
}
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;
}
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);
}
}
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));
}
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;
}
}
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;
}
}
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;
}
/**
* Reset the text to the original size
*/
public void resetTextSize() {
if (mTextSize > 0) {
super.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
mMaxTextSize = mTextSize;
}
}
@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();
}
}
}
}
public void resetTextSize() {
if (mTextSize > 0) {
super.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
mMaxTextSize = mTextSize;
}
}
private void superSetTextSize(int startSize)
{
super.setTextSize(TypedValue.COMPLEX_UNIT_PX,efficientTextSizeSearch(startSize,(int)_maxTextSize,_sizeTester,_availableSpaceRect));
}