How to make a dynamic ImageView?

2019-08-26 18:59发布

问题:

I need to make an app with some Images, and one that it's in the center needs to change his size depending on the size of the screen. Here is the code, the image that I need to change it's the image4:

<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:

Instead of adding ImageView in xml you should do that programmatically. In xml you need just RelativeLayout which will be used as image holder. So what you need to do is:

  1. Create image holder

`

<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. Create and add ImageView in image holder

`

            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);`

Now when you know screen size you can set image size depending on screen size.



回答2:

Try below code: -> First give your layout a id.

<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>

Now, in your code, change as per below.

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);