0

저는 안드로이드에서 2D 응용 프로그램에 대한 자습서를 작성했습니다.중첩 표면 뷰> 선형 뷰> 가로 스크롤 뷰

현재 저는 표면보기에 그려진 캔버스를 사용하고 있지만, 기본 화면을 기반으로 한 전략 게임을 만들기 위해 화면을 가로로 스크롤 할 수있게하려고합니다. 지금까지 먹어 작은 기쁨 :

(전에서 일한지 자습서 : http://www.droidnova.com/playing-with-graphics-in-android-part-vii,220.html)

내가있는 LinearLayout를 확장해야 하는가?

XML :

<?xml version="1.0" encoding="utf-8"?> 

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:id="@+id/linearLayout" 
> 

</LinearLayout> 


</HorizontalScrollView> 

관련 코드 :

package com.blackslot.BlackMark; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 

import com.blackslot.BlackMark.gamethread; 
import android.widget.LinearLayout; 




import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.media.AudioManager; 
import android.media.SoundPool; 
import android.util.Log; 

import android.view.SurfaceHolder; 
import android.view.SurfaceView; 


public class Panel extends SurfaceView implements SurfaceHolder.Callback { 

/** 
* Thread which contains the game loop. 
*/ 

private gamethread _thread; 

/** 
* List of graphics to handle. 
*/ 
private ArrayList<Graphic> _graphics = new ArrayList<Graphic>(); 


/** 
* Sound pool 
*/ 
private SoundPool _soundPool; 


/** 
* Cache variable for all used images. 
*/ 
private Map<Integer, Bitmap> _bitmapCache = new HashMap<Integer, Bitmap>(); 

/** 
* Constructor called on instantiation. 
* @param context Context of calling activity. 
*/ 
public Panel(Context context) { 
    super(context); 
    fillBitmapCache(); 
    _soundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 100); 
    getHolder().addCallback(this); 
    _thread = new gamethread(this); 
    setFocusable(true); 
} 

private void fillBitmapCache() { 

    _bitmapCache.put(R.drawable.tacticalbuttondefault, BitmapFactory.decodeResource(getResources(), R.drawable.tacticalbuttondefault)); 
    _bitmapCache.put(R.drawable.targettingbuttondefault, BitmapFactory.decodeResource(getResources(), R.drawable.targettingbuttondefault)); 
    // _bitmapCache.put(R.drawable.ship1, BitmapFactory.decodeResource(getResources(), R.drawable.ship1)); 
    //_bitmapCache.put(R.drawable.ship2, BitmapFactory.decodeResource(getResources(), R.drawable.ship2)); 
    _bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt)); 
} 

/** 
* Draw on the SurfaceView. 
* Order: 
* <ul> 
* <li>Background image</li> 
* <li>Items on the panel</li> 
* </ul> 
*/ 
@Override 
public void onDraw(Canvas canvas) { 
    // draw the background 
    canvas.drawBitmap(_bitmapCache.get(R.drawable.abstrakt), 0, 0, null); 
    Bitmap bitmap; 
    Graphic.Coordinates coords; 
    // draw the UI 
    canvas.drawBitmap(_bitmapCache.get(R.drawable.tacticalbuttondefault), -2, 0, null); 
    canvas.drawBitmap(_bitmapCache.get(R.drawable.targettingbuttondefault), -2, 120, null); 




    // draw the normal items 
    for (Graphic graphic : _graphics) { 
     bitmap = graphic.getBitmap(); 
     coords = graphic.getCoordinates(); 
     canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null); 
    } 
} 
// @Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } 

/** 
* Called on creation of the SurfaceView. 
* Which could be on first start or relaunch. 
*/ 
// @Override 
public void surfaceCreated(SurfaceHolder holder) { 
    if (!_thread.isAlive()) { 
     _thread = new gamethread(this); 
    } 
    _thread.setRunning(true); 
    _thread.start(); 
} 

/** 
* Called if the SurfaceView should be destroyed. 
* We try to finish the game loop thread here. 
*/ 
// @Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    boolean retry = true; 
    _thread.setRunning(false); 
    while (retry) { 
     try { 
      _thread.join(); 
      retry = false; 
     } catch (InterruptedException e) { 
      // we will try it again and again... 
     } 
    } 
    Log.i("thread", "Thread terminated..."); 
} 
} 

감사합니다.

답변

0

비트 맵에 대해 X & Y 픽셀 오프셋 (양방향으로 스크롤 허용)을 사용하여 문제를 해결하고 가로 스크롤보기를 모두 포기하는 것으로 결정했습니다.

내가 찾던 해결책이 아니지만, 많은 추가 작업을하지 않아도됩니다.