他の人からより良く学び、
より良いものになりますように。
—— 「ウェイカ・ジーシャン」
この記事の長さは 1839ワードで、読むのにかかる時間は 5分です。
序文
前回の記事「Android ピクチャーインピクチャー (PIP) モードの使用」ではピクチャーインピクチャーの使用法について紹介しましたが、今日の記事では、UI と Android 12 の更新後の互換性の問題を中心に、アクション ボタンの使用法について説明します。放送を通じて。
効果を達成する
コード
マイクロカード志祥
前回の記事のプログラム コードを引き続き踏襲し、ここで処理します。デモのソース コード アドレスは記事の最後に送信されます。
ブロードキャストと定数を定義する
ACTION_TEXTVIEW と ACTION_TOAST は、TextView の表示を更新するためと Toast を使用するための 2 つの定数を定義し、次に、TextView を更新するための、または Toast ポップアップを使用するためのさまざまなアクションを区別するために、BroadcastReceiver でコードを記述する実装を定義します。
private val ACTION_TEXTVIEW = "textview"
private val ACTION_TOAST = "toast"
private val ACTION_VALUE = "value"
//定义广播接收
private var mBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent?.let {
val str = it.getStringExtra(ACTION_VALUE)
when (it.action) {
ACTION_TEXTVIEW -> {
binding.textView.text = str
}
ACTION_TOAST -> {
context?.let { mContext ->
Toast.makeText(mContext, str, Toast.LENGTH_SHORT)
.show()
}
}
else -> {
return@let
}
}
}
}
}
RemoteActionのブロードキャスト送信を実現する
これは主に PendingIntent で実装されており、最初のボタンをクリックしてアプリケーションの全画面に戻る際には PendingIntent.getActivity が使用され、2 番目と 3 番目のボタンはブロードキャストを送信して PendingIntent.getBroadcast を使用するため、ここでは次の関数を記述します。関数のパラメータを通じて、対応する PendingIntent を生成します。
private fun getPendingIntent(intent: Intent, requestcode: Int, flag: Int = 1): PendingIntent {
when (flag) {
1 -> return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getActivity(
this, requestcode,
intent,
PendingIntent.FLAG_IMMUTABLE
)
} else {
PendingIntent.getActivity(
this, requestcode,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
}
else -> return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getBroadcast(
this, requestcode,
intent,
PendingIntent.FLAG_IMMUTABLE
)
} else {
PendingIntent.getBroadcast(
this, requestcode,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
}
}
}
集中:
前回の記事で追加されなかった機能にAndroidのSDK版を追加しました 8.0エミュレータは正常に動作しますが、実機のAndroid 12は直接クラッシュします その理由は、Android 12が特定のPendingIntentオブジェクトが変数であるかどうかを宣言しているためです. の場合は、それぞれ PendingIntent.FLAG_MUTABLE フラグまたは PendingIntent.FLAG_IMMUTABLE フラグを使用します。アプリケーションが変更可能なフラグを設定せずに PendingIntent オブジェクトを作成しようとすると、システムは IllegalArgumentException をスローします。
enterPipModel 関数を再変更し、3 つのボタンを作成するコードを追加します。
private fun enterPipModel() {
val builder = PictureInPictureParams.Builder()
//设置Actions
val actions = arrayListOf<RemoteAction>()
//1.返回全屏窗口
val pIntent1 = getPendingIntent(intent, 1)
val remoteAction1 = RemoteAction(
Icon.createWithResource(
this,
R.mipmap.ic_phone_round
), "画中画", "Vaccae", pIntent1
)
actions.add(remoteAction1)
//2.修改Text显示
val intent2 = Intent(ACTION_TEXTVIEW).putExtra(ACTION_VALUE, "Vaccae点击了Text Action")
val pIntent2 = getPendingIntent(intent2, 2, 2)
actions.add(
RemoteAction(
Icon.createWithResource(
this,
R.mipmap.ic_launcher
), "TextView", "Vaccae", pIntent2
)
)
//3.实现Toast控制
val intent3 = Intent(ACTION_TOAST).putExtra(ACTION_VALUE, "关注微卡智享")
val pIntent3 = getPendingIntent(intent3, 3, 2)
actions.add(
RemoteAction(
Icon.createWithResource(
this,
R.mipmap.ic_launcher
), "Toast", "Vaccae", pIntent3
)
)
builder.setActions(actions)
//设置宽高比例,第一个是分子,第二个是分母,指定宽高比,必须在 2.39:1或1:2.39 之间,否则会抛出IllegalArgumentException异常。
val rational = Rational(5, 11)
builder.setAspectRatio(rational)
//Android12下加入的画中画配置,对于非视频内容停用无缝大小调整
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.setSeamlessResizeEnabled(false)
builder.setAutoEnterEnabled(true)
}
enterPictureInPictureMode(builder.build())
}
ピクチャー・イン・ピクチャーモードの開始時と終了時の放送監視の判定
前回の記事ではonPictureInPictureModeChanged関数を使ってピクチャーインピクチャーモード移行時の判定を行い、ボタンを非表示にし、全画面モードに戻った後に表示する機能を追加しましたが、これに移行時にブロードキャスト受信を登録する関数を追加します。ピクチャー・イン・ピクチャー・モード、およびピクチャー・イン・ピクチャー・モードを終了するとき ブロードキャスト受信を解除するコードは次のとおりです。
override fun onPictureInPictureModeChanged(
isInPictureInPictureMode: Boolean,
newConfig: Configuration?
) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
if (isInPictureInPictureMode) {
binding.textView.text = "画中画模式"
binding.btnpip.visibility = View.GONE
//进入画中画时注册广播接收
val intentFilter = IntentFilter()
intentFilter.addAction(ACTION_TEXTVIEW)
intentFilter.addAction(ACTION_TOAST)
registerReceiver(mBroadcastReceiver, intentFilter)
} else {
binding.textView.text = "正常模式"
binding.btnpip.visibility = View.VISIBLE
//退出画中画时停止广播接收
unregisterReceiver(mBroadcastReceiver)
}
}
場合によっては、ユーザーがホーム画面ボタンまたは最近使用したアプリケーション ボタンを積極的に押すと、ピクチャ イン ピクチャ モードに切り替わることがあり、ここでは onUserLeaveHint メソッドが使用されます。
以上の手順でピクチャインピクチャボタンの機能デモが完了です。
マイクロカード志祥
送信元アドレス
https://github.com/Vaccae/AndroidPipDemo.git
クリックして原文を読むと「Code Cloud」のコードアドレスが表示されます
以上
過去の素晴らしいレビュー
Android ピクチャーインピクチャー (PIP) モードの使用法
C++ OpenCV は透視変換のために画像を手動でインターセプトします