2011-07-29 12 views
2

グリッドベースのゲームを画面よりもはるかに大きく作成しており、ユーザーはその中をスクロールします。私は基本的に、相対レイアウトを拡張するカスタムクラスの内側にImageViewsを入れました。問題は、RelativeLayout.LayoutParamsが正しいサイズに設定されていても(1280 * 1280)欲しいということです。画像は画面の横に詰め込まれ、それを超えて伸びません。私はスクロールロジックが動作している、と私はスクロールすると、私はそれが1つの画面のサイズの画像の長方形であることがわかります。画像が画面の向こう側に広がるようにするにはどうすればいいですか?相対レイアウトの内容を画面より大きくする

相対的なレイアウトを拡張するクラス:

public class GameGrid extends RelativeLayout { 
    ImageView[][] subViews; 
    int rows=0, cols=0, cellsize=0; 
    int width, height; 

    //Dragging variables 
    float startX; 
    float startY; 
    float lastX; 
    float lastY; 
    boolean touching; 
    boolean dragging; 
    int clickedChild; 

    public GameGrid(Context context) { 
     super(context); 
     init(); 
    } 

    public GameGrid(Context context, int rws, int cls, int clsze) { 
     super(context); 
     rows=rws; 
     cols=cls; 
     cellsize=clsze; 
     init(); 
    } 

    public GameGrid(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public GameGrid(Context context, AttributeSet attrs, int defaultStyles) { 
     super(context, attrs, defaultStyles); 
     init(); 
    } 

    public void init() { 
     rows=10; 
     cols=10; 
     cellsize=128; 

     startX = 0; 
     startY = 0; 
     lastX=0; 
     lastY=0; 
     touching = false; 
     dragging = false; 
     clickedChild = -1; 

     subViews = new ImageView[cols][rows]; 

     setLayoutParams(new RelativeLayout.LayoutParams(cellsize*cols,cellsize*rows)); 

     width=this.getLayoutParams().width; 
     height=this.getLayoutParams().height; 

     this.setMinimumWidth(width); 
     this.setMinimumHeight(height); 

     Log.i("info","****************"); 
     Log.i("info","GameGrid Made."); 
     Log.i("info","width: "+width+"\nheight: "+height); 
     Log.i("info","****************"); 

     makeGrid(); 

     // this.setOnTouchListener() 
    } 

    public boolean getDragging(){ 
     return dragging; 
    } 

    public void makeGrid() { 
     for(int y=0;y<rows;y++){ 
      for(int x=0;x<cols;x++){ 
       ImageView temp = new ImageView(getContext()); 
       temp.setImageResource(R.drawable.water1); 
       temp.setScaleType(ImageView.ScaleType.FIT_XY); 

       RelativeLayout.LayoutParams temp2 = new RelativeLayout.LayoutParams(width/cols,height/rows); 

       if (x == 0 && y == 0){ //If this is the first view being made, set it relative to the parent. 
        temp2.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
        temp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
       } 
       else if (x == 0){ //If this is in the first column, set it below the one above. 
        temp2.addRule(RelativeLayout.ALIGN_LEFT,subViews[0][y-1].getId()); 
        temp2.addRule(RelativeLayout.BELOW,subViews[0][y-1].getId()); 
       } 
       else { //Align the bottom with first one of that row. 
        temp2.addRule(RelativeLayout.RIGHT_OF,subViews[x-1][y].getId()); 
        temp2.addRule(RelativeLayout.ALIGN_BOTTOM,subViews[0][y].getId()); 
       } 

       temp.setLayoutParams(temp2); 

       subViews[x][y]=temp; 
       subViews[x][y].setId(x+y*cols+1); 

       // Toast.makeText(getContext(), "" + v.getId(), Toast.LENGTH_SHORT).show(); 

       subViews[x][y].setOnTouchListener(new OnTouchListener() { 
        public boolean onTouch(View v,MotionEvent event) { 
         if (event.getAction() == MotionEvent.ACTION_DOWN) 
          clickedChild = v.getId(); 
         return false; 
        } 
       }); 
       addView(temp); 
      } 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) 
     { // when the user touches the screen 
      startX = event.getX(); 
      startY = event.getY(); 
      lastX = event.getX(); 
      lastY = event.getY(); 
      touching = true; 
      dragging = false; 
      return true; 
     } 
     else if (event.getAction() == MotionEvent.ACTION_MOVE) 
     { // when the user moves the touch 
      if (!dragging) 
       dragging = true; 
      int distX = (int)(event.getX()-lastX); 
      int distY = (int)(event.getY()-lastY); 
      this.scrollBy(-distX, -distY); 
      lastX = event.getX(); 
      lastY = event.getY(); 
      return true; 
     } 
     else if (event.getAction() == MotionEvent.ACTION_UP) 
     { // when the user lifts the touch 
      if (!dragging){ 
       if (clickedChild>0){ 
       Toast.makeText(getContext(), "getHeight()= " + getHeight(), Toast.LENGTH_SHORT).show(); 

       clickedChild = -1; 
       } 
      } 
      touching = false; 
      dragging = false; 
      return true; 
     } 
     else if (event.getAction() == MotionEvent.ACTION_CANCEL) 
     { // if something gets lost in translation 
      startX = 0; 
      startY = 0; 
      lastX=0; 
      lastY=0; 
      touching = false; 
      dragging = false; 
      return true; 
     } 
     return false; 
    } 


    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
     int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
     this.setMeasuredDimension(parentWidth, parentHeight); 
    } 

活動:

public class Attacktics2 extends Activity { 
/** Called when the activity is first created. */ 
    GameGrid grid; 

    int rows, cols, cellsize; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

     setContentView(R.layout.main); 
    } 

    public void start(View view) { 
     view.setVisibility(View.GONE); 
     grid = new GameGrid(this,10,10,128); 
     setContentView(grid); 
    } 
} 

答えて

1

あなたがすでにすべてのスクロールを管理する力仕事をやっているので、私はことをお勧めしたいですレイアウトロジック全体を自分で実装し、RelativeLayoutに依存しないでください。 ScrollViewとHorizo​​ntalScrollViewを除いて、ストックレイアウトクラスは、子が親境界内に収まるように制限します。これらは、次に、画面の寸法に制限されます。レイアウトロジックを自分で処理する場合は、子ビューをスクリーンから外すように配置できます。その後、大きなグリッドにビューポートを作成し、ビューポート内で可視の子をレンダリングするだけです。

+0

私もそれを考えていました。したがって、レイアウトを自分で処理するには、onLayoutとonMeasureをオーバーライドする必要がありますか? – Amax125

+1

あなたが必要とする最小限のものはViewGroupを拡張し、onLayoutとonMeasureを上書きします。 [AbsoluteLayout](http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/AbsoluteLayout.java;h)を見てください。 = 970cbe353a8c52ddd9332281dbadf8d1dc977d6c; hb = HEAD)。子が可視領域と交差している場合にのみ、drawChildをオーバーライドしてsuperを呼び出すことは、パフォーマンスが良いことがあります。また、ViewGroup.LayoutParamsを拡張し、子ごとのレイアウトデータを定義する静的ネストされたクラスを定義すると便利です。 –

+1

それは働いてくれました、ありがとう! – Amax125

関連する問題