내 캔버스 아래에 XML 레이아웃을 구현하려고합니다. 캔버스는 휴대 전화의 너비에 따라 크기가 바뀌는 사각형 블록입니다 (너비를 감지하고 높이와 일치 함).).동적 크기의 캔버스 아래에 xml 레이아웃을 추가 할 수 없습니다.
내 앱은 항상 세로 모드로 표시되므로 내 캔버스 아래에 틈이 생깁니다. 모든 다양한 화면 크기로 인해 캔버스 아래의 공간은 끊임없이 변화하고 있습니다. 이 섹션에서는 레이아웃을 추가하여 텍스트 뷰와 이미지 버튼을 추가 할 수있게하겠습니다. 나는 현재이 모든 것을 onDraw 내에서 수행하고 있지만 이것은 매우 지저분한 코드이며 화면 사이의 방대한 크기 차이로 인해 코드의 3 가지 변형이 있음을 의미합니다.
저는 Game 클래스를 가지고 있습니다.이 Game 클래스는 모든 onDraw 코드로 구성된 GameView를 호출합니다. 질문은 캔버스 밑에 남겨진 공간을 채우기 위해 어떻게 레이아웃을 추가 할 수 있습니까? 게임/GameView 클래스의 기본 내역은 아래를 참조하십시오
게임 CLASS
public class Game extends Activity
{
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.test);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
this.maze = (Maze)getLastNonConfigurationInstance();
if(this.maze == null)
{
this.maze = (Maze)extras.get("maze");
}
// This is where I am setting the view, this leads to my onDraw code which progromatically creates as maze.
// This is a square box taking the width of the screen as the dimensions for the square.
GameView view = (GameView)findViewById(R.id.gameview);
view.setMaze(this.maze);
setContentView(view);
}
}
GAMEVIEW의 CLASS - 수동으로 생성 된 텍스트 내용의 작은 조각으로 캔버스
public class GameView extends View
{
// This is the second canvas within the maze, which sites below the main game maze which is the square i mentioned previously.
// This is where i need to add a layout which dynamically fills any remaining space.
protected void onDraw(Canvas canvas)
{
// Setting the canvas size for previous calcuated values.
canvas.drawRect(0, 0, width, height, background);
if((PhoneWidth < PhoneHeight) && (PhoneWidth < 600))
{
canvas.drawBitmap(arrows, QTRWidth * 2, height + 50, null);
if(CharacterCount >= 3)
{
canvas.drawText(Characters.get(2).Name(), 10, height + 135, GrayTextMedium);
// HIT POINTS
if((float)Characters.get(2).HitPoints()/(float)Characters.get(2).HitPointsMax() < 0.11)
canvas.drawText("HP: " + Characters.get(2).HitPoints(), 10, height + 160, RedTextMedium);
else
canvas.drawText("HP: " + Characters.get(2).HitPoints(), 10, height + 160, GrayTextMedium);
// MAGIC POINTS
if((float)Characters.get(0).MagicPoints()/(float)Characters.get(0).MagicPointsMax() < 0.11)
canvas.drawText("MP: " + Characters.get(2).MagicPoints(), QTRWidth, height + 160, RedTextMedium);
else
canvas.drawText("MP: " + Characters.get(2).MagicPoints(), QTRWidth, height + 160, GrayTextMedium);
}
if(CharacterCount == 1) { canvas.drawText("MONEY: " + Characters.get(0).Money(), 10, height + 80, GrayTextMedium); }
if(CharacterCount == 2) { canvas.drawText("MONEY: " + Characters.get(0).Money(), 10, height + 135, GrayTextMedium); }
if(CharacterCount == 3) { canvas.drawText("MONEY: " + Characters.get(0).Money(), 10, height + 190, GrayTextMedium); }
}
}
}
에서 누군가이 코드를 정리하는 데 도움을 주시겠습니까? 아니면 다른 곳에서 자세한 자습서를 가르쳐 주시겠습니까? 나는 이것에 대해 조사해 보았고 여러 가지 일을 시도하지 않았으므로 어떤 도움이라도 대단히 감사 할 것입니다. 감사.
'GameView'는 정사각형이므로 ('onMeasure()'를 오버라이드하고있는 중입니다.) 레이아웃에서 배치하는 것을 멈추고 원하는 뷰를 맨 아래에 두는 것이 무엇입니까? – Luksprog
나는 안드로이드 개발에 상당히 익숙하지 않고 어떻게하는지 잘 모르겠다. 예제 레이아웃과 내 Game 클래스 내에서 호출하는 방법을 알려줄 수 있겠는가? –
Thr 게임 클래스는 캔버스 내에서 미로를 그리고 그 위에 다양한 비트 맵을 onDraw 광고로 가져 오는 내 MazeCreator 클래스를 호출합니다.일부 예제는 내가 현재 잃어버린 것처럼 매우 도움이 될 것입니다. –