我在这里有一个自定义视图,但不幸的是它显示黑屏。
我的 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);
}
}
最后,未正确显示的 GameView 活动
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);
}
}
任何帮助将不胜感激,感谢您阅读
编辑:
这是我的 Sprite 的 onDraw()
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);
}
不幸的是,精灵和 GameView 在我实现自定义布局之前工作。