Java Code Examples for android.app.Activity#getResources()

The following examples show how to use android.app.Activity#getResources() . 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: ScreenUtils.java    From Common with Apache License 2.0 5 votes vote down vote up
/**
 * Return the bitmap of screen.
 *
 * @param activity          The activity.
 * @param isDeleteStatusBar True to delete status bar, false otherwise.
 * @return the bitmap of screen
 */
public static Bitmap screenShot(@NonNull final Activity activity, boolean isDeleteStatusBar) {
    View decorView = activity.getWindow().getDecorView();
    boolean drawingCacheEnabled = decorView.isDrawingCacheEnabled();
    boolean willNotCacheDrawing = decorView.willNotCacheDrawing();
    decorView.setDrawingCacheEnabled(true);
    decorView.setWillNotCacheDrawing(false);
    Bitmap bmp = decorView.getDrawingCache();
    if (bmp == null) {
        decorView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        decorView.layout(0, 0, decorView.getMeasuredWidth(), decorView.getMeasuredHeight());
        decorView.buildDrawingCache();
        bmp = Bitmap.createBitmap(decorView.getDrawingCache());
    }
    if (bmp == null) {
        return null;
    }
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap ret;
    if (isDeleteStatusBar) {
        Resources resources = activity.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = resources.getDimensionPixelSize(resourceId);
        ret = Bitmap.createBitmap(bmp,
                0,
                statusBarHeight,
                dm.widthPixels,
                dm.heightPixels - statusBarHeight);
    } else {
        ret = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
    }
    decorView.destroyDrawingCache();
    decorView.setWillNotCacheDrawing(willNotCacheDrawing);
    decorView.setDrawingCacheEnabled(drawingCacheEnabled);
    return ret;
}
 
Example 2
Source File: NavigationUtil.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
/**
 * 检测是否具有底部导航栏
 */
private static boolean checkDeviceHasNavigationBar(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        display.getRealMetrics(realDisplayMetrics);
        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;
        return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    } else {
        boolean hasNavigationBar = false;
        Resources resources = activity.getResources();
        int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = resources.getBoolean(id);
        }
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                hasNavigationBar = true;
            }
        } catch (Exception e) {
        }
        return hasNavigationBar;
    }
}
 
Example 3
Source File: SystemBarTintManager.java    From monolog-android with MIT License 5 votes vote down vote up
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
    Resources res = activity.getResources();
    mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
    mSmallestWidthDp = getSmallestWidthDp(activity);
    mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
    mActionBarHeight = getActionBarHeight(activity);
    mNavigationBarHeight = getNavigationBarHeight(activity);
    mNavigationBarWidth = getNavigationBarWidth(activity);
    mHasNavigationBar = (mNavigationBarHeight > 0);
    mTranslucentStatusBar = translucentStatusBar;
    mTranslucentNavBar = traslucentNavBar;
}
 
Example 4
Source File: HelpActivity.java    From Noyze with Apache License 2.0 5 votes vote down vote up
/** Makes an {@link Activity} a "popup" like a {@link Dialog}. */
public static void popup(final Activity mAct) {
    if (null == mAct) return;
    final Resources aRes = mAct.getResources();
    if (null == aRes) return;

    DisplayMetrics dm = new DisplayMetrics();
    WindowManager wm = (WindowManager) mAct.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(dm);
    final int[] mWindowDims = new int[] { dm.widthPixels, dm.heightPixels };
    final int mMaxWidth = aRes.getDimensionPixelSize(R.dimen.max_menu_width),
              mGutter = aRes.getDimensionPixelSize(R.dimen.activity_horizontal_margin);
    final boolean isBounded = (mWindowDims[0] > mMaxWidth);

    mAct.requestWindowFeature(Window.FEATURE_ACTION_BAR);
    mAct.requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    final Window mWindow = mAct.getWindow();
    mWindow.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
            WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    // Now get our attributes and make the window dim it's background.
    final WindowManager.LayoutParams params = mWindow.getAttributes();
    params.alpha = 1.0f;
    params.dimAmount = 0.5f;

    // Bound the height and barHeight for this overlay.
    if (isBounded) {
        params.width = mMaxWidth + (4 * mGutter);
        // Bound to the maximum of a square.
        if (mWindowDims[1] > params.width) {
            params.height = params.width;
        } else {
            params.height = (int) (0.81f * mWindowDims[1]);
        }
    }

    mWindow.setAttributes(params);
}
 
Example 5
Source File: NavigationUtil.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
/**
     * 获取底部导航栏高度
     */
    public static int getNavigationBarHeight(Activity activity) {
        int navigationBarHeight = 0;
        Resources resources = activity.getResources();
        int resourceId = resources.getIdentifier(isPortrait(activity) ? "navigation_bar_height" : "navigation_bar_height_landscape", "dimen", "android");
        if (resourceId > 0 && checkDeviceHasNavigationBar(activity) && isNavigationBarVisible(activity)) {
            navigationBarHeight = resources.getDimensionPixelSize(resourceId);
        }
        return navigationBarHeight;
//        return 0;
    }
 
Example 6
Source File: BarConfig.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 5 votes vote down vote up
public BarConfig(Activity activity) {
    Resources res = activity.getResources();
    mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
    mSmallestWidthDp = getSmallestWidthDp(activity);
    mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
    mActionBarHeight = getActionBarHeight(activity);
    mNavigationBarHeight = getNavigationBarHeight(activity);
    mNavigationBarWidth = getNavigationBarWidth(activity);
    mHasNavigationBar = (mNavigationBarHeight > 0);
}
 
Example 7
Source File: Util.java    From SeeWeather with Apache License 2.0 5 votes vote down vote up
/**
 * 获取底部 navigation bar 高度
 */
public static int getNavigationBarHeight(Activity mActivity) {
    Resources resources = mActivity.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    int height = resources.getDimensionPixelSize(resourceId);
    PLog.i("Nav height:" + height);
    return height;
}
 
Example 8
Source File: ViewUtil.java    From timecat with Apache License 2.0 5 votes vote down vote up
public static int getNavigationBarHeight(Activity activity) {
    if (!isNavigationBarShow(activity)) {
        return 0;
    }
    Resources resources = activity.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    //获取NavigationBar的高度
    int height = resources.getDimensionPixelSize(resourceId);
    return height;
}
 
Example 9
Source File: SystemBarTintManager.java    From quickmark with MIT License 5 votes vote down vote up
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
    Resources res = activity.getResources();
    mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
    mSmallestWidthDp = getSmallestWidthDp(activity);
    mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
    mActionBarHeight = getActionBarHeight(activity);
    mNavigationBarHeight = getNavigationBarHeight(activity);
    mNavigationBarWidth = getNavigationBarWidth(activity);
    mHasNavigationBar = (mNavigationBarHeight > 0);
    mTranslucentStatusBar = translucentStatusBar;
    mTranslucentNavBar = traslucentNavBar;
}
 
Example 10
Source File: SystemBarTintManager.java    From tup.dota2recipe with Apache License 2.0 5 votes vote down vote up
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
    Resources res = activity.getResources();
    mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
    mSmallestWidthDp = getSmallestWidthDp(activity);
    mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
    mActionBarHeight = getActionBarHeight(activity);
    mNavigationBarHeight = getNavigationBarHeight(activity);
    mNavigationBarWidth = getNavigationBarWidth(activity);
    mHasNavigationBar = (mNavigationBarHeight > 0);
    mTranslucentStatusBar = translucentStatusBar;
    mTranslucentNavBar = traslucentNavBar;
}
 
Example 11
Source File: BarConfig.java    From MNImageBrowser with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Instantiates a new Bar config.
 *
 * @param activity the activity
 */
BarConfig(Activity activity) {
    Resources res = activity.getResources();
    mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
    mSmallestWidthDp = getSmallestWidthDp(activity);
    mStatusBarHeight = getInternalDimensionSize(activity, IMMERSION_STATUS_BAR_HEIGHT);
    mActionBarHeight = getActionBarHeight(activity);
    mNavigationBarHeight = getNavigationBarHeight(activity);
    mNavigationBarWidth = getNavigationBarWidth(activity);
    mHasNavigationBar = (mNavigationBarHeight > 0);
}
 
Example 12
Source File: SystemBarTintManager.java    From school_shop with MIT License 5 votes vote down vote up
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
    Resources res = activity.getResources();
    mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
    mSmallestWidthDp = getSmallestWidthDp(activity);
    mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
    mActionBarHeight = getActionBarHeight(activity);
    mNavigationBarHeight = getNavigationBarHeight(activity);
    mNavigationBarWidth = getNavigationBarWidth(activity);
    mHasNavigationBar = (mNavigationBarHeight > 0);
    mTranslucentStatusBar = translucentStatusBar;
    mTranslucentNavBar = traslucentNavBar;
}
 
Example 13
Source File: SystemBarTintManager.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
    Resources res = activity.getResources();
    mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
    mSmallestWidthDp = getSmallestWidthDp(activity);
    mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
    mActionBarHeight = getActionBarHeight(activity);
    mNavigationBarHeight = getNavigationBarHeight(activity);
    mNavigationBarWidth = getNavigationBarWidth(activity);
    mHasNavigationBar = (mNavigationBarHeight > 0);
    mTranslucentStatusBar = translucentStatusBar;
    mTranslucentNavBar = traslucentNavBar;
}
 
Example 14
Source File: MapsAdapter.java    From android-anuto with GNU General Public License v2.0 5 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Activity activity = mActivityRef.get();

    if (activity == null) {
        return convertView;
    }

    View mapItemView;

    if (convertView == null) {
        mapItemView = LayoutInflater.from(activity).inflate(R.layout.item_map, parent, false);
    } else {
        mapItemView = convertView;
    }

    Resources resources = activity.getResources();
    MapInfo mapInfo = mMapInfos.get(position);
    ViewHolder viewHolder = new ViewHolder(mapItemView);

    viewHolder.txt_name.setText(resources.getString(mapInfo.getMapNameResId()));

    DecimalFormat fmt = new DecimalFormat("###,###,###,###");
    String highScore = fmt.format(mHighScores.getHighScore(mapInfo.getMapId()));
    viewHolder.txt_highscore.setText(resources.getString(R.string.score) + ": " + highScore);

    if (!sThumbCache.containsKey(mapInfo.getMapId())) {
        MapThumbGenerator generator = new MapThumbGenerator();
        Bitmap thumb = generator.generateThumb(resources, mapInfo.getMapDataResId());
        sThumbCache.put(mapInfo.getMapId(), thumb);
    }

    viewHolder.img_thumb.setImageBitmap(sThumbCache.get(mapInfo.getMapId()));

    return mapItemView;
}
 
Example 15
Source File: SystemBarTintManager.java    From ImagePicker with Apache License 2.0 5 votes vote down vote up
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
    Resources res = activity.getResources();
    mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
    mSmallestWidthDp = getSmallestWidthDp(activity);
    mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
    mActionBarHeight = getActionBarHeight(activity);
    mNavigationBarHeight = getNavigationBarHeight(activity);
    mNavigationBarWidth = getNavigationBarWidth(activity);
    mHasNavigationBar = (mNavigationBarHeight > 0);
    mTranslucentStatusBar = translucentStatusBar;
    mTranslucentNavBar = traslucentNavBar;
}
 
Example 16
Source File: MaterialMenuIcon.java    From Conquer with Apache License 2.0 5 votes vote down vote up
@Override
protected View getActionBarUpView(Activity activity) {
    Resources resources = activity.getResources();
    ViewGroup actionBarView = (ViewGroup) activity.getWindow().getDecorView().findViewById(
        resources.getIdentifier("android:id/action_bar", null, null)
    );
    View homeView = actionBarView.getChildAt(
        actionBarView.getChildCount() > 1 ? 1 : 0
    );
    return homeView.findViewById(
        resources.getIdentifier("android:id/up", null, null)
    );
}
 
Example 17
Source File: SystemBarTintManager.java    From KUtils-master with Apache License 2.0 5 votes vote down vote up
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
    Resources res = activity.getResources();
    mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
    mSmallestWidthDp = getSmallestWidthDp(activity);
    mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
    mActionBarHeight = getActionBarHeight(activity);
    mNavigationBarHeight = getNavigationBarHeight(activity);
    mNavigationBarWidth = getNavigationBarWidth(activity);
    mHasNavigationBar = (mNavigationBarHeight > 0);
    mTranslucentStatusBar = translucentStatusBar;
    mTranslucentNavBar = traslucentNavBar;
}
 
Example 18
Source File: ExpandedScreen.java    From Dashchan with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ExpandedScreen(Activity activity, boolean enabled) {
	this.activity = activity;
	statusBar = new StatusBarController(activity);
	navigationBar = new NavigationBarController();
	Resources resources = activity.getResources();
	Window window = activity.getWindow();
	boolean fullScreenLayoutEnabled;
	if (C.API_LOLLIPOP) {
		fullScreenLayoutEnabled = true;
	} else if (C.API_KITKAT) {
		int resId = resources.getIdentifier("config_enableTranslucentDecor", "bool", "android");
		fullScreenLayoutEnabled = resId != 0 && resources.getBoolean(resId);
	} else {
		fullScreenLayoutEnabled = false;
	}
	expandingEnabled = enabled;
	this.fullScreenLayoutEnabled = fullScreenLayoutEnabled;
	float density = ResourceUtils.obtainDensity(resources);
	slopShiftSize = (int) (6f * density);
	lastItemLimit = (int) (72f * density);
	if (fullScreenLayoutEnabled) {
		if (C.API_LOLLIPOP) {
			int statusBarColor = window.getStatusBarColor() | Color.BLACK;
			int navigationBarColor = window.getNavigationBarColor() | Color.BLACK;
			window.setStatusBarColor(Color.TRANSPARENT);
			window.setNavigationBarColor(Color.TRANSPARENT);
			contentForeground = new LollipopContentForeground(statusBarColor, navigationBarColor);
			statusBarContentForeground = new LollipopStatusBarForeground(statusBarColor);
			statusBarDrawerForeground = new LollipopDrawerForeground();
		} else {
			contentForeground = new KitKatContentForeground();
			statusBarContentForeground = null;
			statusBarDrawerForeground = null;
		}
		window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
				View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
	} else {
		contentForeground = null;
		statusBarContentForeground = null;
		statusBarDrawerForeground = null;
		if (enabled) {
			window.requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		}
	}
	readConfiguration(resources.getConfiguration());
}
 
Example 19
Source File: AccountAdapter.java    From Zom-Android-XMPP with GNU General Public License v3.0 4 votes vote down vote up
private void runBindTask( final Activity context, final List<AccountInfo> accountInfoList ) {
    final Resources resources = context.getResources();
    final ContentResolver resolver = context.getContentResolver();
    final ImApp mApp = (ImApp)context.getApplication();

    // if called multiple times
    if (mBindTask != null)
        mBindTask.cancel(false);
    //


    mBindTask = new AsyncTask<Void, Void, List<AccountSetting>>() {

        @Override
        protected List<AccountSetting> doInBackground(Void... params) {
            List<AccountSetting> accountSettingList = new ArrayList<AccountSetting>();
            for( AccountInfo ai : accountInfoList ) {
                accountSettingList.add( getAccountSettings(ai) );
            }
            return accountSettingList;
        }

        private AccountSetting getAccountSettings(AccountInfo ai) {
            AccountSetting as = new AccountSetting();


            Cursor pCursor = resolver.query(Imps.ProviderSettings.CONTENT_URI,new String[] {Imps.ProviderSettings.NAME, Imps.ProviderSettings.VALUE},Imps.ProviderSettings.PROVIDER + "=?",new String[] { Long.toString(ai.providerId)},null);

            if (pCursor != null)
            {
                Imps.ProviderSettings.QueryMap settings =
                        new Imps.ProviderSettings.QueryMap(pCursor, resolver, ai.providerId, false , null);

                as.connectionStatus = ai.dbConnectionStatus;
                as.activeUserName = ai.activeUserName;
                as.domain = settings.getDomain();
                as.host = settings.getServer();
                as.port = settings.getPort();

                /**
                IImConnection conn = mApp.getConnection(ai.providerId,settings.get);
                if (conn == null) {
                    as.connectionStatus = ImConnection.DISCONNECTED;
                } else {
                    try {
                        as.connectionStatus = conn.getState();
                    } catch (RemoteException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }*/

                settings.close();
            }
            return as;
        }

        @Override
        protected void onPostExecute(List<AccountSetting> result) {
            // store
            mBindTask = null;
            // swap
            AccountAdapter.super.swapCursor(mStashCursor);
            if (mListener != null)
                mListener.onPopulate();
        }
    };
    mBindTask.execute();
}
 
Example 20
Source File: GPUCameraRecorderBuilder.java    From GPUVideo-android with MIT License 4 votes vote down vote up
public GPUCameraRecorderBuilder(Activity activity, GLSurfaceView glSurfaceView) {
    this.activity = activity;
    this.glSurfaceView = glSurfaceView;
    this.resources = activity.getResources();
}