问题描述
我想为我的游戏多意见,我创建了一个视图组,目前只有一个,当我运行程序我来到这里黑屏是code为我的游戏视图类(ViewGroup中)
进口android.content.Context;
进口android.view.ViewGroup;
进口java.io.FileNotFoundException;公共类GameView扩展的ViewGroup { 私人MainActivity活动; 公共GameView(上下文的背景下,MainActivity活动)抛出FileNotFoundException异常
{
超级(上下文);
this.activity =活动;
图形V =新的图形(这一点,活动,背景);
addView(五);
}
@覆盖
保护无效onLayout(布尔B,INT I,诠释I2,I3 INT,INT I4){ }
}
这是我的code为我的图形类(视图):
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.view.MotionEvent;
进口android.view.View;
进口android.widget.Button;
进口java.io.IOException异常;/ **
*创建者匿名在14年4月29日。
* /公共类图形扩展视图实现Runnable,Button.OnTouchListener {
私人螺纹螺纹;
私人布尔运行= FALSE;
私人玩家P;
私人MainActivity活动;
私人键b;
GameView图。
公共图形(GameView观点,MainActivity活动,上下文的背景下){
超级(上下文);
this.view =视图。
this.activity =活动;
P =新播放器(400,600,活动); } 公共同步无效的start()
{
如果(运行)
返回;
运行= TRUE;
线程=新主题(本);
thread.start();
}
公共同步无效停止()
{
如果(!运行)
返回; 运行= FALSE;
System.exit(0); } @覆盖
公共无效的run()
{
位图B = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888);
帆布C =新的Canvas(B);
而(运行)
{
的onDraw(C);
蜱(); } } 公共无效打勾()
{
p.tick(本); }
@覆盖
保护无效的onDraw(帆布油画)
{
尝试
{
p.render(画布); }赶上(IOException异常E)
{ }
} 公共布尔onTouch(查看视图,MotionEvent五){
INT行动= e.getAction();
开关(动作)
{
案例MotionEvent.ACTION_DOWN: p.setY(p.getY()+ 1);
打破;
}
返回false;
}
}
尝试把你的addView内的活动。
如果您需要附加一个视图不止一次,只需拨打addView不止一次。需要注意的是一个ViewGroup是一个抽象类,所以它可能会更快,为您只需使用的LinearLayout追加你的意见,而不是。的LinearLayout本身就是一个ViewGroup中。
试试我的建议,如果你被卡住的地方,请张贴在这里您的新活动的code(请不要发表您的老年活动),并告诉我们为什么你认为你被卡住。
的对于任何人读这篇文章,如果您需要更多的情况下,请参阅本original 的问题,别人问他要一分为二。的
I am trying to have multiple views for my game so I created a view group and currently only have one and when I run the program I get a blank screen here is the code for my Game View Class(ViewGroup) :
import android.content.Context;
import android.view.ViewGroup;
import java.io.FileNotFoundException;
public class GameView extends ViewGroup {
private MainActivity activity;
public GameView(Context context,MainActivity activity) throws FileNotFoundException
{
super(context);
this.activity = activity;
Graphics v = new Graphics(this,activity,context);
addView(v);
}
@Override
protected void onLayout(boolean b, int i, int i2, int i3, int i4) {
}
}
here is my code for my graphics class(View) :
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
/**
* Created by Anonymous on 4/29/14.
*/
public class Graphics extends View implements Runnable,Button.OnTouchListener {
private Thread thread;
private boolean running = false;
private Player p;
private MainActivity activity;
private Button b;
GameView view;
public Graphics(GameView view,MainActivity activity,Context context){
super(context);
this.view = view;
this.activity = activity;
p = new Player(400,600,activity);
}
public synchronized void start()
{
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop()
{
if(!running)
return;
running = false;
System.exit(0);
}
@Override
public void run()
{
Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
while(running)
{
onDraw(c);
tick();
}
}
public void tick()
{
p.tick(this);
}
@Override
protected void onDraw(Canvas canvas)
{
try
{
p.render(canvas);
}catch (IOException e)
{
}
}
public boolean onTouch(View view,MotionEvent e){
int action = e.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
p.setY(p.getY()+1);
break;
}
return false;
}
}
Try putting addView inside your Activity.
If you need to append a View more than once, just call addView more than once. Note that ViewGroup is an abstract class, so it may be quicker for you to simply use a LinearLayout to append your views to instead. LinearLayout is itself a ViewGroup.
Try out my suggestion, if you get stuck somewhere, please post the code of your new activity on here (please don't post your old activity), and tell us why you think you're getting stuck.
For anyone else reading this, if you need more context, please see this original question that someone else asked him to split into two.
这篇关于安卓游戏产生黑屏,当我添加新的视图视图组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!