2013-04-16 186 views
0

我在這裏有自定義視圖,但不幸的是它顯示黑屏。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工作,我實現我的自定義佈局之前,很遺憾。

+0

您的onDraw函數正在繪製黑屏,然後嘗試繪製精靈。那麼你的精靈onDraw在做什麼?另外,通常你使用而不是來定義自定義視圖,但我懷疑這是問題 –

回答

0

權,我找到了修復,它應該是很明顯&櫃面任何人有我的問題,這裏是修復。

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)將在最後對其進行初始化,並覆蓋其他的我不知道。

也許有人可以評論和啓發我所做的每個人和我自己。

謝謝

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> 
+0

不幸的是,這不是問題。當我這樣做時,我只是得到一個黑屏。 –