Android開發(fā)實(shí)現(xiàn)多進(jìn)程彈窗效果
安卓開發(fā)之多進(jìn)程彈窗,供大家參考,具體內(nèi)容如下
背景
有時(shí)在彈窗繪圖時(shí),需要彈窗在新的進(jìn)程中,以保證在彈窗繪圖的過程中不會占用過多的內(nèi)存導(dǎo)致主進(jìn)程被關(guān)。
代碼實(shí)現(xiàn)
子進(jìn)程彈窗
首先我們需要一個(gè)透明的activity來作為彈窗展示,并且這個(gè)透明activity就存在于子進(jìn)程中,這一切都可以在清單文件中實(shí)現(xiàn):
<activity android:name=".ProcessActivity" android:process=":process_test" android:theme="@style/TranslucentStyle" />
使用到的主題定義在res/values/themes.xml中:
<style name="TranslucentStyle" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowBackground">@android:color/transparent</item> <!-- 背景色透明 --> <item name="android:windowIsTranslucent">true</item> <!-- 是否有透明屬性 --> <item name="android:backgroundDimEnabled">false</item> <!-- 背景是否半透明 --> <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> <!-- activity窗口切換效果 --> </style>
而后設(shè)置activity的位置和尺寸:
public class ProcessActivity extends Activity { ... @SuppressLint("ClickableViewAccessibility") @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_process); ... WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.width = 950; lp.height = 1700; lp.gravity = Gravity.START; getWindow().setAttributes(lp); ... } ... }
使用到的布局文件activity_process.xml如下所示:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root_process" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/teal_200" android:orientation="vertical"> <Button android:id="@+id/btn_process" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Test sub process" /> <Button android:id="@+id/btn_finish" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="finish sub process" /> </LinearLayout>
背景色為青色,兩個(gè)button,一個(gè)負(fù)責(zé)展示toast,一個(gè)負(fù)責(zé)結(jié)束這個(gè)彈窗,我們在onCreate()中為它們添加點(diǎn)擊事件監(jiān)聽:
Button button_process = findViewById(R.id.btn_process); Button button_finish = findViewById(R.id.btn_finish); button_process.setOnClickListener(v -> { Toast.makeText(this, "onCreate: Button in sub process has been clicked.", Toast.LENGTH_SHORT).show(); }); button_finish.setOnClickListener(v -> { ProcessActivity.this.finish(); });
接下來要實(shí)現(xiàn)的是事件透傳:因?yàn)樽舆M(jìn)程窗口是一個(gè)彈窗,當(dāng)沒有觸摸到彈窗中可點(diǎn)擊組件時(shí),應(yīng)該由下面的activity去承接觸摸事件,這部分邏輯的實(shí)現(xiàn)如下所示:
public class ProcessActivity extends Activity { private View mRootView; @SuppressLint("ClickableViewAccessibility") @Override protected void onCreate(@Nullable Bundle savedInstanceState) { mRootView = LayoutInflater.from(this).inflate(R.layout.activity_process, null); setContentView(mRootView); ... Button button_process = findViewById(R.id.btn_process); Button button_finish = findViewById(R.id.btn_finish); ... } @Override public boolean dispatchTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { View target = Utils.getViewTouchedByEvent(mRootView, event); if (target != null) { target.dispatchTouchEvent(event); return true; } } Intent intent = new Intent(); intent.setAction("TouchEvent"); intent.putExtra("event", event); sendBroadcast(intent); return super.dispatchTouchEvent(event); } }
因?yàn)閺棿按翱诤椭鞔翱谖挥趦蓚€(gè)進(jìn)程中,因此觸摸事件的傳遞需要用IPC方式,這里采用的是廣播。Utils.isDebugWindowValidTouched()負(fù)責(zé)判斷當(dāng)前點(diǎn)擊事件是否點(diǎn)到了某個(gè)可點(diǎn)擊的控件,方法代碼如下:
public static View getViewTouchedByEvent(View view, MotionEvent event) { if (view == null || event == null) { return null; } if (!(view instanceof ViewGroup)) { return isDebugWindowValidTouched(view, event) ? view : null; } ViewGroup parent = ((ViewGroup) view); int childrenCount = parent.getChildCount(); for (int i = 0; i < childrenCount; i++) { View target = getViewTouchedByEvent(parent.getChildAt(i), event); if (target != null) { return target; } } return null; } private static boolean isDebugWindowValidTouched(View view, MotionEvent event) { if (event == null || view == null) { return false; } if (view.getVisibility() != View.VISIBLE) { return false; } final float eventRawX = event.getRawX(); // 獲取event在屏幕上的坐標(biāo) final float eventRawY = event.getRawY(); RectF rect = new RectF(); int[] location = new int[2]; view.getLocationOnScreen(location); // 獲取view在屏幕上的坐標(biāo)位置 float x = location[0]; float y = location[1]; rect.left = x; rect.right = x + view.getWidth(); rect.top = y; rect.bottom = y + view.getHeight(); return rect.contains(eventRawX, eventRawY); }
子進(jìn)程彈窗窗口ProcessActivity的完整代碼如下所示:
package com.example.testrxjava; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Process; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Button; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; public class ProcessActivity extends Activity { public static final String TAG = "ProcessActivity"; private View mRootView; @SuppressLint("ClickableViewAccessibility") @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mRootView = LayoutInflater.from(this).inflate(R.layout.activity_process, null); setContentView(mRootView); Log.i(TAG, "onCreate: pid = " + Process.myPid()); Button button_process = findViewById(R.id.btn_process); TextView button_finish = findViewById(R.id.btn_finish); button_process.setOnClickListener(v -> { Toast.makeText(this, "onCreate: Button in sub process has been clicked.", Toast.LENGTH_SHORT).show(); }); button_finish.setOnClickListener(v -> { ProcessActivity.this.finish(); }); ToggleButton toggleButton = findViewById(R.id.toggle); toggleButton.setOnCheckedChangeListener((buttonView, isChecked) -> Toast.makeText(ProcessActivity.this, "Toggle button in sub process has been clicked, current state of checking is: " + isChecked, Toast.LENGTH_SHORT).show()); Switch switch_button = findViewById(R.id.switch_sub_process); switch_button.setOnCheckedChangeListener((buttonView, isChecked) -> Toast.makeText(ProcessActivity.this, "Switch in sub process has been clicked, current state of checking is: " + isChecked, Toast.LENGTH_SHORT).show()); WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.width = 950; lp.height = 1700; lp.gravity = Gravity.START; getWindow().setAttributes(lp); } @Override public boolean dispatchTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { View target = Utils.getViewTouchedByEvent(mRootView, event); if (target != null) { target.dispatchTouchEvent(event); return true; } } Intent intent = new Intent(); intent.setAction("TouchEvent"); intent.putExtra("event", event); sendBroadcast(intent); return super.dispatchTouchEvent(event); } }
主界面
回到主界面,首先需要接收一下TouchEvent這個(gè)廣播:
public class MainActivity extends AppCompatActivity { ... private BroadcastReceiver mReceiver; @Override protected void onCreate(Bundle savedInstanceState) { ... mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("TouchEvent")) { MotionEvent event = intent.getParcelableExtra("event"); try { Class popupClass = Class.forName("android.widget.PopupWindow"); Field decorViewField = popupClass.getDeclaredField("mDecorView"); decorViewField.setAccessible(true); Object decorView = decorViewField.get(window); Method dispatchTouchEvent = decorView.getClass().getDeclaredMethod("dispatchTouchEvent", MotionEvent.class); dispatchTouchEvent.invoke(decorView, event); } catch (Exception e) { e.printStackTrace(); } } } }; IntentFilter filter = new IntentFilter("TouchEvent"); registerReceiver(mReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver); } }
因?yàn)橹鹘缑嬷幸灿幸粋€(gè)彈窗,因此當(dāng)觸摸事件從子進(jìn)程傳過來的時(shí)候,需要主進(jìn)程的彈窗去處理,因此在onReceive()方法中通過反射執(zhí)行了主進(jìn)程彈窗的mDecorView的dispatchTouchEvent()方法去傳遞觸摸事件,MainActivity的完整代碼如下所示:
package com.example.testrxjava; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.RectF; import android.os.Bundle; import android.os.Parcelable; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Button mButton; private Button mHide; private BroadcastReceiver mReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.text_view); ListView listView = findViewById(R.id.list); ArrayList<String> list = new ArrayList<>(); for (int i = 0; i < 200; i++) { list.add("No." + i); } MyAdapter adapter = new MyAdapter(list, this); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); PopupWindow window = new PopupWindow(this); View windowView = LayoutInflater.from(this).inflate(R.layout.window_layout, null); mButton = windowView.findViewById(R.id.btn_window); mButton.setOnClickListener(view -> { startActivity(new Intent(MainActivity.this, ProcessActivity.class)); }); mHide = windowView.findViewById(R.id.btn_hide); mHide.setOnClickListener(v -> { mButton.setVisibility(mButton.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE); }); window.setTouchInterceptor((view, motionEvent) -> { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { View target = Utils.getViewTouchedByEvent(windowView, motionEvent); if (target != null) { target.dispatchTouchEvent(motionEvent); return true; } } MainActivity.this.dispatchTouchEvent(motionEvent); return false; }); View rootView = getWindow().getDecorView(); window.setOutsideTouchable(false); window.setOnDismissListener(() -> textView.post(() -> { window.showAtLocation(rootView, Gravity.BOTTOM, 0, 0); })); window.setContentView(windowView); window.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); window.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); findViewById(R.id.root).setOnClickListener(v -> { Log.i("MainActivity", "Touch event gets to text view!"); }); textView.post(() -> { window.showAtLocation(rootView, Gravity.BOTTOM, 0, 0); }); mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("TouchEvent")) { MotionEvent event = intent.getParcelableExtra("event"); try { Class popupClass = Class.forName("android.widget.PopupWindow"); Field decorViewField = popupClass.getDeclaredField("mDecorView"); decorViewField.setAccessible(true); Object decorView = decorViewField.get(window); Method dispatchTouchEvent = decorView.getClass().getDeclaredMethod("dispatchTouchEvent", MotionEvent.class); dispatchTouchEvent.invoke(decorView, event); } catch (Exception e) { e.printStackTrace(); } } } }; IntentFilter filter = new IntentFilter("TouchEvent"); registerReceiver(mReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver); } }
效果
背景紫色的是主進(jìn)程的彈窗,青色的是子進(jìn)程的彈窗。從錄像中可以看到,當(dāng)按下事件按到位于上層的組件時(shí),上層的組件會響應(yīng);如果按到了上層彈窗的空白處,觸摸事件則會向下傳遞。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android屏幕鎖屏彈窗的正確姿勢DEMO詳解
- Android仿支付寶支付從底部彈窗效果
- Android實(shí)現(xiàn)氣泡布局/彈窗效果 氣泡尖角方向及偏移量可控
- Android監(jiān)聽輸入法彈窗和關(guān)閉的實(shí)現(xiàn)方法
- Android仿支付寶微信支付密碼界面彈窗封裝dialog
- Android如何實(shí)現(xiàn)鎖屏狀態(tài)下彈窗
- Android開發(fā)實(shí)現(xiàn)仿京東商品搜索選項(xiàng)卡彈窗功能
- Android自定義彈窗提醒控件使用詳解
- Android實(shí)現(xiàn)彈窗進(jìn)度條效果
- Android UI設(shè)計(jì)之AlertDialog彈窗控件
相關(guān)文章
Retrofit2.0添加Header的方法總結(jié)(推薦)
這篇文章主要介紹了Retrofit2.0添加Header的方法總結(jié)(推薦),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09Android實(shí)現(xiàn)圖片隨手指旋轉(zhuǎn)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片隨手指旋轉(zhuǎn)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android?Flutter實(shí)現(xiàn)彈簧動畫交互的示例詳解
物理模擬可以讓應(yīng)用程序的交互感覺逼真和互動,本文章實(shí)現(xiàn)了演示了如何使用彈簧模擬將小部件從拖動的點(diǎn)移回中心,感興趣的可以了解一下2023-04-04Android版微信跳一跳小游戲利用技術(shù)手段達(dá)到高分的操作方法
朋友圈到處都是曬微信跳一跳小游戲的,很多朋友能達(dá)到二三百分了。下面小編給大家分享Android版微信跳一跳小游戲利用技術(shù)手段達(dá)到高分的操作方法,需要的朋友一起看看吧2018-01-01Android學(xué)習(xí)筆記之藍(lán)牙功能
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)筆記之藍(lán)牙功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android中將Bitmap對象以PNG格式保存在內(nèi)部存儲中的方法
在Android中進(jìn)行圖像處理的任務(wù)時(shí),有時(shí)我們希望將處理后的結(jié)果以圖像文件的格式保存在內(nèi)部存儲空間中,本文以此為目的,介紹將Bitmap對象的數(shù)據(jù)以PNG格式保存下來的方法2017-08-08Android 避免APP啟動閃黑屏的解決辦法(Theme和Style)
閃黑屏的原因主要是我們啟動Activity的時(shí)候,需要跑完onCreate和onResume才會顯示界面2013-07-07關(guān)于Android輸入法彈窗bug的優(yōu)雅處理
在Android應(yīng)用中,當(dāng)跳轉(zhuǎn)到某個(gè)Activity時(shí),該Activity顯示頁面的EditText獲得焦點(diǎn),在某些機(jī)器中會觸發(fā)軟鍵盤的自動彈出,這篇文章主要給大家介紹了關(guān)于Android輸入法彈窗bug的優(yōu)雅處理,需要的朋友可以參考下2021-10-10