SurfaceView in RelativeLayout zal niet tekenen

Ik heb een XML-indeling en wil mijn eigen aangepaste SurfaceView eraan toevoegen en tekenen op het canvas. Wanneer ik debug, voert het canvas zijn onDraw uit en krijgt het de unlockandpost-functie, maar het is altijd een zwart scherm. Hier is mijn code. Ik probeer het al uren uit te zoeken en heb verschillende alternatieven geprobeerd, zoals het toevoegen van mijn aangepaste SurfaceView in de xml in plaats van het dynamisch toe te voegen, maar ik kreeg dezelfde resultaten.

Alle hulp wordt op prijs gesteld,

Dank je.

<?xml version="1.0" encoding="UTF-8"?>




 public class BeginGame extends Activity implements OnClickListener {
public RelativeLayout game_screen;
public LetterBar theBar;
public SurfaceHolder holder;
public static int width, height;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        theBar = new LetterBar(this);
    } catch (IOException e) {
       //TODO Auto-generated catch block
        e.printStackTrace();
    }

    Display display = getWindowManager().getDefaultDisplay(); 
    width = display.getWidth(); //deprecated
    height = display.getHeight(); //deprecated

    setContentView(R.layout.game_menu);
    game_screen = (RelativeLayout)findViewById(R.id.letter_bar);
    game_screen.addView(theBar);
}

@Override
public void onClick(View v) {
   //TODO Auto-generated method stub

}

}

 public class LetterBar extends SurfaceView implements SurfaceHolder.Callback {
private int width, height, toneWidth, toneHeight;
private Paint backgroundPaint, paint;

private Resources res;
private Drawable myImage;
private Bitmap neck, b;

private LetterThread _thread;

public LetterBar(Context context) throws IOException {
    super(context);
   //TODO Auto-generated constructor stub

    getHolder().addCallback(this);
    _thread = new LetterThread(getHolder(), this);

    paint = new Paint();
    paint.setColor(0xFFFFFF);
    backgroundPaint = new Paint();
    backgroundPaint.setARGB(0, 255, 255, 255);

    res = context.getResources();

    neck = BitmapFactory.decodeFile("/assets/guitar_neck.gif");

    InputStream bitmap=null;
    b = BitmapFactory.decodeResource(context.getResources(), R.drawable.guitar_neck);
    toneWidth = b.getWidth();
    toneHeight = b.getHeight();
}

@Override
public void onDraw(Canvas canvas){
    canvas.drawText("TEST", 20, 20, paint);
    canvas.drawBitmap(b, 0, 0, paint);
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
   //TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
   //TODO Auto-generated method stub
    setWillNotDraw(false);
    _thread.setRunning(true);
    _thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
   //simply copied from sample application LunarLander:
   //we have to tell thread to shut down & wait for it to finish, or else
   //it might touch the Surface after we return and explode
    boolean retry = true;
    _thread.setRunning(false);
    while (retry) {
        try {
            _thread.join();
            retry = false;
        } catch (InterruptedException e) {
           //we will try it again and again...
        }
    }
}
 }


 public class LetterThread extends Thread {
private SurfaceHolder _surfaceHolder;
private LetterBar letter_bar;
private boolean _run = false;

public LetterThread(SurfaceHolder surfaceHolder, LetterBar bar) {
    _surfaceHolder = surfaceHolder;
    letter_bar = bar;
}

public void setRunning(boolean run) {
    _run = run;
}

@Override
public void run() {
    Canvas c;
    while (_run) {
        c = null;
        try {
            c = _surfaceHolder.lockCanvas(null);
            synchronized (_surfaceHolder) {
                letter_bar.onDraw(c);
            }
        } finally {
           //do this in a finally so that if an exception is thrown
           //during the above, we don't leave the Surface in an
           //inconsistent state
            if (c != null) {
                _surfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}
 }
0

1 antwoord

Momenteel hebben uw paint en backgroundPaint beide de alpha 0 .

Probeer dit:

paint = new Paint();
paint.setColor(0xFFFFFFFF);//Added alpha bits out front to make it visible white
backgroundPaint = new Paint();
backgroundPaint.setARGB(255, 255, 255, 255);//255 alpha bit, so it's visible black
0
toegevoegd
/ facepalm Bedankt ... stomme fout ...
toegevoegd de auteur datWooWoo, de bron