Search code examples
javaandroidcanvassurfaceviewsurfaceholder

SurfaceView Example


So I've spent about two days trying to get a working SurfaceView. Tutorials I am following online aren't working even when followed to the letter. I normally get an entirely black screen.

In order to help teach myself how it works I need a working SurfaceView program.

I'm looking for a program that has the SurfaceView generated in a separate class. I would be very grateful if someone is able to post full code (XML and Java) for a SurfaceView program that simply turns the entire screen Red or White.

Thank you for any help!

(Any explanations along with the code would be amazing!)


Solution

  • Try this link

    Ansroid SurfaceView Example

    I followed this tutorial example. It works fine.

    Edit

    Simple Code for SurfaceView

    Layout xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
    

    Java Activity Code

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.SurfaceView;
    import android.view.ViewGroup;
    import android.view.WindowManager;
    
    public class Main2Activity extends AppCompatActivity{
    
    
      SurfaceView surfaceView;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        //Method for making the activity full screen
        //With SurfaceView
        makeItFullScreen();
      }
      
      private void makeItFullScreen(){
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
        //Changing SurfaceView background color
        surfaceView.setBackgroundColor(Color.RED);
    
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    
        ViewGroup.LayoutParams videoLayoutParams = surfaceView.getLayoutParams();
        videoLayoutParams.width = displayMetrics.widthPixels;
        videoLayoutParams.height = displayMetrics.heightPixels;
    
        ViewGroup.LayoutParams videoParams = surfaceView.getLayoutParams();
        videoParams.width = displayMetrics.widthPixels;
        videoParams.height = displayMetrics.heightPixels;
      }
    
    }
    

    Edit2

    If you use custom SurfaceView xml will be like this..

    <customClassPackageName.CustomSurfaceViewClassName
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    

    Code inside Activity

      .......
      customClassPackageName.CustomSurfaceViewClassName surfaceView;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    
        surfaceView = (customClassPackageName.CustomSurfaceViewClassName) findViewById(R.id.surfaceView);
        .......