2013-09-25 4 views
0

Мне нужно сделать приложение с некоторыми изображениями, а одно, что в центре, нужно изменить его размер в зависимости от размера экрана. Вот код, изображение, которое нужно изменить это image4:Как сделать динамический ImageView?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:background="@color/negro" 
     android:paddingBottom="20dp" 
     android:paddingTop="20dp" 
     android:src="@drawable/image1" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/azul" 
     android:src="@drawable/lineaceleste" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:background="@color/negro" 
     android:paddingBottom="5dp" 
     android:paddingTop="5dp" 
     android:src="@drawable/img3" /> 

    <ImageView 
     android:id="@+id/imageView8" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/azul" 
     android:src="@drawable/img8" /> 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/blanco" 
     android:entries="@array/listaRadios" 
     android:prompt="@string/radioPrompt" 
     android:scaleType="fitStart" /> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:background="@color/blk" > 

     <ImageView 
      android:id="@+id/imageView4" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@color/celeste" 
      android:paddingBottom="50dp" 
      android:paddingTop="50dp" 
      android:src="@drawable/image4" /> 
    </RelativeLayout> 

    <ImageView 
     android:id="@+id/imageView5" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/blanco" 
     android:src="@drawable/lineablanca" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/cel" 
     android:paddingBottom="5dp" 
     android:paddingTop="5dp" > 

     <ImageButton 
      android:id="@+id/btnPlay" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@null" 
      android:src="@drawable/play" /> 

     <ImageButton 
      android:id="@+id/btnPause" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@null" 
      android:src="@drawable/pause" /> 
    </LinearLayout> 

    <ImageView 
     android:id="@+id/imageView6" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/blanco" 
     android:src="@drawable/img6" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/cel" 
     android:paddingBottom="10dp" 
     android:paddingTop="10dp" > 

     <ImageView 
      android:id="@+id/imageView7" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="2" 
      android:src="@drawable/img7" /> 

     <ImageButton 
      android:id="@+id/imageButton3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@null" 
      android:src="@drawable/btn3" /> 

     <ImageButton 
      android:id="@+id/imageButton4" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="@null" 
      android:src="@drawable/btn4" /> 
    </LinearLayout> 

    <ImageView 
     android:id="@+id/imageView9" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/azul" 
     android:src="@drawable/img9" /> 

    <ImageView 
     android:id="@+id/imageView10" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/blk" 
     android:src="@drawable/img10" /> 

</LinearLayout> 

Thnx

+1

вы можете получить скриншот перед тем, как нарисовать изображение. затем вы масштабируете свое изображение и, наконец, рисуете его. – bofredo

+0

Возможный дубликат [Как динамически изменять высоту ImageView] (http://stackoverflow.com/questions/16397753/how-to-dynamically-change-imageview-height) –

ответ

3

Вместо добавления ImageView в xml вы должны сделать это программно. В xml вам нужно просто RelativeLayout, которое будет использоваться как держатель изображения. Так что вам нужно сделать, это:

  1. Создать держатель изображения

`

<RelativeLayout 
    android:id="@+id/image_view_holder" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:background="@color/blk" >` 
  1. Создание и добавление ImageView в держателе изображения

`

  DisplayMetrics metrics = new DisplayMetrics(); 
      getWindowManager().getDefaultDisplay().getMetrics(metrics); 

      int width = metrics.widthPixels; 
      int height = metrics.heightPixels; 

      RelativeLayout imgHolder = (RelativeLayout) findViewById(R.id.image_view_holder); 

      ImageView img = new ImageView(this); 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height/2); 
      img.setLayoutParams(params); 

      imgHolder.addView(img);` 

Теперь, когда вы знаете размер экрана, вы можете установить размер изображения в зависимости от размера экрана.

1

Попробуйте ниже код: -> Сначала дайте макет идентификатор.

<RelativeLayout 
     android:id="@+id/relative" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:background="@color/blk" > 

     <ImageView 
      android:id="@+id/imageView4" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@color/celeste" 
      android:paddingBottom="50dp" 
      android:paddingTop="50dp" 
      android:src="@drawable/image4" /> 
    </RelativeLayout> 

Теперь, в вашем коде, измените, как указано ниже.

ImageView imageView4; 
    RelativeLayout relative; 
imageView4=(ImageView)findViewById(R.id.imageView4); 
     relative=(RelativeLayout)findViewById(R.id.relative); 
LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     imageView4.setLayoutParams(params); 

     relative.addView(imageView4, params); 
Смежные вопросы