本文整理匯總了Java中android.content.res.Resources.getDisplayMetrics方法的典型用法代碼示例。如果您正苦於以下問題:Java Resources.getDisplayMetrics方法的具體用法?Java Resources.getDisplayMetrics怎麽用?Java Resources.getDisplayMetrics使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.content.res.Resources
的用法示例。
在下文中一共展示了Resources.getDisplayMetrics方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: init
import android.content.res.Resources; //導入方法依賴的package包/類
public static void init(final Resources res) {
// The proximate bogus down move up event hack is needed for a device such like,
// 1) is large tablet, or 2) is small tablet and the screen density is less than hdpi.
// Though it seems odd to use screen density as criteria of the quality of the touch
// screen, the small table that has a less density screen than hdpi most likely has been
// made with the touch screen that needs the hack.
final int screenMetrics = res.getInteger(R.integer.config_screen_metrics);
final boolean isLargeTablet = (screenMetrics == Constants.SCREEN_METRICS_LARGE_TABLET);
final boolean isSmallTablet = (screenMetrics == Constants.SCREEN_METRICS_SMALL_TABLET);
final int densityDpi = res.getDisplayMetrics().densityDpi;
final boolean hasLowDensityScreen = (densityDpi < DisplayMetrics.DENSITY_HIGH);
final boolean needsTheHack = isLargeTablet || (isSmallTablet && hasLowDensityScreen);
if (DEBUG_MODE) {
final int sw = res.getConfiguration().smallestScreenWidthDp;
Log.d(TAG, "needsProximateBogusDownMoveUpEventHack=" + needsTheHack
+ " smallestScreenWidthDp=" + sw + " densityDpi=" + densityDpi
+ " screenMetrics=" + screenMetrics);
}
sNeedsProximateBogusDownMoveUpEventHack = needsTheHack;
}
示例2: detectLanguage
import android.content.res.Resources; //導入方法依賴的package包/類
@SuppressWarnings("deprecation")
public static void detectLanguage(Context context) {
SharedPreferences setting = PreferenceManager
.getDefaultSharedPreferences(context);
String language = setting.getString(Util.PREF_LANGUAGE, "auto");
Resources res = context.getResources();
Configuration conf = res.getConfiguration();
switch (language) {
case "en":
case "zh":
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
conf.setLocale(new Locale(language));
} else {
conf.locale = new Locale(language);
}
break;
default:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
conf.setLocale(Resources.getSystem().getConfiguration().getLocales().get(0));
} else {
conf.locale = Resources.getSystem().getConfiguration().locale;
}
}
DisplayMetrics dm = res.getDisplayMetrics();
res.updateConfiguration(conf, dm);
}
示例3: switchLanguage
import android.content.res.Resources; //導入方法依賴的package包/類
protected void switchLanguage(String lang) {
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
switch (lang) {
case "zh":
configuration.locale = Locale.TAIWAN;
break;
case "ja":
configuration.locale = Locale.JAPAN;
break;
default:
configuration.locale = Locale.ENGLISH;
break;
}
resources.updateConfiguration(configuration, displayMetrics);
/* 避免重複寫入
MainApplication.writeSetting("uiLang", lang);
//*/
}
示例4: resetDimensionConstants
import android.content.res.Resources; //導入方法依賴的package包/類
/**
* Helper function that gather the static constants from values/dimens.xml.
* @param context The Android Context.
*/
public static void resetDimensionConstants(Context context) {
Resources res = context.getResources();
final float pxToDp = 1.0f / res.getDisplayMetrics().density;
sStackedTabVisibleSize =
res.getDimensionPixelOffset(R.dimen.stacked_tab_visible_size) * pxToDp;
sStackBufferWidth = res.getDimensionPixelOffset(R.dimen.stack_buffer_width) * pxToDp;
sStackBufferHeight = res.getDimensionPixelOffset(R.dimen.stack_buffer_height) * pxToDp;
}
示例5: setFakeConfig
import android.content.res.Resources; //導入方法依賴的package包/類
/**
* Sets a fake configuration to the passed Resources to allow access to resources
* accessible to a sdk level. Used to backport adaptive icon support to different
* devices.
*
* @param resources the resources to set the configuration to
* @param sdk the sdk level to become accessible
* @throws NoSuchMethodException if something is wrong
* @throws IllegalAccessException if something is very wrong
* @throws InvocationTargetException if something is really very extremely wrong
*/
public static void setFakeConfig(Resources resources, int sdk) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
int width, height;
DisplayMetrics metrics = resources.getDisplayMetrics();
if (metrics.widthPixels >= metrics.heightPixels) {
width = metrics.widthPixels;
height = metrics.heightPixels;
} else {
width = metrics.heightPixels;
height = metrics.widthPixels;
}
Configuration configuration = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AssetManager.class.getDeclaredMethod("setConfiguration", int.class, int.class, String.class, int.class, int.class,
int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class,
int.class, int.class, int.class, int.class)
.invoke(resources.getAssets(), configuration.mcc, configuration.mnc,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? configuration.locale.toLanguageTag() : null,
configuration.orientation, configuration.touchscreen, configuration.densityDpi,
configuration.keyboard, configuration.keyboardHidden, configuration.navigation,
width, height, configuration.smallestScreenWidthDp,
configuration.screenWidthDp, configuration.screenHeightDp, configuration.screenLayout,
configuration.uiMode, configuration.colorMode, sdk);
} else {
AssetManager.class.getDeclaredMethod("setConfiguration", int.class, int.class, String.class, int.class, int.class,
int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class,
int.class, int.class, int.class)
.invoke(resources.getAssets(), configuration.mcc, configuration.mnc,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? configuration.locale.toLanguageTag() : null,
configuration.orientation, configuration.touchscreen, configuration.densityDpi,
configuration.keyboard, configuration.keyboardHidden, configuration.navigation,
width, height, configuration.smallestScreenWidthDp,
configuration.screenWidthDp, configuration.screenHeightDp, configuration.screenLayout,
configuration.uiMode, sdk);
}
}
示例6: PinView
import android.content.res.Resources; //導入方法依賴的package包/類
public PinView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
final Resources res = getResources();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.density = res.getDisplayMetrics().density;
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setTextSize(getTextSize());
mAnimatorTextPaint = new TextPaint(mTextPaint);
final Resources.Theme theme = context.getTheme();
TypedArray a = theme.obtainStyledAttributes(attrs, R.styleable.PinView, defStyleAttr, 0);
mViewType = a.getInt(R.styleable.PinView_viewType, VIEW_TYPE_RECTANGLE);
mPinItemCount = a.getInt(R.styleable.PinView_itemCount, DEFAULT_COUNT);
mPinItemSize = a.getDimensionPixelSize(R.styleable.PinView_itemSize,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_size));
mPinItemHeight = mPinItemWidth = mPinItemSize;
if (a.hasValue(R.styleable.PinView_itemHeight)) {
mPinItemHeight = a.getDimensionPixelOffset(R.styleable.PinView_itemHeight,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_size));
}
if (a.hasValue(R.styleable.PinView_itemWidth)) {
mPinItemWidth = a.getDimensionPixelOffset(R.styleable.PinView_itemWidth,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_size));
}
mPinItemSpacing = a.getDimensionPixelOffset(R.styleable.PinView_itemSpacing,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_spacing));
mPinItemRadius = a.getDimensionPixelOffset(R.styleable.PinView_itemRadius,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_radius));
mLineWidth = a.getDimensionPixelOffset(R.styleable.PinView_borderWidth,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_line_width));
mLineColor = a.getColorStateList(R.styleable.PinView_borderColor);
if (a.hasValue(R.styleable.PinView_lineWidth)) {
mLineWidth = a.getDimensionPixelOffset(R.styleable.PinView_lineWidth,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_line_width));
}
if (a.hasValue(R.styleable.PinView_lineColor)) {
mLineColor = a.getColorStateList(R.styleable.PinView_lineColor);
}
a.recycle();
setMaxLength(mPinItemCount);
mPaint.setStrokeWidth(mLineWidth);
setupAnimator();
setCursorVisible(false);
setTextIsSelectable(false);
}
示例7: initWith
import android.content.res.Resources; //導入方法依賴的package包/類
public static void initWith(@NonNull final Context context) {
final Resources resources = context.getResources();
final DisplayMetrics dm = resources.getDisplayMetrics();
mDensity = dm.density;
final Configuration configuration = resources.getConfiguration();
mScreenWidth = configuration.orientation == Configuration.ORIENTATION_PORTRAIT ? dm.widthPixels : dm.heightPixels;
mScreenHeight = configuration.orientation == Configuration.ORIENTATION_PORTRAIT ? dm.heightPixels : dm.widthPixels;
}
示例8: createIconGenerator
import android.content.res.Resources; //導入方法依賴的package包/類
@VisibleForTesting
static RoundedIconGenerator createIconGenerator(Resources resources) {
int largeIconWidthPx =
resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
int largeIconHeightPx =
resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
float density = resources.getDisplayMetrics().density;
int cornerRadiusPx = Math.min(largeIconWidthPx, largeIconHeightPx) / 2;
return new RoundedIconGenerator(largeIconWidthPx, largeIconHeightPx, cornerRadiusPx,
NOTIFICATION_ICON_BG_COLOR, NOTIFICATION_ICON_TEXT_SIZE_DP * density);
}
示例9: pixel2dip
import android.content.res.Resources; //導入方法依賴的package包/類
public float pixel2dip(Context context, float n){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = n / (metrics.densityDpi / 160f);
return dp;
}
示例10: dpToPx
import android.content.res.Resources; //導入方法依賴的package包/類
public static int dpToPx(Resources resources, int dp) {
return (int) (dp * resources.getDisplayMetrics().density);
}
示例11: convertDpToPixel
import android.content.res.Resources; //導入方法依賴的package包/類
public static float convertDpToPixel(float dp, Context context) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
示例12: sp2px
import android.content.res.Resources; //導入方法依賴的package包/類
public static float sp2px(Resources resources, float sp){
final float scale = resources.getDisplayMetrics().scaledDensity;
return sp * scale;
}
示例13: convertDpToPx
import android.content.res.Resources; //導入方法依賴的package包/類
public static int convertDpToPx(View view, int dp) {
Resources resources = view.getContext().getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return Math.round(dp * (metrics.densityDpi / 160.0f));
}
示例14: convertPixelsToDp
import android.content.res.Resources; //導入方法依賴的package包/類
public static float convertPixelsToDp(float px, Resources resources) {
DisplayMetrics metrics = resources.getDisplayMetrics();
return px / metrics.density;
}
示例15: convertSpToPx
import android.content.res.Resources; //導入方法依賴的package包/類
/**
* Convert sp(scale-independent pixels) unit to equivalent px(pixels), depending on
* device scaledDensity of {@link DisplayMetrics}.
*
* @param sp value in sp unit which will be converted into px
* @param context context to get resources and device specific display metrics
* @return a float value to represent px equivalent to sp
*/
public synchronized static float convertSpToPx(float sp, Context context) {
final Resources resources = context.getResources();
final DisplayMetrics metrics = resources.getDisplayMetrics();
final float fontScale = metrics.scaledDensity;
return sp * fontScale;
}