android7/8新特性 畫中畫、shortcut和分屏模式


多窗口

在android7.0中原生提供了多窗口模式和畫中畫模式,多窗口模式將屏幕分為上下或左右兩塊區域分別顯示兩個應用,畫中畫模式主要應用在android TV中,類似於windows中的多窗口。

分屏

實現分屏功能只需要在AndroidManifest.xml中為application或特定的activity添加以下屬性即可


android:resizeableActivity="true"

畫中畫

和分屏實現方式相同,只是需要設備支持,目前只是在android TV中有此功能,手機並沒有開啟該功能,需要進行特殊設置。實現方法也是在AndroidManifest.xml中為application或特定的activity添加以下屬性

android:supportsPictureInPicture="true"

demo:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".TestPIPActivity"
            android:supportsPictureInPicture="true"
          
            ></activity>
        <activity android:name=".MainActivity"
            android:resizeableActivity="true"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
import android.annotation.TargetApi;
import android.app.PictureInPictureParams;
import android.content.res.Configuration;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Rational;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
// 畫中畫 test
public class TestPIPActivity extends AppCompatActivity {
    private static final String TAG = "TestPIPActivity";
    private PictureInPictureParams.Builder mPictureInPictureParamsBuilder;

    @TargetApi(Build.VERSION_CODES.O)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout content = new FrameLayout(this);
        setContentView(content,new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        if(Build.VERSION.SDK_INT == Build.VERSION_CODES.O){
            mPictureInPictureParamsBuilder = new PictureInPictureParams.Builder();

            final TextView textView = new TextView(this);
            textView.setText("test PIP");
            textView.setTextSize(20);
            FrameLayout.LayoutParams fl = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            fl.gravity = Gravity.CENTER ;
            textView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {//主要操作
                    Rational aspectRatio = new Rational(10,10);
                    mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
                    enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
                }
            });
            content.addView(textView,fl);

        }else{
            TextView descTv = new TextView(this);
            descTv.setText("當前版本不支持...");
            descTv.setTextSize(20);
            FrameLayout.LayoutParams Tvfl = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            Tvfl.gravity = Gravity.CENTER ;
            content.addView(descTv,Tvfl);
        }

    }



    @Override
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
        super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
        Log.d(TAG,String.valueOf(isInPictureInPictureMode));
    }

}

shortcut

android7.1中加入的shortcut類似於iOS的3D touch,只是將iPhone上的重按替換成了長按,長按后會彈出菜單,提供快捷操作,shortcut分為靜態配置和動態配置。

靜態配置

靜態配置一般用於固定的選項,需要在資源文件重定義。在res下建立xml目錄並新建shortcuts.xml文件,這里面定義shortcut的顯示內容和跳轉intent,代碼如下

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="static_1"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/static_shortcut_short_name_1"
        android:shortcutLongLabel="@string/static_shortcut_long_name_1" >
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.myapplication"
            android:targetClass="com.example.myapplication.TestPIPActivity" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>
  • id: shortcut的id,必須惟一,否則后面的會覆蓋前面的
  • longLabel: 顯示在彈出菜單中的label
  • shortLabel: 通過拖拽顯示在桌面上的label
  • icon: 一個選項對應的icon
  • intent: 跳轉intent,必須設置,否則會拋出java.lang.NullPointerException: Shortcut Intent must be provided的異常
之后還要在AndroidManifest中的activity中添加meta-data標簽,代碼如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".TestPIPActivity"
            android:supportsPictureInPicture="true"

            ></activity>
        <activity android:name=".MainActivity"
            android:resizeableActivity="true"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts"/>
        </activity>
    </application>

</manifest>

這樣就可以通過資源文件的配置生成shortcut。需要注意的是,shortcut的數量並不能無限增加,當數量超過4時就會拋出如下異常

動態生成

動態生成shortcut由ShortcutInfo構成,ShortcutInfo有幾個主要屬性與靜態配置相同,下面就通過ShortcutManager動態的生成兩個shortcut

    private void initShortcut() {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "1")
                .setShortLabel("dynamic short 1")
                .setLongLabel("dynamic long 1")
                .setDisabledMessage("disable")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                .setIntent(new Intent(Intent.ACTION_VIEW, Uri.EMPTY, this, MainActivity.class))
                .build();

        ShortcutInfo shortcutInfo1 = new ShortcutInfo.Builder(this, "2")
                .setShortLabel("dynamic short 1")
                .setLongLabel("dynamic long 2")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                .setIntent(new Intent(Intent.ACTION_VIEW, Uri.EMPTY, this, SecondActivity.class))
                .build();

        List<ShortcutInfo> shortcutInfoList = new ArrayList<>();
        shortcutInfoList.add(shortcutInfo);
        shortcutInfoList.add(shortcutInfo1);

        shortcutManager.addDynamicShortcuts(shortcutInfoList);
    }
通過以上兩種方法就可以在android7.1中加入快捷菜單,可以讓用戶在不進入應用時直接進入某個頁面,實現類似iPhone中3D touch的效果。使用時也需要注意,添加數量的上限,將重要的快捷入口添加進來!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM