下面列出了android.content.Context#getResources ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public YearPickerView(Context context, DatePickerController controller) {
super(context);
mController = controller;
mController.registerOnDateChangedListener(this);
ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
setLayoutParams(frame);
Resources res = context.getResources();
mViewSize = mController.getVersion() == DatePickerDialog.Version.VERSION_1
? res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height)
: res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height_v2);
mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height);
setVerticalFadingEdgeEnabled(true);
setFadingEdgeLength(mChildSize / 3);
init();
setOnItemClickListener(this);
setSelector(new StateListDrawable());
setDividerHeight(0);
onDateChanged();
}
public static WindowManager.LayoutParams createLayoutParams(Context context) {
Resources res = context.getResources();
int width = res.getDimensionPixelSize(R.dimen.view_inspector_toolbar_header_width)
+ res.getDimensionPixelSize(R.dimen.view_inspector_toolbar_icon_width) * TOOLBAR_MENU_ITEMS;
int height = res.getDimensionPixelSize(R.dimen.view_inspector_toolbar_height);
if (Build.VERSION.SDK_INT == 23) { // MARSHMALLOW
height = res.getDimensionPixelSize(R.dimen.view_inspector_toolbar_height_m);
}
final WindowManager.LayoutParams params =
new WindowManager.LayoutParams(width, height, TYPE_SYSTEM_ERROR,
FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL | FLAG_LAYOUT_NO_LIMITS
| FLAG_LAYOUT_INSET_DECOR | FLAG_LAYOUT_IN_SCREEN, TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.RIGHT;
return params;
}
public static ContextWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
} else {
configuration.locale = newLocale;
res.updateConfiguration(configuration, res.getDisplayMetrics());
}
return new ContextWrapper(context);
}
/**
* 重新计算displayMetrics.xhdpi, 使单位pt重定义为设计稿的相对长度
* @see #activate()
*
* @param context
* @param designWidth 设计稿的宽度
*/
public static void resetDensity(Context context, float designWidth){
if(context == null)
return;
Point size = new Point();
((WindowManager)context.getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getSize(size);
Resources resources = context.getResources();
resources.getDisplayMetrics().xdpi = size.x/designWidth*72f;
DisplayMetrics metrics = getMetricsOnMiui(context.getResources());
if(metrics != null)
metrics.xdpi = size.x/designWidth*72f;
}
@UiThread
public void initialize(Context context, final BookmarkId folderId,
BookmarkLoaderCallback callback) {
mCallback = callback;
Resources res = context.getResources();
mLargeIconBridge = new LargeIconBridge(
Profile.getLastUsedProfile().getOriginalProfile());
mMinIconSizeDp = (int) res.getDimension(R.dimen.default_favicon_min_size);
mDisplayedIconSize = res.getDimensionPixelSize(R.dimen.default_favicon_size);
mCornerRadius = res.getDimensionPixelSize(R.dimen.default_favicon_corner_radius);
int textSize = res.getDimensionPixelSize(R.dimen.default_favicon_icon_text_size);
int iconColor =
ApiCompatibilityUtils.getColor(res, R.color.default_favicon_background_color);
mIconGenerator = new RoundedIconGenerator(mDisplayedIconSize, mDisplayedIconSize,
mCornerRadius, iconColor, textSize);
mRemainingTaskCount = 1;
mBookmarkModel = new BookmarkModel();
mBookmarkModel.runAfterBookmarkModelLoaded(new Runnable() {
@Override
public void run() {
loadBookmarks(folderId);
}
});
}
public static boolean hasNavBar(Context context) {
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
if (hasBackKey && hasHomeKey) {
if (Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).contains("samsung") && !Build.MODEL.toLowerCase(Locale.ENGLISH).contains("nexus")) {
return false;
}
Resources resources = context.getResources();
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
return resources.getBoolean(id);
} else {
return false;
}
} else {
return true;
}
}
public DriverMapPresenter(Context context, DriverMapPresenter.DriverView view) {
super(context, view, new DriverState(context));
Resources r = context.getResources();
float paddingTop = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, r.getDisplayMetrics());
float paddingBottom = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, r.getDisplayMetrics());
mapCenterOffset = (int) ((paddingBottom - paddingTop) / 2);
trackingPresenter = new TrackingPresenter(context, view);
}
public static int getNavigationBarHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
/**
* Constructor for <code>LetterTileProvider</code>
*
* @param context The {@link Context} to use
*/
public LetterBitmap(Context context) {
final Resources res = context.getResources();
mPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.BOLD));
mPaint.setColor(Color.WHITE);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setAntiAlias(true);
mColors = res.obtainTypedArray(R.array.letter_tile_colors);
TypedValue typedValue = new TypedValue();
res.getValue(R.dimen.tile_letter_font_size_scale, typedValue, true);
mTileLetterFontSizeScale = typedValue.getFloat();
}
static void updateMoreAppsIcon(Context context) {
try {
Drawable d = new BitmapDrawable(context.getResources(), new IconHelper(mContext).cachedResourceIconHelper(MORE_APPS_PACKAGE));
PrefsFragment mGeneralSettings = (PrefsFragment) mGetFragments.getFragmentByPosition(GENERAL_TAB);
mGeneralSettings.more_apps_icon_preference.setIcon(d);
} catch (Exception e) {
}
}
/**
* Creates an empty InfoBarContainerLayout.
*/
InfoBarContainerLayout(Context context) {
super(context);
Resources res = context.getResources();
mBackInfobarHeight = res.getDimensionPixelSize(R.dimen.infobar_peeking_height);
mFloatingBehavior = new FloatingBehavior(this);
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
Resources r = context.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpValue, r.getDisplayMetrics());
return (int) px;
}
/**
* @param context A context.
* @param listener An optional listener.
*/
public RawResourceDataSource(Context context, @Nullable TransferListener listener) {
super(/* isNetwork= */ false);
this.resources = context.getResources();
if (listener != null) {
addTransferListener(listener);
}
}
protected ImageLoader(Context context, int imageSize) {
mResources = context.getResources();
mImageSize = imageSize;
}
@Override
public void onReceive(Context context, Intent intent) {
if (DEBUG) log("Broadcast received: " + intent.toString());
if (intent.getAction().equals(ACTION_GET_AUTOBRIGHTNESS_CONFIG) &&
intent.hasExtra("receiver")) {
ResultReceiver receiver = intent.getParcelableExtra("receiver");
Bundle data = new Bundle();
Resources res = context.getResources();
data.putIntArray("config_autoBrightnessLevels",
res.getIntArray(res.getIdentifier(
"config_autoBrightnessLevels", "array", "android")));
data.putIntArray("config_autoBrightnessLcdBacklightValues",
res.getIntArray(res.getIdentifier(
"config_autoBrightnessLcdBacklightValues", "array", "android")));
receiver.send(RESULT_AUTOBRIGHTNESS_CONFIG, data);
} else if (intent.getAction().equals(ACTION_SET_AUTOBRIGHTNESS_CONFIG)) {
int[] luxArray = intent.getIntArrayExtra("config_autoBrightnessLevels");
int[] brightnessArray = intent.getIntArrayExtra("config_autoBrightnessLcdBacklightValues");
updateAutobrightnessConfig(luxArray, brightnessArray);
} else if (intent.getAction().equals(GravityBoxSettings.ACTION_PREF_BUTTON_BACKLIGHT_CHANGED)) {
if (intent.hasExtra(GravityBoxSettings.EXTRA_BB_MODE)) {
mButtonBacklightMode = intent.getStringExtra(GravityBoxSettings.EXTRA_BB_MODE);
updateButtonBacklight();
}
if (intent.hasExtra(GravityBoxSettings.EXTRA_BB_NOTIF)) {
mButtonBacklightNotif = intent.getBooleanExtra(GravityBoxSettings.EXTRA_BB_NOTIF, false);
if (!mButtonBacklightNotif) {
updateButtonBacklight();
}
}
} else if ((intent.getAction().equals(Intent.ACTION_SCREEN_ON)
|| intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) &&
!mButtonBacklightMode.equals(GravityBoxSettings.BB_MODE_DEFAULT)) {
updateButtonBacklight(intent.getAction().equals(Intent.ACTION_SCREEN_ON));
} else if (intent.getAction().equals(GravityBoxSettings.ACTION_PREF_LOCKSCREEN_BG_CHANGED) &&
intent.hasExtra(GravityBoxSettings.EXTRA_LOCKSCREEN_BG)) {
mLsBgLastScreenEnabled = intent.getStringExtra(GravityBoxSettings.EXTRA_LOCKSCREEN_BG)
.equals(GravityBoxSettings.LOCKSCREEN_BG_LAST_SCREEN);
if (DEBUG_KIS) log("mLsBgLastScreenEnabled = " + mLsBgLastScreenEnabled);
} else if (intent.getAction().equals(GravityBoxSettings.ACTION_BATTERY_LED_CHANGED) &&
intent.hasExtra(GravityBoxSettings.EXTRA_BLED_CHARGING)) {
ChargingLed cg = ChargingLed.valueOf(intent.getStringExtra(GravityBoxSettings.EXTRA_BLED_CHARGING));
if (cg == ChargingLed.EMULATED || cg == ChargingLed.CONSTANT) {
resetLight(LIGHT_ID_BATTERY);
}
mChargingLed = cg;
if (!mPendingNotif) {
resetLight(LIGHT_ID_NOTIFICATIONS);
}
} else if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
boolean charging = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
int level = (int) (100f * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
/ intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
if (mCharging != charging || mBatteryLevel != level) {
mCharging = charging;
mBatteryLevel = level;
if ((mChargingLed == ChargingLed.EMULATED || mChargingLed == ChargingLed.CONSTANT) &&
!mPendingNotif) {
resetLight(LIGHT_ID_NOTIFICATIONS);
}
}
}
}
@Override
public void onStart() {
final Context context = getContext();
final PowerManager powerManager =
(PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
context.registerReceiver(mDockModeReceiver,
new IntentFilter(Intent.ACTION_DOCK_EVENT));
context.registerReceiver(mBatteryReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
mConfiguration.setToDefaults();
final Resources res = context.getResources();
mDefaultUiModeType = res.getInteger(
com.android.internal.R.integer.config_defaultUiModeType);
mCarModeKeepsScreenOn = (res.getInteger(
com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1);
mDeskModeKeepsScreenOn = (res.getInteger(
com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1);
mEnableCarDockLaunch = res.getBoolean(
com.android.internal.R.bool.config_enableCarDockHomeLaunch);
mUiModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockUiMode);
mNightModeLocked = res.getBoolean(com.android.internal.R.bool.config_lockDayNightMode);
final PackageManager pm = context.getPackageManager();
mTelevision = pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
|| pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
mWatch = pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
final int defaultNightMode = res.getInteger(
com.android.internal.R.integer.config_defaultNightMode);
mNightMode = Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.UI_NIGHT_MODE, defaultNightMode);
// Update the initial, static configurations.
SystemServerInitThreadPool.get().submit(() -> {
synchronized (mLock) {
updateConfigurationLocked();
sendConfigurationLocked();
}
}, TAG + ".onStart");
publishBinderService(Context.UI_MODE_SERVICE, mService);
}
public Drawable getResetIconDrawable(Context context, LauncherActivityInfo app, ItemInfo info) {
final Drawable icon = new BitmapDrawable(context.getResources(), getDrawableIconForPackage(info.getTargetComponent()));
return new BitmapDrawable(context.getResources(), LauncherIcons.createBadgedIconBitmap(icon, info.user, context, app.getApplicationInfo().targetSdkVersion));
}
private ImageLoader(Context context) {
this.weakContext = new WeakReference<>(context);
this.resources = context.getResources();
this.windowManager = ((WindowManager) context.getSystemService(Service.WINDOW_SERVICE));
}
MessageViewHolder(Context context, MessageListener listener, View view) {
super(context, view);
ButterKnife.bind(this, view);
this.resolver = new StringResolver(context.getResources());
this.formatter = new TimeFormatter(resolver);
this.listener = listener;
}
/**
* Find out whether a dictionary is available for this locale.
* @param context the context on which to check resources.
* @param locale the locale to check for.
* @return whether a (non-placeholder) dictionary is available or not.
*/
public static boolean isDictionaryAvailable(final Context context, final Locale locale) {
final Resources res = context.getResources();
return 0 != getMainDictionaryResourceIdIfAvailableForLocale(res, locale);
}