2017-08-12 5 views
0

RelativeLayoutが完全に画面に収まるようなアプリを作っています。私はレイアウトにとsetScaleY()を使ってこのレイアウトをズームするボタンを追加しました。結果として(明らかに)レイアウト全体がスクリーンにはもう収まりません。だから最初はScrollViewを使ってRelativeLayoutに移動できると思ったが、これはうまくいかなかった。助言がありますか?前もって感謝します。ここRelativeLayoutで移動する

がXMLコードである:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layoutMain" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="phou.minesweeper.GameActivity"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/chronometer" 
    android:layout_below="@+id/textViewBest"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="true" 
     android:layout_centerVertical="true"> 

     <RelativeLayout 
      android:id="@+id/grid" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerInParent="true" 
      android:layout_centerVertical="true"></RelativeLayout> 
    </ScrollView> 
</RelativeLayout> 
</RelativeLayout> 

iはズーミングだRelativeLayoutは、ID =グリッドを有するものです。 すべてのビューがJavaで作成されるため、xmlコードは非常に短いです。

+0

私にxmlコードを見せてください。 –

答えて

1

setScaleX/Yを使用してJavaで表示サイズを操作する場合は、ドラッグ操作のイベントを処理するために、RelativeLayoutにonTouchListenerを実装する必要があります。

スケーリングしているビューが@ id/gridであると仮定すると、単純なOnTouchListenerは次のようになります。

final View grid = findViewById(R.id.grid); 
final Point dispSize = new Point(); 
getWindowManager().getDefaultDisplay().getSize(dispSize); 
grid.setOnTouchListener(new OnTouchListener() { 

    private PointF mAnchor = new PointF(); 
    private PointF mTouchPoint = new PointF(); 

    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     mTouchPoint.set(motionEvent.getX(), motionEvent.getY()); 
     switch (motionEvent.getActionMasked()) { 
      case MotionEvent.ACTION_DOWN: 
       mAnchor.set(mTouchPoint); 
       return true; 
      case MotionEvent.ACTION_MOVE: 
       float dx = mTouchPoint.x - mAnchor.x; 
       float dy = mTouchPoint.y - mAnchor.y; 
       float left = grid.getX() + dx; 
       float right = left + grid.getWidth(); 
       float top = grid.getY() + dy; 
       float bottom = top + grid.getHeight(); 
       if (grid.getWidth() > dispSize.x) { 
        if (left > 0f) { 
         left = 0f; 
        } 
        if (right < dispSize.x) { 
         left += dispSize.x - right; 
        } 
       } 
       if (grid.getHeight() > dispSize.y) { 
        if (top > 0f) { 
         top = 0f; 
        } 
        if (bottom < dispSize.y) { 
         top += dispSize.y - bottom; 
        } 
       } 
       grid.setX(left); 
       grid.setY(top); 
       return true; 
      default: 
       return true; 
     } 
    } 
}); 

このOnTouchListenerのみ(私たちはXMLコードで表示されていない)RelativeLayoutのコンテンツビューによって傍受されていないタッチイベントを受け取ることに注意してください。つまり、コンテンツビューでは、消費者のタッチ/ジェスチャのリスナーでfalseを返すことによって、消費されていないタッチイベントを階層の上に渡す必要があります。

+0

あなたの答えをありがとうが、私はまだこれに問題があります。どれくらい移動できるかに制限はありません。レイアウトを画面からドラッグすることができます。あなたにはこれに対する解決策がありますか?ありがとう –

+0

最初の答えはあなたのonTouchListenerでできることの味を与えることでした。グリッドを画面の範囲内に保つ簡単な方法を示すために答えを編集しました。あなたの特定の要件は、おそらくより洗練されている必要がありますが、うまくいけばあなたはアイデアを得る。 – AGDownie

関連する問題