当片段恢复时,TextureView变黑

时间:2018-06-05 11:28:48

标签: android textureview android-textureview

我有一个TextureView用于绘制一些linesrect以及我需要的抽奖 但是当我通过单击主页按钮退出应用程序并返回应用程序时 TextureView一直是黑色的,当我再次调用draw方法时,它现在显示任何只有黑色的东西(只有TextureView黑色而不是屏幕)

所以首先这是我的Manifest

 <application
    android:hardwareAccelerated="true"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/MyMaterialTheme" >



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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

现在我的TextureView

public class CanvasView extends TextureView implements   TextureView.SurfaceTextureListener   {


private Surface surface;
private Canvas canvas;
private SurfaceTexture mSurfaceTexture;
private boolean surfaceAvailable = false;


 public CanvasView(Context context) {
    super(context);
    init();
}

public CanvasView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CanvasView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init(){
setOpaque(false);
setSurfaceTextureListener(this);

}


   public void draw(){
 canvas = surface.lockCanvas(null);
 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
 //some drawing
 canvas.drawRect(qqqq, qq, sss, gggg, mPaint);
  //some drawing
 surface.unlockCanvasAndPost(canvas);
   }

 public CanvasView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    this.surfaceAvailable = true;

    Toast.makeText(getContext(),"surface created ",Toast.LENGTH_LONG).show();



    mSurfaceTexture = surface;
    drawCanvas();



}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    mSurfaceTexture = surface;



}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    this.surfaceAvailable = false;
    Toast.makeText(getContext(),"surface Destroyed ",Toast.LENGTH_LONG).show();

    return false;
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    mSurfaceTexture = surface;

}


   public void drawCanvas()
    {
     if (this.surfaceAvailable) {
                       if(surface == null)
                        surface = new Surface(mSurfaceTexture);             
                        draw();
      }
   }


  }

第一次抽奖没有问题 但当我从(android 7.0)上的应用程序退出并返回时,我看到TextureView的区域在调用Toast(表面创建)的同时它是黑色但是当我调用时绘制method called但是没有东西只绘制黑色TextureView

0 个答案:

没有答案