简体   繁体   English

包装内容ImageView显示完整尺寸的图像

[英]Wrap Content ImageView display full size of image

I am using imageview in android and when I put image in imageview using attributes wrap_content for both height and width. 我在android中使用imageview ,当我在图像imageview使用属性wrap_content设置高度和宽度时将其放在imageview Images appear to be so much big as they are of real size or if i took small images they got blur . 图像看起来太大了,无法达到实际尺寸,或者如果我拍摄的图像很小,它们就会变得模糊

code for it 它的代码


         <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/showhide"
        android:layout_marginRight="20sp"
        android:layout_marginLeft="20sp"
        android:layout_marginTop="10sp"
        android:padding="5sp"
        android:weightSum="4"
        >

        <!-- delivery intimation-->
        <LinearLayout android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/layout_d_i"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:weightSum="2"


            >
            <ImageView android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:src="@drawable/delivintimation"
                android:id="@+id/delivery_intimation"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:layout_marginBottom="10sp"

                />

            <TextView android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Delivery Intimation"
                android:gravity="center"
                android:maxLines="100"
                android:singleLine="false"
                android:layout_below="@id/delivery_intimation"
                android:textSize="12sp"
                android:textColor="@android:color/black"
                />

        </LinearLayout>
          </LinearLayout>

使用android:scaleType="centerInside"

Did you try using android:scaleType attribute? 您是否尝试使用android:scaleType属性?

Example: 例:

android:scaleType="centerCrop"

This attribute uses many other values like, 'fitXY', 'center', 'centerInside' etc. Try this 此属性使用许多其他值,例如“ fitXY”,“ center”,“ centerInside”等。

Use fixed size width and height on imageview . imageview上使用固定的大小宽度和高度。

<ImageView
    android:layout_width="200dp"
    android:layout_height="180dp"
    android:id="@+id/imageView"
    android:src="@drawable/image"
    android:scaleType="fitXY"/>

Use LinearyLayout to wrap your ImageView and another invisible View, and set layout_weight both to 0.3 or 0.5 etc, then the ImageView will be according to your size. 使用LinearyLayout包裹您的ImageView和另一个不可见的视图,并将layout_weight都设置为0.3或0.5等,然后ImageView将根据您的大小而定。

Here is an example for you showing how to set the image half the screen size 这是一个示例,显示如何将图像设置为屏幕大小的一半

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#ff0000"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/your_image"
            android:adjustViewBounds="true"
            />
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#00ff00"
        />
</LinearLayout>

If you want to put your image in center of the screen, you can use the same idea and set proper weight to achieve your goal 如果要将图像放在屏幕中央,可以使用相同的想法并设置适当的权重以实现目标

You can manually give width and height programatically 您可以通过编程手动指定宽度和高度

<ImageView   android:layout_width="100dp"
android:layout_height="200dp"
android:src="@drawable/notes"
android:id="@+id/iv_notes"
android:scaleType="fitXY"/>

scaleType-- fitXY will compress and fit all image inside imageview scaleType-- centerCrop will crop the image to the image view size scaleType-- fitXY将压缩并适合imageview内的所有图像scaleType-- centerCrop将图像裁剪为图像视图的大小

If you dont want to hard code the Width and height you have to create different drawable folders and place your different resolution images in different folder , Examples of drawable folders are 如果您不想对宽度和高度进行硬编码,则必须创建不同的可绘制文件夹,并将不同分辨率的图像放置在不同的文件夹中,可绘制文件夹的示例为

 drawable-ldpi
 drawable-mdpi
 drawable-hdpi
 drawable-xhdpi
 drawable-xxhdpi
 drawable-xxxhdpi

在XML中使用src而不是背景,请提供xml以了解您如何实现它

The code which you have used gave you the correct result: you have used LinearLayout and used weights. 您使用的代码为您提供了正确的结果:您已使用LinearLayout和权重。 weights will divide according to the screen resolution. 权重将根据屏幕分辨率进行划分。 just remove LinearLayout and use Relativelayout instead. 只需删除LinearLayout并改用Relativelayout。 Next remove all the weights for images and set its attributes to wrap_content. 接下来,删除图像的所有权重并将其属性设置为wrap_content。 If you want to give images that fits exactly to your screen width do it programmatically. 如果要提供完全适合您的屏幕宽度的图像,请以编程方式进行。

 DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
int desiredWidth=width/2;
yourView.setHeight(desiredWidth);

if this is not your requirement then in your RelativeLayout where you have these images. 如果这不是您的要求,请在您的RelativeLayout中拥有这些图像。 give attributes . 赋予属性。 android:alignParentRight="true" for one and android:alignParentLeft="true" for another image and set margins. android:alignParentRight="true"代表一个图像, android:alignParentLeft="true"代表另一幅图像并设置边距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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