简体   繁体   中英

How do you fill imageview to screen in android

<?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"
    tools:context="galleryimages.galleryimages.Main2Activity"
    android:background="#000000"
    android:id="@+id/linear1">


    <ImageView
        android:id="@+id/iv_image2"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_above="@+id/button1"
        android:layout_weight="0"
        android:layout_gravity="center"
        tools:ignore="InvalidId" />


    <ImageButton
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:adjustViewBounds="false"
        android:background="#ffffff"
        android:gravity="bottom"
        android:src="@drawable/ic_delete_black_24dp" />


</RelativeLayout>

我尝试填充屏幕,但如您所见,边框为黑色。如何填充完整图像?

i want the images to fill the whole screen. And should i write separate code for 18:9 ratio mobiles? Also,if i delete an image, it's not syncing with google photos.

Add this attribute to the ImageView:

android:scaleType="centerCrop"  

or

android:scaleType="fitXY"

you can use from this code:

 <ImageView
                android:id="@+id/img"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_gravity="center"
                android:layout_marginBottom="64dp"
                android:adjustViewBounds="true" />

and other way:

DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int height = displayMetrics.heightPixels;
            int width = displayMetrics.widthPixels;
            int ratio = imgTop.getDrawable().getIntrinsicWidth() / imgTop.getDrawable().getIntrinsicHeight();
            int newheght = width / ratio;
            imgTop.getLayoutParams().height = newheght;

i think you must add below code:

 android:adjustViewBounds="true"

in xml file

This always works for me. Add it in the ImageView attribute:

   android:scaleType="fitCenter"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM