我创建的相机活动不是透明的。没有显示ListView,我如何使活动透明,以便ListView可见?

时间:2020-09-20 00:41:33

标签: java android

我用带滤镜的相机制作了一个应用程序,滤镜名称在“ ListView”中,它应该出现在相机的左侧,但它不在相机的左侧,因为当我单击该区域时,滤镜工作,但我看不到或阅读过滤器的列表视图,因为它们看不见,它们可以工作,但它们是不可见的。我该怎么办?我该如何解决这个问题?

这是相机的布局:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:keepScreenOn="true">


    <ImageView
        android:id="@+id/cross"
        android:layout_width="43dp"
        android:layout_height="43dp"
        android:visibility="visible"
        android:contentDescription="@string/cross"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_cross" />


    <ListView
        android:id="@+id/filter_list"
        android:layout_width="145dp"
        android:layout_height="615dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_record"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </ListView>



    <com.anisacoding.expriment.VideoEditorFolder.widget.PortraitFrameLayout
        android:id="@+id/wrap_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">


    </com.anisacoding.expriment.VideoEditorFolder.widget.PortraitFrameLayout>

    <ImageView
        android:id="@+id/btn_record"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:contentDescription="@string/record_Video"
        android:text="@string/app_record"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.454"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.926"
        app:srcCompat="@drawable/ic_btn_record" />

    <ImageView
        android:id="@+id/btn_switch_camera"
        android:layout_width="71dp"
        android:layout_height="47dp"
        android:text="@string/SWITCH"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.91"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.052"
        app:srcCompat="@drawable/ic_switch"
        android:contentDescription="@string/SWITCH" />

    <ImageView
        android:id="@+id/btn_flash"
        android:layout_width="51dp"
        android:layout_height="40dp"
        android:contentDescription="@string/flash"
        android:text="@string/flash"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.95"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.2"
        app:srcCompat="@drawable/ic_flash" />

    <ImageView
        android:id="@+id/pause_video"
        android:layout_width="90dp"
        android:layout_height="54dp"
        android:contentDescription="@string/record_Video"
        android:text="@string/pause"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.454"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.926"
        android:color="#FF0000"
        app:srcCompat="@drawable/ic_stop" />

    <ImageView
        android:id="@+id/music_library"
        android:layout_width="52dp"
        android:layout_height="47dp"
        android:contentDescription="@string/library_music"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.45"
        app:layout_constraintLeft_toLeftOf="@+id/btn_record"
        app:layout_constraintStart_toStartOf="@+id/guideline18"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.926"
        app:srcCompat="@drawable/ic_music_golibrary" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.7347932" />


    <TextView
        android:id="@+id/timer2"
        android:layout_width="53dp"
        android:layout_height="43dp"
        android:layout_marginStart="25dp"
        android:layout_marginTop="25dp"
        android:text="@string/_2_m"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="#Ff0000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline18"
        app:layout_constraintTop_toBottomOf="@+id/btn_flash" />


</androidx.constraintlayout.widget.ConstraintLayout>

清单:

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

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.hardware.camera"/>
    <uses-permission android:name="android.hardware.camera.autofocus"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme"
        >

        <activity android:name=".Activities.Dashboard" android:screenOrientation="nosensor">



        <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".VideoEditorFolder.PortraitCameraActivity" android:screenOrientation="portrait"/>
        <activity android:name=".VideoEditorFolder.CameraSelectActivity" android:screenOrientation="portrait"/>
        <activity android:name=".Activities.SoundActivity" android:screenOrientation="nosensor"/>
        <activity android:name=".VideoEditorFolder.BaseCameraActivity" android:screenOrientation="portrait"/>
        <activity android:name=".VideoEditorFolder.LandscapeCameraActivity" android:screenOrientation="landscape"/>
        <activity android:name=".Activities.Home" android:screenOrientation="nosensor"/>

    </application>

</manifest>

相机类别:

public class BaseCameraActivity extends AppCompatActivity {
    private SampleCameraGLView sampleGLView;
    protected GPUCameraRecorder GPUCameraRecorder;
    private String filepath;
    private ImageView recordBtn, pause_video, btn_flash, btn_switch_camera;
    protected LensFacing lensFacing = LensFacing.BACK;
    protected int cameraWidth = 1280;
    protected int cameraHeight = 720;
    protected int videoWidth = 720;
    protected int videoHeight = 720;
    private boolean toggleClick = false;

    private ImageView music_library;
    private ListView filter_list;
    private TextView label;
    private ImageView cross;
    private String sound_url = null, sound_title = null;
    private MediaPlayer mp;

    protected void onCreateActivity() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // Objects.requireNonNull(getSupportActionBar()).hide();

        recordBtn = findViewById(R.id.btn_record);
        pause_video = findViewById(R.id.pause_video);
        btn_flash = findViewById(R.id.btn_flash);
        music_library = findViewById(R.id.music_library);
        label = findViewById(R.id.label);

        sound_url = getIntent().getStringExtra("sound url");
        sound_title = getIntent().getStringExtra("sound title");

        if (sound_url != null) {
            Toast.makeText(this, sound_url, Toast.LENGTH_SHORT).show();
        }

        music_library.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(BaseCameraActivity.this, SoundActivity.class);
                Animatoo.animateCard(BaseCameraActivity.this);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();

            }
        });


        cross = findViewById(R.id.cross);
        cross.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (GPUCameraRecorder != null) {
                    GPUCameraRecorder.stop();
                    GPUCameraRecorder.release();
                    GPUCameraRecorder = null;
                }

                Intent intent = new Intent(BaseCameraActivity.this, Dashboard.class);
                startActivity(intent);
                finish();
            }
        });

        recordBtn.setOnClickListener(v -> {
            filepath = getVideoFilePath();
            GPUCameraRecorder.start(filepath);
            recordBtn.setVisibility(View.GONE);
            pause_video.setVisibility(View.VISIBLE);
            filter_list.setVisibility(View.GONE);
            Toast.makeText(this, "Recording Started", Toast.LENGTH_SHORT).show();
            Glide.with(this).load(R.drawable.ic_pause_circle).into(pause_video);

            ///////////////play sound///////////////
            if (sound_url != null) {
                try {
                    mp = new MediaPlayer();
                    mp.setDataSource(sound_url);
                    mp.prepare();
                    mp.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


            //////stop recording in 2 m////////

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(BaseCameraActivity.this, "Recording Stopped", Toast.LENGTH_SHORT).show();
                    Toast.makeText(BaseCameraActivity.this, "You can only record 1 minute video", Toast.LENGTH_SHORT).show();

                    GPUCameraRecorder.stop();
                    Toast.makeText(BaseCameraActivity.this, "Recording Stopped", Toast.LENGTH_SHORT).show();
                    Toast.makeText(BaseCameraActivity.this, getVideoFilePath(), Toast.LENGTH_SHORT).show();
                    recordBtn.setVisibility(View.VISIBLE);
                    filter_list.setVisibility(View.VISIBLE);
                    music_library.setVisibility(View.VISIBLE);
                    pause_video.setVisibility(View.GONE);


                }
            }, 120000);

            ///////////////////////////////////////////


        });

        pause_video.setOnClickListener(v -> {
            GPUCameraRecorder.stop();
            Toast.makeText(this, "Recording stopped", Toast.LENGTH_SHORT).show();
            recordBtn.setVisibility(View.VISIBLE);
            pause_video.setVisibility(View.GONE);
            filter_list.setVisibility(View.VISIBLE);

            //////////stop music/////////
            if (sound_url != null) {
                try {
                    mp.stop();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }
            }

        });

        findViewById(R.id.btn_flash).setOnClickListener(v -> {
            if (GPUCameraRecorder != null && GPUCameraRecorder.isFlashSupport()) {
                GPUCameraRecorder.switchFlashMode();
                GPUCameraRecorder.changeAutoFocus();
            }
        });
        findViewById(R.id.btn_switch_camera).setOnClickListener(v -> {
            releaseCamera();
            if (lensFacing == LensFacing.BACK) {
                lensFacing = LensFacing.FRONT;
            } else {
                lensFacing = LensFacing.BACK;
            }
            toggleClick = true;
        });

        filter_list = findViewById(R.id.filter_list);
        final List<FilterType> filterTypes = FilterType.createFilterList();
        filter_list.setAdapter(new FilterAdapter(this, R.id.label, filterTypes).whiteMode());
        filter_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (GPUCameraRecorder != null) {
                    GPUCameraRecorder.setFilter(FilterType.createGlFilter(filterTypes.get(position), getApplicationContext()));
                    filter_list.setVisibility(View.VISIBLE);

                }

            }

        });


    }

    private void init() {


        if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
            setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
        }
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_FULLSCREEN);
        }
        if (Build.VERSION.SDK_INT >= 21) {
            setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

    }




        @Override
    protected void onResume() {
        super.onResume();
        setUpCamera();
    }
    @Override
    protected void onStop() {
        super.onStop();
        releaseCamera();
    }
    private void releaseCamera() {
        if (sampleGLView != null) {
            sampleGLView.onPause();
        }
        if (GPUCameraRecorder != null) {
            GPUCameraRecorder.stop();
            GPUCameraRecorder.release();
            GPUCameraRecorder = null;
        }
        if (sampleGLView != null) {
            ((FrameLayout) findViewById(R.id.wrap_view)).removeView(sampleGLView);
            sampleGLView = null;
        }
    }
    private void setUpCameraView() {
        runOnUiThread(() -> {
            FrameLayout frameLayout = findViewById(R.id.wrap_view);
            frameLayout.removeAllViews();
            sampleGLView = null;
            sampleGLView = new SampleCameraGLView(getApplicationContext());
            sampleGLView.setTouchListener((event, width, height) -> {
                if (GPUCameraRecorder == null) return;
                GPUCameraRecorder.changeManualFocusPoint(event.getX(), event.getY(), width, height);
            });
            frameLayout.addView(sampleGLView);
        });
    }
    private void setUpCamera() {
        setUpCameraView();
        GPUCameraRecorder = new GPUCameraRecorderBuilder(this, sampleGLView)
                //.recordNoFilter(true)
                .cameraRecordListener(new CameraRecordListener() {
                    @Override
                    public void onGetFlashSupport(boolean flashSupport) {
                        runOnUiThread(() -> {
                            findViewById(R.id.btn_flash).setEnabled(flashSupport);
                        });
                    }
                    @Override
                    public void onRecordComplete() {
                        exportMp4ToGallery(getApplicationContext(), filepath);
                    }
                    @Override
                    public void onRecordStart() {
                        runOnUiThread(() -> {
                            filter_list.setVisibility(View.GONE);
                        });
                    }
                    @Override
                    public void onError(Exception exception) {
                        Log.e("GPUCameraRecorder", exception.toString());
                    }
                    @Override
                    public void onCameraThreadFinish() {
                        if (toggleClick) {
                            runOnUiThread(() -> {
                                setUpCamera();
                            });
                        }
                        toggleClick = false;
                    }
                    @Override
                    public void onVideoFileReady() {
                    }
                })
                .videoSize(videoWidth, videoHeight)
                .cameraSize(cameraWidth, cameraHeight)
                .lensFacing(lensFacing)
                .build();
    }

    public void backBtn (View view){
        Intent intent = new Intent(BaseCameraActivity.this, Dashboard.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        Animatoo.animateInAndOut(this);
        finish();
    }


    @Override
    public void onBackPressed () {
        Intent intent = new Intent(BaseCameraActivity.this, Dashboard.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        Animatoo.animateInAndOut(this);
        finish();
    }


    //    private void changeFilter(Filters filters) {
//        GPUCameraRecorder.setFilter(Filters.getFilterInstance(filters, getApplicationContext()));
//    }
    private interface BitmapReadyCallbacks {
        void onBitmapReady(Bitmap bitmap);
    }
    private void captureBitmap(final BitmapReadyCallbacks bitmapReadyCallbacks) {
        sampleGLView.queueEvent(() -> {
            EGL10 egl = (EGL10) EGLContext.getEGL();
            GL10 gl = (GL10) egl.eglGetCurrentContext().getGL();
            Bitmap snapshotBitmap = createBitmapFromGLSurface(sampleGLView.getMeasuredWidth(), sampleGLView.getMeasuredHeight(), gl);
            runOnUiThread(() -> {
                bitmapReadyCallbacks.onBitmapReady(snapshotBitmap);
            });
        });
    }
    private Bitmap createBitmapFromGLSurface(int w, int h, GL10 gl) {
        int[] bitmapBuffer = new int[w * h];
        int[] bitmapSource = new int[w * h];
        IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
        intBuffer.position(0);
        try {
            gl.glReadPixels(0, 0, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
            int offset1, offset2, texturePixel, blue, red, pixel;
            for (int i = 0; i < h; i++) {
                offset1 = i * w;
                offset2 = (h - i - 1) * w;
                for (int j = 0; j < w; j++) {
                    texturePixel = bitmapBuffer[offset1 + j];
                    blue = (texturePixel >> 16) & 0xff;
                    red = (texturePixel << 16) & 0x00ff0000;
                    pixel = (texturePixel & 0xff00ff00) | red | blue;
                    bitmapSource[offset2 + j] = pixel;
                }
            }
        } catch (GLException e) {
            Log.e("CreateBitmap", "createBitmapFromGLSurface: " + e.getMessage(), e);
            return null;
        }
        return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
    }
    public void saveAsPngImage(Bitmap bitmap, String filePath) {
        try {
            File file = new File(filePath);
            FileOutputStream outStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void exportMp4ToGallery(Context context, String filePath) {
        final ContentValues values = new ContentValues(2);
        values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
        values.put(MediaStore.Video.Media.DATA, filePath);
        context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                values);
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.parse("file://" + filePath)));
    }
    public static String getVideoFilePath() {
        return getAndroidMoviesFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "GPUCameraRecorder.mp4";
    }
    public static File getAndroidMoviesFolder() {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    }
    private static void exportPngToGallery(Context context, String filePath) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(filePath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        context.sendBroadcast(mediaScanIntent);
    }
    public static String getImageFilePath() {
        return getAndroidImageFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "GPUCameraRecorder.png";
    }
    public static File getAndroidImageFolder() {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    }
}

样式:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#49464E</item>
        <item name="colorPrimaryDark">#6C6971</item>
        <item name="colorAccent">@color/colorAccent</item>

    </style>

    <style name="ServiceTextShadow">

        <item name="android:shadowColor">#55000000</item>
        <item name="android:shadowDx">0</item>
        <item name="android:shadowDy">0</item>
        <item name="android:shadowRadius">10.0</item>


    </style>

</resources>

文本布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    style="@style/ServiceTextShadow"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:padding="12dp"
    android:textColor="@android:color/white"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:textSize="12sp" />

过滤器适配器:

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.anisacoding.expriment.R;

import java.util.List;
public class FilterAdapter extends ArrayAdapter<FilterType> {
    static class ViewHolder {
        public TextView text;
    }
    private final Context context;
    private final List<FilterType> values;
    private boolean isWhite = false;
    public FilterAdapter(Context context, int resource, List<FilterType> objects) {
        super(context, resource, objects);
        this.context = context;
        values = objects;
    }
    @SuppressLint("InflateParams")
    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        View rowView = convertView;
        // reuse views
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (isWhite) {
                rowView = inflater.inflate(R.layout.row_white_text, null);
            } else {
                rowView = inflater.inflate(R.layout.row_text, null);
            }
            // configure view holder
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = rowView.findViewById(R.id.label);
            rowView.setTag(viewHolder);
        }
        ViewHolder holder = (ViewHolder) rowView.getTag();
        String s = values.get(position).name();
        holder.text.setText(s);
        return rowView;
    }
    public FilterAdapter whiteMode() {
        isWhite = true;
        return this;
    }

}

0 个答案:

没有答案
相关问题