深圳全飞鸿

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 691|回复: 1
打印 上一主题 下一主题

FullScreen Activity

[复制链接]

104

主题

171

帖子

1188

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1188
跳转到指定楼层
楼主
发表于 2019-5-22 18:37:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. package com.example.administrator.myapplication9;

  2. import android.annotation.SuppressLint;
  3. import android.support.v7.app.ActionBar;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.view.MotionEvent;
  8. import android.view.View;

  9. /**
  10. * An example full-screen activity that shows and hides the system UI (i.e.
  11. * status bar and navigation/system bar) with user interaction.
  12. */
  13. public class FullscreenActivity extends AppCompatActivity {
  14.     /**
  15.      * Whether or not the system UI should be auto-hidden after
  16.      * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
  17.      */
  18.     private static final boolean AUTO_HIDE = true;

  19.     /**
  20.      * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
  21.      * user interaction before hiding the system UI.
  22.      */
  23.     private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

  24.     /**
  25.      * Some older devices needs a small delay between UI widget updates
  26.      * and a change of the status and navigation bar.
  27.      */
  28.     private static final int UI_ANIMATION_DELAY = 300;
  29.     private final Handler mHideHandler = new Handler();
  30.     private View mContentView;
  31.     private final Runnable mHidePart2Runnable = new Runnable() {
  32.         @SuppressLint("InlinedApi")
  33.         @Override
  34.         public void run() {
  35.             // Delayed removal of status and navigation bar

  36.             // Note that some of these constants are new as of API 16 (Jelly Bean)
  37.             // and API 19 (KitKat). It is safe to use them, as they are inlined
  38.             // at compile-time and do nothing on earlier devices.
  39.             mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
  40.                     | View.SYSTEM_UI_FLAG_FULLSCREEN
  41.                     | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  42.                     | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
  43.                     | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  44.                     | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
  45.         }
  46.     };
  47.     private View mControlsView;
  48.     private final Runnable mShowPart2Runnable = new Runnable() {
  49.         @Override
  50.         public void run() {
  51.             // Delayed display of UI elements
  52.             ActionBar actionBar = getSupportActionBar();
  53.             if (actionBar != null) {
  54.                 actionBar.show();
  55.             }
  56.             mControlsView.setVisibility(View.VISIBLE);
  57.         }
  58.     };
  59.     private boolean mVisible;
  60.     private final Runnable mHideRunnable = new Runnable() {
  61.         @Override
  62.         public void run() {
  63.             hide();
  64.         }
  65.     };
  66.     /**
  67.      * Touch listener to use for in-layout UI controls to delay hiding the
  68.      * system UI. This is to prevent the jarring behavior of controls going away
  69.      * while interacting with activity UI.
  70.      */
  71.     private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
  72.         @Override
  73.         public boolean onTouch(View view, MotionEvent motionEvent) {
  74.             if (AUTO_HIDE) {
  75.                 delayedHide(AUTO_HIDE_DELAY_MILLIS);
  76.             }
  77.             return false;
  78.         }
  79.     };

  80.     @Override
  81.     protected void onCreate(Bundle savedInstanceState) {
  82.         super.onCreate(savedInstanceState);

  83.         setContentView(R.layout.activity_fullscreen_zz);

  84.         mVisible = true;
  85.         mControlsView = findViewById(R.id.fullscreen_content_controls);
  86.         mContentView = findViewById(R.id.fullscreen_content);


  87.         // Set up the user interaction to manually show or hide the system UI.
  88.         mContentView.setOnClickListener(new View.OnClickListener() {
  89.             @Override
  90.             public void onClick(View view) {
  91.                 toggle();
  92.             }
  93.         });

  94.         // Upon interacting with UI controls, delay any scheduled hide()
  95.         // operations to prevent the jarring behavior of controls going away
  96.         // while interacting with the UI.
  97.         findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
  98.     }

  99.     @Override
  100.     protected void onPostCreate(Bundle savedInstanceState) {
  101.         super.onPostCreate(savedInstanceState);

  102.         // Trigger the initial hide() shortly after the activity has been
  103.         // created, to briefly hint to the user that UI controls
  104.         // are available.
  105.         delayedHide(100);
  106.     }

  107.     private void toggle() {
  108.         if (mVisible) {
  109.             hide();
  110.         } else {
  111.             show();
  112.         }
  113.     }

  114.     private void hide() {
  115.         // Hide UI first
  116.         ActionBar actionBar = getSupportActionBar();
  117.         if (actionBar != null) {
  118.             actionBar.hide();
  119.         }
  120.         mControlsView.setVisibility(View.GONE);
  121.         mVisible = false;

  122.         // Schedule a runnable to remove the status and navigation bar after a delay
  123.         mHideHandler.removeCallbacks(mShowPart2Runnable);
  124.         mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
  125.     }

  126.     @SuppressLint("InlinedApi")
  127.     private void show() {
  128.         // Show the system bar
  129.         mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  130.                 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
  131.         mVisible = true;

  132.         // Schedule a runnable to display UI elements after a delay
  133.         mHideHandler.removeCallbacks(mHidePart2Runnable);
  134.         mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
  135.     }

  136.     /**
  137.      * Schedules a call to hide() in delay milliseconds, canceling any
  138.      * previously scheduled calls.
  139.      */
  140.     private void delayedHide(int delayMillis) {
  141.         mHideHandler.removeCallbacks(mHideRunnable);
  142.         mHideHandler.postDelayed(mHideRunnable, delayMillis);
  143.     }
  144. }
复制代码
回复

使用道具 举报

104

主题

171

帖子

1188

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1188
沙发
 楼主| 发表于 2019-5-22 18:37:59 | 只看该作者
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#0099cc"
  6.     tools:context="com.example.administrator.myapplication9.FullscreenActivity">

  7.     <!-- The primary full-screen view. This can be replaced with whatever view
  8.          is needed to present your content, e.g. VideoView, SurfaceView,
  9.          TextureView, etc. -->
  10.     <TextView
  11.         android:id="@+id/fullscreen_content"
  12.         android:layout_width="match_parent"
  13.         android:layout_height="match_parent"
  14.         android:gravity="center"
  15.         android:keepScreenOn="true"
  16.         android:text="@string/dummy_content"
  17.         android:textColor="#33b5e5"
  18.         android:textSize="50sp"
  19.         android:textStyle="bold" />

  20.     <!-- This FrameLayout insets its children based on system windows using
  21.          android:fitsSystemWindows. -->
  22.     <FrameLayout
  23.         android:layout_width="match_parent"
  24.         android:layout_height="match_parent"
  25.         android:fitsSystemWindows="true">

  26.         <LinearLayout
  27.             android:id="@+id/fullscreen_content_controls"
  28.             style="?metaButtonBarStyle"
  29.             android:layout_width="match_parent"
  30.             android:layout_height="wrap_content"
  31.             android:layout_gravity="bottom|center_horizontal"
  32.             android:background="@color/black_overlay"
  33.             android:orientation="horizontal"
  34.             tools:ignore="UselessParent">

  35.             <Button
  36.                 android:id="@+id/dummy_button"
  37.                 style="?metaButtonBarButtonStyle"
  38.                 android:layout_width="0dp"
  39.                 android:layout_height="wrap_content"
  40.                 android:layout_weight="1"
  41.                 android:text="@string/dummy_button" />

  42.         </LinearLayout>
  43.     </FrameLayout>

  44. </FrameLayout>
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|nagomes  

GMT+8, 2025-6-29 07:29 , Processed in 0.057282 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表