编程语言
首页 > 编程语言> > java – SurfaceView示例

java – SurfaceView示例

作者:互联网

所以我花了大约两天时间尝试使用SurfaceView.我在网上关注的教程即使在信中也没有用.我通常会得到一个完全黑屏.

为了帮助自己教我如何工作,我需要一个有效的SurfaceView程序.

我正在寻找一个在单独的类中生成SurfaceView的程序.如果有人能够为SurfaceView程序发布完整代码(XMLJava),只需将整个屏幕变为红色或白色,我将非常感激.

感谢您的任何帮助!

(任何解释和代码都会很棒!)

解决方法:

试试这个链接

Ansroid SurfaceView Example

我已经按照本教程示例进行操作它工作正常.

编辑

SurfaceView的简单代码

布局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活动代码

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

如果您使用自定义SurfaceView xml将是这样的..

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

活动内的代码

  .......
  customClassPackageName.CustomSurfaceViewClassName surfaceView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    surfaceView = (customClassPackageName.CustomSurfaceViewClassName) findViewById(R.id.surfaceView);
    .......

标签:java,android,canvas,surfaceview,surfaceholder
来源: https://codeday.me/bug/20190622/1263633.html