Application scenarios and summaries of Activity style, status bar transparency, screen brightness issues

Keywords: Android Attribute xml encoding

A summary of Activity as a dialog full screen display, immersive status bar and screen brightness issues

Requirements:

  • 1. Pop up a full-screen Dialog, which does a lot of logical processing, such as grabbing red bags, requesting interfaces, such as animation effects.

  • 2. Change the background color of the current layout by an event

Design sketch:

Analysis:

    1. If you encounter a dialog with a more complex layout and logic, it is recommended that you use pop-up activity as a dialog because life cycle and its API provide more
    1. Set theme for full screen dialog
    1. Implement status bar transparency
    1. Setting the brightness

Set Activity Transparency

  • Using a custom theme, first look at some of the property settings you need to use in your custom theme

     <!-- Add a color value pattern here ARGB{xxxxxxxx},A{First two}Express Appha I.e. transparency with a value of 0-255 -->
        <style name="activity_DialogTransparent">
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowFullscreen">true</item>
            <item name="android:backgroundDimEnabled">true</item>
            <item name="android:backgroundDimAmount">0.2</item>
            <item name="android:windowIsTranslucent">true</item>
            <item name="android:layout_width">fill_parent</item>
            <item name="android:layout_height">fill_parent</item>
            <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> <!--Activity Toggle Animation Effects-->
        </style>
    

    After defining a theme, you need to make a reference to it in the Activity configuration!

    Method of setting transparency grayscale to form in code
    Set transparency (this is the transparency of the form itself, not the background)

    WindowManager.LayoutParams windowLP = getWindow().getAttributes();
    windowLP.alpha = 0.5f;
    getWindow().setAttributes(windowLP);
    

    alpha is between 0.0f and 1.0f.1.0 completely opaque, 0.0f completely transparent

    Set Grayscale

    WindowManager.LayoutParams windowLP = getWindow().getAttributes();
    windowLP.dimAmount = 0.5f;
    getWindow().setAttributes(windowLP);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    

    dimAmount is between 0.0f and 1.0f, 0.0f is completely dark, 1.0F is completely dark

    These settings are also valid for dialog dialogs;

  • Declare when configuring Activity in manifest file

    android:theme="@android:style/Theme.Translucent" 
    

Set Activity/Application Full Screen

1. Set in code

   //No title    
   requestWindowFeature(Window.FEATURE_NO_TITLE);    
    //Full screen    
   getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
   //These two pieces of code must be set before the setContentView() method
   setContentView(R.layout.main);  

2. Set in configuration file
Set theme to full screen in Activity declaration

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

Status Bar Coloring-Transparent Status Bar

http://blog.csdn.net/androidstarjack/article/details/53114097

Starting with Android 4.4, status bar coloring is possible and has been enhanced since 5.0 by setting it directly in the theme

<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

perhaps

getWindow().setStatusBarColor(color)

To achieve this, but after all, 4.4+ machines still account for a large proportion, so it is necessary to find other solutions.

First option:

1. First, make the status bar of your mobile phone transparent:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 and above
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 to 5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }

Execute this code in the appropriate Activity or base class.

It can be seen that systems from 4.4 to 5.0, 5.0 and above are handled differently

In addition to this amount of code modification, you can modify it through themes, which need to be created in the values, values-v19, values-v21 directories:

//values
<style name="TranslucentTheme" parent="AppTheme">
</style>

//values-v19
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>
</style>

//values-v21
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
</style>

Setting the theme to the appropriate Activity or Application is ok ay.

You just need to choose between the two options, so here we're done with the first step, making the status bar transparent.

Finished the first step, let's start adding the colors we want to the status bar!

Add the following dimensions to the values, values-v19 directory:

//values
<dimen name="padding_top">0dp</dimen>

//values-v19
<dimen name="padding_top">25dp</dimen>

With regard to 25dp, there may be errors on some systems, which are not discussed here!

2.1 Use Toolbar (or custom title) at the top of the page
In general, the color of the status bar is the same as that of the Toolbar. Since the status bar is transparent and the layout page extends to the status bar, why not add a top padding with the height of the status bar to the Toolbar?

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:paddingTop="@dimen/padding_top"
    android:theme="@style/AppTheme.AppBarOverlay" />

Effects below:

The second option:

In scenario one, instead of using the android:fitsSystemWindows="true" property, we extend the layout to the status bar. This time, we use the android:fitsSystemWindows="true" property to prevent the layout from extending to the status bar, where the status bar is transparent, and then add a specified color View with the same height and width as the status bar to override the transparent state.Column.Let's do it one step at a time.

1. The first step is to make the status bar transparent, the same as above.

2. Add the android:fitsSystemWindows="true" attribute to the layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:title="Second option" />
</LinearLayout>

3. Create a View and add it to the status bar:

private void addStatusBarView() {
        View view = new View(this);
        view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                getStatusBarHeight(this));
        ViewGroup decorView = (ViewGroup) findViewById(android.R.id.content);
        decorView.addView(view, params);
    }

The principle is simple, but you have to write extra code.Finally, look at the effect:

The third option:

Similar to scenario 2, use the android:fitsSystemWindows="true" attribute, and then modify the root layout of the layout file to the desired status bar color. Because the color of the root layout is modified, you need to nest an additional layer inside to specify the main background color of the interface, such as white, or else it will be the same as the status bar color.It's a bit abstract, but let's look at a specific example:

1. Make the status bar transparent first, using the same method as above.

2. Modify the layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff9900"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ff9900"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:title="Third option" />
    </LinearLayout>
</RelativeLayout>

Modified, see the effect:

If your project has dozens of interfaces, modifying them in this way is tiring, and you have to consider nesting issues as well.
The latter two scenarios are relatively simple, and you can try more scenarios if you are interested!
Three ways to choose, I believe you should have an answer here, I personally prefer the first one!

About Activity Screen Brightness

/**
 * Android Gets and sets the brightness of the Activity
 * This API only works with versions 2.1 and above
 */
public class ActivityUtils  {
    /**
     * Determine if automatic brightness adjustment is turned on
     *
     * @param aContentResolver
     * @return
     */
    public static boolean isAutoBrightness(ContentResolver aContentResolver) {
        boolean automicBrightness = false;
        try {
            automicBrightness = Settings.System.getInt(aContentResolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return automicBrightness;
    }

    /**
     * Get the brightness of the screen
     *
     * @param activity
     * @return
     */
    public static int getScreenBrightness(Activity activity) {
        int nowBrightnessValue = 0;
        ContentResolver resolver = activity.getContentResolver();
        try {
            nowBrightnessValue = android.provider.Settings.System.getInt(
                    resolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nowBrightnessValue;
    }

    /**
     * Set Brightness
     *
     * @param activity
     * @param brightness
     */
    public static void setBrightness(Activity activity, int brightness) {
        // Settings.System.putInt(activity.getContentResolver(),
        // Settings.System.SCREEN_BRIGHTNESS_MODE,
        // Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
        activity.getWindow().setAttributes(lp);
    }

    /**
     * Stop automatic brightness adjustment
     *
     * @param activity
     */
    public static void stopAutoBrightness(Activity activity) {
        Settings.System.putInt(activity.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    /**
     * Turn on automatic brightness adjustment
     *
     * @param activity
     */
    public static void startAutoBrightness(Activity activity) {
        Settings.System.putInt(activity.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }

    /**
     * Save brightness setting state
     *
     * @param resolver
     * @param brightness
     */
    public static void saveBrightness(ContentResolver resolver, int brightness) {
        Uri uri = android.provider.Settings.System
                .getUriFor("screen_brightness");
        android.provider.Settings.System.putInt(resolver, "screen_brightness",
                brightness);
        resolver.notifyChange(uri, null);
    }
}

Note that the above applies only to version 2.1.

You can change its brightness through the seekBar:

/**
*   Use SeekBar for brightness control:
*/
private void detalSeekBar() {
        sSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                LogUtil.e("yuyahao","progress:  "+progress);
                if (progress < 10) {
                } else {
                    messageTv.setText("activity The current brightness is: "+progress);
                    ActivityUtils.setBrightness(MyShowLightDialog.this, progress);
                    //ll_contentView.setBackgroundResource(ContextCompat.getColor(MainActivity.this,Color.parseColor("#"+getRandColorCode()));
                    ll_contentView.setBackgroundColor(Color.parseColor("#"+getRandColorCode()));
                }
            }
        });
        //Get the position of the current brightness
//        int a =ActivityUtils.getScreenBrightness(this);
//        sSeekBar.setProgress(a);
    }

Design sketch:

If you find this article helpful, please join the QQ group: 23220388
WeChat Public Number: Terminal Research and Development Department

(Welcome to learning and communication)

Posted by jvanv8 on Thu, 20 Jun 2019 11:23:22 -0700