Android SurfaceView相对论Yout Won';t画
我有一个XML布局,希望向其中添加我自己的自定义SurfaceView,并在其画布上绘制。当我调试时,画布运行其onDraw并获得unlockandpost函数,但它始终只是一个黑屏。这是我的密码。我已经花了几个小时试图解决这个问题,并尝试了多种替代方法,例如只将自定义SurfaceView添加到XML中,而不是动态添加,但我得到了相同的结果 任何帮助都将不胜感激 多谢各位Android SurfaceView相对论Yout Won';t画,android,layout,surfaceview,Android,Layout,Surfaceview,我有一个XML布局,希望向其中添加我自己的自定义SurfaceView,并在其画布上绘制。当我调试时,画布运行其onDraw并获得unlockandpost函数,但它始终只是一个黑屏。这是我的密码。我已经花了几个小时试图解决这个问题,并尝试了多种替代方法,例如只将自定义SurfaceView添加到XML中,而不是动态添加,但我得到了相同的结果 任何帮助都将不胜感激 多谢各位 <?xml version="1.0" encoding="UTF-8"?> <LinearLayout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/guitar_neck">
</ImageView>
<RelativeLayout
android:id="@+id/letter_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
}
目前,您的
paint
和backgroundPaint
都具有alpha0
试试这个:
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
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);
}
}
}
}
}
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