我在這裏有自定義視圖,但不幸的是它顯示黑屏。Android - 自定義視圖顯示黑屏
我的XML文件 「tester1」
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<view
class="pap.crowslanding.GameView"
android:id="@+id/game_view"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</view>
</LinearLayout>
它使用的佈局我的活動似乎罰款
package pap.crowslanding;
import android.os.Bundle;
import android.widget.Button;
public class Game extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tester1);
//GameView gameView = (GameView) findViewById(R.id.game_view);
Button button = (Button) findViewById(R.id.sbutton);
}
}
最後,這是不正確顯示
package pap.crowslanding;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
public class GameView extends SurfaceView {
static final long FPS = 10;
public Bitmap bmp;
public SurfaceHolder holder;
public LoopGameThread loopGameThread;
public Sprite sprite;
public LayoutInflater inflater;
public GameView(Context context) {
super(context);
loopGameThread = new LoopGameThread(this);
holder = getHolder();
holder.addCallback(new Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
loopGameThread.isStart(true);
loopGameThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.crow);
sprite = new Sprite(this, bmp);
}
public GameView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
sprite.onDraw(canvas);
}
}
的GameView活動任何幫助將不勝感激,感謝您閱讀
編輯:
這是我的雪碧
public void onDraw(Canvas canvas) {
update();
int srcX = currentFrame * width;
int srcY = direction * height;
src = new Rect(srcX, srcY, srcX + width, srcY + height);
dst = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(bmp, src, dst, null);
}
精靈的的onDraw()和GameView工作,我實現我的自定義佈局之前,很遺憾。
您的onDraw函數正在繪製黑屏,然後嘗試繪製精靈。那麼你的精靈onDraw在做什麼?另外,通常你使用而不是來定義自定義視圖,但我懷疑這是問題 –