Java Code Examples for android.util.TypedValue#COMPLEX_UNIT_DIP
The following examples show how to use
android.util.TypedValue#COMPLEX_UNIT_DIP .
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: SizeUtils.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
/** * 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 2
Source File: Bypass.java From bypasses with Apache License 2.0 | 6 votes |
public Options() { mHeaderSizes = new float[]{ 1.5f, // h1 1.4f, // h2 1.3f, // h3 1.2f, // h4 1.1f, // h5 1.0f, // h6 }; mUnorderedListItem = "\u2022"; mListItemIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mListItemIndentSize = 10; mBlockQuoteColor = 0xff0000ff; mBlockQuoteIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mBlockQuoteIndentSize = 10; mCodeBlockIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mCodeBlockIndentSize = 10; mHruleColor = Color.GRAY; mHruleUnit = TypedValue.COMPLEX_UNIT_DIP; mHruleSize = 1; }
Example 3
Source File: MixtureTextView.java From MixtureTextView with Apache License 2.0 | 6 votes |
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 4
Source File: Bypass.java From narrate-android with Apache License 2.0 | 6 votes |
public Options() { mHeaderSizes = new float[] { 1.5f, // h1 1.4f, // h2 1.3f, // h3 1.2f, // h4 1.1f, // h5 1.0f, // h6 }; mUnorderedListItem = "\u2022"; mListItemIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mListItemIndentSize = 10; mBlockQuoteColor = 0xff0000ff; mBlockQuoteIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mBlockQuoteIndentSize = 10; mCodeBlockIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mCodeBlockIndentSize = 10; mHruleColor = Color.GRAY; mHruleUnit = TypedValue.COMPLEX_UNIT_DIP; mHruleSize = 1; }
Example 5
Source File: SizeUtils.java From AcgClub with MIT License | 6 votes |
/** * 各种单位转换 * <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 6
Source File: ScreenAdaperHelper.java From screenAdaptation with Apache License 2.0 | 6 votes |
public void getResources(Resources res) { if (application != null) { //To solve the problem that the DisplayMetrics attribute value is restored in some cases float xdpi = mSharedPreferences.getFloat("xdpi", 0f); if ((res.getClass().getSimpleName().contains("MiuiResources") || res.getClass().getSimpleName().contains("XResources")) && !enableOtherResources) return; if (xdpi > 0f && getMetrics(res).xdpi != xdpi) { if (unit == TypedValue.COMPLEX_UNIT_DIP) { if (enableDp) getMetrics(res).density = xdpi; } else { getMetrics(res).xdpi = xdpi; } } else if (xdpi <= 0f) setDensity(res, unit); } }
Example 7
Source File: SizeUtils.java From Matisse with Apache License 2.0 | 6 votes |
/** * 各种单位转换 * <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 8
Source File: XDensityUtils.java From XFrame with Apache License 2.0 | 6 votes |
/** * 各种单位转换 * <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 9
Source File: DimensionParser.java From android-cassowary-layout with Apache License 2.0 | 6 votes |
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 10
Source File: SizeUtils.java From Ticket-Analysis with MIT License | 6 votes |
/** * 各种单位转换 * <p>该方法存在于TypedValue */ 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 11
Source File: SizeUtils.java From Android-UtilCode with Apache License 2.0 | 6 votes |
/** * 各种单位转换 * <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 12
Source File: DimenUtils.java From Common with Apache License 2.0 | 6 votes |
/** * 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(@NonNull final Context context, final float value, final int unit) { DisplayMetrics metrics = context.getResources().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 13
Source File: PixelValue.java From HtmlNative with Apache License 2.0 | 5 votes |
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 14
Source File: ScreenAdaperHelper.java From screenAdaptation with Apache License 2.0 | 5 votes |
/** * @param unit {@link TypedValue} Not recommended COMPLEX_UNIT_PX、COMPLEX_UNIT_DIP、COMPLEX_UNIT_SP * recommend COMPLEX_UNIT_PT */ private void setDensity(Resources res, int unit) { if (res != null) { try { if ((res.getClass().getSimpleName().contains("MiuiResources") || res.getClass().getSimpleName().contains("XResources")) && !enableOtherResources) return; float xdpi = 0f; DisplayMetrics dm = getMetrics(res); if (dm == null) return; switch (unit) { case TypedValue.COMPLEX_UNIT_PT: xdpi = dm.widthPixels / DESIGN_WIDTH * 72; break; case TypedValue.COMPLEX_UNIT_IN: xdpi = dm.widthPixels / DESIGN_WIDTH; break; case TypedValue.COMPLEX_UNIT_MM: xdpi = dm.widthPixels / DESIGN_WIDTH * 25.4f; break; case TypedValue.COMPLEX_UNIT_DIP: xdpi = dm.widthPixels / DESIGN_WIDTH; break; } SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putFloat("xdpi", xdpi); editor.apply(); if (unit == TypedValue.COMPLEX_UNIT_DIP) { if (enableDp) dm.density = xdpi; } else { dm.xdpi = xdpi; } } catch (Exception e) { e.printStackTrace(); } } }
Example 15
Source File: Bypass.java From materialup with Apache License 2.0 | 5 votes |
public Options() { mHeaderSizes = new float[]{ 1.5f, // h1 1.4f, // h2 1.3f, // h3 1.2f, // h4 1.1f, // h5 1.0f, // h6 }; mUnorderedListItem = "\u2022"; mListItemIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mListItemIndentSize = 10; mBlockQuoteLineColor = 0xff0000ff; mBlockQuoteIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mBlockQuoteIndentSize = 10; mPreImageLinebreakHeight = 4; mCodeBlockIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mCodeBlockIndentSize = 10; mHruleColor = Color.GRAY; mHruleUnit = TypedValue.COMPLEX_UNIT_DIP; mHruleSize = 1; }
Example 16
Source File: Bypass.java From android-proguards with Apache License 2.0 | 5 votes |
public Options() { mHeaderSizes = new float[]{ 1.5f, // h1 1.4f, // h2 1.3f, // h3 1.2f, // h4 1.1f, // h5 1.0f, // h6 }; mUnorderedListItem = "\u2022"; mListItemIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mListItemIndentSize = 10; mBlockQuoteLineColor = 0xff0000ff; mBlockQuoteIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mBlockQuoteIndentSize = 10; mPreImageLinebreakHeight = 4; mCodeBlockIndentUnit = TypedValue.COMPLEX_UNIT_DIP; mCodeBlockIndentSize = 10; mHruleColor = Color.GRAY; mHruleUnit = TypedValue.COMPLEX_UNIT_DIP; mHruleSize = 1; }
Example 17
Source File: DimensionResource.java From external-resources with Apache License 2.0 | 5 votes |
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 18
Source File: DimensionResourceTest.java From external-resources with Apache License 2.0 | 5 votes |
@Test public void testToFloat() throws Exception { resource = new DimensionResource(TypedValue.COMPLEX_UNIT_DIP, 2.0f); DisplayMetrics metrics = new DisplayMetrics(); metrics.density = 200; assertTrue(resource.toFloat(metrics) == 400.0f); }
Example 19
Source File: XmlToClassAttribHandler.java From RxAndroidBootstrap with Apache License 2.0 | 5 votes |
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 20
Source File: SpinnerWidget.java From commcare-android with Apache License 2.0 | 4 votes |
public SpinnerWidget(Context context, FormEntryPrompt prompt) { super(context, prompt); mItems = getSelectChoices(); spinner = new Spinner(context); choices = new String[mItems.size()]; for (int i = 0; i < mItems.size(); i++) { choices[i] = prompt.getSelectChoiceText(mItems.get(i)); } // The spinner requires a custom adapter. It is defined below SpinnerAdapter adapter = new SpinnerAdapter(getContext(), android.R.layout.simple_spinner_item, getChoicesWithEmptyFirstSlot(choices), TypedValue.COMPLEX_UNIT_DIP, mQuestionFontSize); spinner.setAdapter(adapter); spinner.setPrompt(prompt.getQuestionText()); spinner.setEnabled(!prompt.isReadOnly()); spinner.setFocusable(!prompt.isReadOnly()); // Fill in previous answer String s = null; if (prompt.getAnswerValue() != null) { s = ((Selection)prompt.getAnswerValue().getValue()).getValue(); } if (s != null) { for (int i = 0; i < mItems.size(); ++i) { String sMatch = mItems.get(i).getValue(); if (sMatch.equals(s)) { // Workaround to a really weird spinner bug that triggers onItemSelected // in the initial selection of spinner even though no listener is set. // https://stackoverflow.com/a/17336944/6671572 spinner.setSelection(i+1, false); } } } spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { widgetEntryChanged(); } @Override public void onNothingSelected(AdapterView<?> parent) { //do nothing here } }); addView(spinner); }