SurfaceView示例 [英] SurfaceView Example

查看:98
本文介绍了SurfaceView示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我花了大约两天的时间来获取可用的SurfaceView.即使遵循这封信,我在网上关注的教程也无法正常工作.我通常会看到一个全黑的屏幕.

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.

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

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

我正在寻找一个在单独的类中生成SurfaceView的程序.如果有人能够为SurfaceView程序发布完整的代码(XML和Java),而该代码只是将整个屏幕变成红色或白色,我将不胜感激.

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.

谢谢您的帮助!

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

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

推荐答案

尝试此链接

Ansroid SurfaceView示例

我已按照本教程示例进行操作.效果很好.

I have follow this tutorial example. It works fine.

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将会是这样.

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"/>

活动中的代码

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

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

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

这篇关于SurfaceView示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆