Search code examples
xamarin.androidimageview

An small image stretches in ImageView with the larger dimensions than the main image


I have an image (116*109 pixels).

I have an imageview (150dp * 150dp).

        <ImageView
            android:id="@+id/imageView3"
            android:layout_gravity="center"
            android:gravity="center"
            android:background="@android:color/transparent"
            android:layout_width="150dp"
            android:layout_height="150dp" /> 
            ImageView iv = FindViewById<ImageView>(Resource.Id.imageView3);
            iv.SetBackgroundResource(Resource.Drawable.house1);

Main image stretches in ImageView, why?

I do not use android:scaleType="fitXY" and android:adjustViewBounds="true".


Solution

  • Set the src will not stretch the image while set the background will:

     <ImageView
            android:id="@+id/imageView3"     
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:contentDescription=""
            android:src="@drawable/ttt"
            android:scaleType="centerInside" 
        /> 
    

    Or in code behind:

        ImageView iv = FindViewById<ImageView>(Resource.Id.imageView3);
        iv.SetImageResource(Resource.Drawable.ttt);
        iv.SetScaleType(ImageView.ScaleType.CenterInside);
    

    Refer: ImageView