Android - 显示黑屏的自定义视图

时间:2013-04-16 01:57:49

标签: java android xml layout

我在这里有自定义视图但不幸的是它显示黑屏。

我的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);

}

}

任何帮助将不胜感激,谢谢你的阅读

编辑:

这是我的精灵的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已经工作了。

2 个答案:

答案 0 :(得分:0)

问题在于您尝试包含视图的方式:

<view
    class="pap.crowslanding.GameView"
    android:id="@+id/game_view"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</view>

这应该是:

<?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" >

<com.mypackage.pap.crowslanding.GameView // <--replace with fully qualified class name
    android:id="@+id/game_view"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />
</LinearLayout>

答案 1 :(得分:0)

是的,我找到了解决方案,它应该是显而易见的&amp;如果有人有我的问题,这是修复。

public GameView(Context context) {
    super(context);
    // TODO
}
public GameView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO
}

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    //PUT YOUR CODE IN HERE
}

我的问题是我把代码放在上下文,上下文和新手中,所以我无法解释它,也许是因为它位于底层所以(上下文attrs)它最后被初始化并覆盖其他我不知道的

也许有人可以就我所做的事情发表评论并启发每个人和我自己。

由于