[*] 删除预览视频复用布局

This commit is contained in:
acgist
2023-05-11 08:58:39 +08:00
parent 621c1b0e75
commit 22c6970cbd

View File

@@ -15,6 +15,7 @@ import android.os.Message;
import android.util.Log; import android.util.Log;
import android.view.SurfaceView; import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.GridLayout; import android.widget.GridLayout;
@@ -33,6 +34,8 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.RandomUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
/** /**
@@ -46,6 +49,7 @@ public class MainActivity extends AppCompatActivity {
private MainHandler mainHandler; private MainHandler mainHandler;
private ActivityMainBinding binding; private ActivityMainBinding binding;
private MediaProjectionManager mediaProjectionManager; private MediaProjectionManager mediaProjectionManager;
private List<ViewGroup.LayoutParams> removeLayoutParams;
private ActivityResultLauncher<Intent> activityResultLauncher; private ActivityResultLauncher<Intent> activityResultLauncher;
@Override @Override
@@ -70,6 +74,7 @@ public class MainActivity extends AppCompatActivity {
this.binding.record.setOnClickListener(this::record); this.binding.record.setOnClickListener(this::record);
this.binding.settings.setOnClickListener(this::settings); this.binding.settings.setOnClickListener(this::settings);
this.binding.photograph.setOnClickListener(this::photograph); this.binding.photograph.setOnClickListener(this::photograph);
this.removeLayoutParams = new ArrayList<>();
} }
@Override @Override
@@ -293,18 +298,25 @@ public class MainActivity extends AppCompatActivity {
* *
* @param message 消息 * @param message 消息
*/ */
private synchronized void previewVideo(Message message) { private void previewVideo(Message message) {
final GridLayout video = this.binding.video; synchronized (this) {
final int count = video.getChildCount(); final GridLayout video = this.binding.video;
final GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams( final int count = video.getChildCount();
GridLayout.spec(count / 2, 1.0F), final ViewGroup.LayoutParams layoutParams;
GridLayout.spec(count % 2, 1.0F) if(this.removeLayoutParams.isEmpty()) {
); layoutParams = new GridLayout.LayoutParams(
layoutParams.width = 0; GridLayout.spec(count / 2, 1.0F),
layoutParams.height = 0; GridLayout.spec(count % 2, 1.0F)
final SurfaceView surfaceView = (SurfaceView) message.obj; );
surfaceView.setZ(0F); layoutParams.width = 0;
video.addView(surfaceView, layoutParams); layoutParams.height = 0;
} else {
layoutParams = this.removeLayoutParams.remove(0);
}
final SurfaceView surfaceView = (SurfaceView) message.obj;
surfaceView.setZ(0F);
video.addView(surfaceView, layoutParams);
}
} }
/** /**
@@ -312,14 +324,17 @@ public class MainActivity extends AppCompatActivity {
* *
* @param message 消息 * @param message 消息
*/ */
private synchronized void removePreviewVideo(Message message) { private void removePreviewVideo(Message message) {
final GridLayout video = this.binding.video; synchronized (this) {
final SurfaceView surfaceView = (SurfaceView) message.obj; final GridLayout video = this.binding.video;
final int index = video.indexOfChild(surfaceView); final SurfaceView surfaceView = (SurfaceView) message.obj;
if(index < 0) { final int index = video.indexOfChild(surfaceView);
return; if(index < 0) {
return;
}
video.removeViewAt(index);
this.removeLayoutParams.add(surfaceView.getLayoutParams());
} }
video.removeViewAt(index);
} }
} }