2016-06-23 18 views
1

Androidスタジオのレイアウトに要素を正しく配置することができません。私は数字2を含むpngの背景を持つImageViewを持っています。私は小さな円をそれぞれ含む2つのビューを持っています。私はサークルが常に2番の各先端に整列したいと思っています。私は1つのデバイス上でそれらを正しく整列させることができますが、別のデバイスで整列が変わります。これらは常にすべてのデバイスで正しく整列させることが可能ですか?ここでImageView上の正確な位置での位置の形状

は、私はそれが見えるようにしたいものです。ここで http://i.stack.imgur.com/v7nsg.png

ドット位置のいずれかを計算するために私のコードです:ここでは

circle2 = findViewById(R.id.circle2); 

    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    int heightPixels = metrics.heightPixels; 
    int widthPixels = metrics.widthPixels; 

    //dimensions of background image: 547x839 

    dot2H = (heightPixels/547) * 70; 
    dot2W = (widthPixels/829) * 70; 
    dot2Hposition = (heightPixels/829) * 500; 
    dot2Wposition = (widthPixels/547) * 300; 
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) circle2.getLayoutParams(); 
    params.height = dot2H; 
    params.width = dot2H; 
    params.leftMargin = dot2Wposition; 
    params.topMargin = dot2Hposition; 
    circle2.setLayoutParams(params); 

は、レイアウトコードです:

<?xml version="1.0" encoding="utf-8"?> 
<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:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/bg" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="net.beauvine.animationtest.MainActivity" 
tools:showIn="@layout/activity_main"> 
<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scaleType="centerInside" 

    android:src="@drawable/two" 
    android:id="@+id/imageView" 
    android:padding="10dp" 
    android:layout_alignParentEnd="false" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" /> 

<RelativeLayout 
    android:id="@+id/dotLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:gravity="center" 
    > 

    <View android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:id="@+id/circle2" 
     android:background="@drawable/circle" 
     android:clickable="true" 
     android:alpha=".8" 
     /> 
    <View android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:id="@+id/circle" 
     android:background="@drawable/circle" 
     android:clickable="true" 
     android:alpha=".8" 
     /> 

</RelativeLayout> 

答えて

1

Androidは非常に大きなnu非常に異なる画面サイズと密度を持つデバイスのこのため、XMLレイアウトを使用して必要な作業を行うのは難しいです。しかし、ビューの固定された関係でレイアウトを使用することができますので、それはあなたが欲しいものに似て見えますが、これは良いアプローチではありません。

この例では、キーを与えることができます。このレイアウトは、常に画面の正確な場所 の4つのビューを示しています。このような

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2"> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <View 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:background="@android:color/white" /> 
     </FrameLayout> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <View 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:background="@android:color/white" /> 
     </FrameLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="2"> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <View 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:background="@android:color/white" /> 
     </FrameLayout> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <View 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:background="@android:color/white" /> 
     </FrameLayout> 
    </LinearLayout> 

</LinearLayout> 

何か:カスタムビュー内の両方(番号2の)背景と円を描くかどう

enter image description here

良いです。それで、あなたはすべてのオブジェクトの正確なサイズと座標を知り、正しく配置できます。

カスタムビューに描画するだけの例:

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(new MyView(this)); 
    } 

    public class MyView extends View { 
     public MyView(Context context) { 
      super(context); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      // TODO Auto-generated method stub 
      super.onDraw(canvas); 

      int x = getWidth(); 
      int y = getHeight(); 
      int radius; 
      radius = 100; 
      Paint paint = new Paint(); 
      paint.setStyle(Paint.Style.FILL); 
      paint.setColor(Color.BLACK); 
      canvas.drawPaint(paint); 

      paint.setColor(Color.WHITE); 
      canvas.drawCircle(x/2, y/2, radius, paint); 

      paint.setColor(Color.WHITE); 
      canvas.drawCircle(x/3, y/3, radius, paint); 
     } 
    } 
} 

・ホープ、このことができます。

+0

私はカスタムビューの提案を試してきました。それは私の目標に近づいているようです。しかし、私が前に明らかにしていなかった点の1つは、点に触れると点がアニメートするボタンであることです。それをどのように組み込むのですか?あなたの例では、キャンバスに円を描いていました。残念ながら、それらはボタンではありません。 –

+0

自分自身を描き、タッチアクション(Viewなど)を処理する特定のオブジェクトを作成する必要があります。あなたはゲームのようなものをプログラミングしていますか?この場合、無料のゲームエンジンの1つを使用しようとすることができます。彼らはすべてカスタム描画、オブジェクトのアニメーション、必要な対話などをサポートしています。 – comrade

+0

助けてくれてありがとう!私は各ドットの別のカスタムビューを作成して終了し、それらをボタンにしました。あなたの解決策は間違いなく私を正しい道に導きました。 –

関連する問題