gpt4 book ai didi

android - SurfaceView 显示黑屏 - Android

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:25 25 4
gpt4 key购买 nike

基本上我想用 SurfaceView 做动画。因此该类实现了 Runnable。为了实验,我想画一个圆圈。但是,它只显示黑屏。

我已经试了好几天了。如果有人能提供帮助,我将不胜感激。

主 Activity 类

public class MainActivity extends Activity {

private Bitmap Liquid;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature (Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

DrawStripFrame D1 = new DrawStripFrame(this);
setContentView(D1);

DrawStripFrame 类

public class DrawStripFrame extends SurfaceView implements Runnable{

private SurfaceHolder holder;
private boolean running = true;

public DrawStripFrame (Context context){
super (context);
holder = getHolder();
}

@Override
public void run(){

while(running){
if(holder.getSurface().isValid()){
Canvas c = holder.lockCanvas();
c.drawARGB(0, 0, 0, 0);
Paint redPaint = new Paint();
redPaint.setColor(Color.RED);
c.drawCircle(100, 100, 30, redPaint);
holder.unlockCanvasAndPost(c);
}

}
}
}

最佳答案

我遇到了同样的问题。我在不同的模拟器上测试了我的APP。

这是我的 MainActivity 类

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private SurfaceViewTest test;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
test = new SurfaceViewTest(this);
setContentView(test);
}

@Override
protected void onResume() {
super.onResume();
test.resume();
}
}

SurfaceViewTest.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceView;


public class SurfaceViewTest extends SurfaceView implements Runnable {

private Thread thread;
volatile boolean running;


public SurfaceViewTest(Context context) {
super(context);
}
@Override
public void run() {
while(running) {
draw();
}
}

private void draw(){

if(getHolder().getSurface().isValid()) {
Canvas canvas = getHolder().lockCanvas();

canvas.drawColor(Color.argb(255, 255, 0, 0));

getHolder().unlockCanvasAndPost(canvas);
}
}

public void resume() {
running = true;
thread = new Thread(this);
thread.start();
}

public void pause() {
running = false;

while (thread.isAlive()) {
try {
thread.join(100);

if(thread.isAlive()) {
thread.interrupt();
thread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

样式.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowFullscreen">true</item>
</style>

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:background">#000000</item><!-- Remove it to fix the problem -->
</style>
</resources>

我解决了它从 styles.xml 中删除项目背景

<item name="android:background">#00000</item>

看起来 alpha channel 有问题。

关于android - SurfaceView 显示黑屏 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21311573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com