简体   繁体   中英

ImageView array not keeping aspect ratio

I have a small problem that I think it is easily fixable by someone more experienced. In an ImageView I set all attributes in xml as I wanted and it was displaying my image perfectly fine. Then I decided to make an imageView array and to change the image each time something happened in my app. This is the xml I had before that was displaying perfectly fine:

`<ImageView
            android:id="@+id/questions_image_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:contentDescription="@string/image_q1"
            android:layout_margin="16dp"
            android:src="@drawable/view_groups_good"
            android:layout_below="@id/score_no"/>` 

After the creation of the array I have removed from the xml the android :src attribute and created this method that I call each time I want to change the image.

This is what I have before onCreate

int [] imageAllQ = {R.drawable.view1, R.drawable.view2, R.drawable.view3, R.drawable.view4, R.drawable.view5, R.drawable.view6, R.drawable.view7, R.drawable.view8, R.drawable.view9, R.drawable.view10};
int imgCount = 0;
ImageView questionsViews;

And this is the method:

public void changeImgQA (){
    questionsViews = (ImageView)findViewById(R.id.questions_image_view);
    questionsViews.setBackgroundResource(imageAllQ[imgCount]);
    imgCount = imgCount + 1;
}

Everything works exactly as I want, but now the image view does not take into account the xml attributes and the image displayed is distorted, I mean it is match parent on width but on height it is completely distorted, being too small compared to what I want.

Any suggestion is welcomed

use scaleType = centerCrop , this should preserve the aspect ratio along android:adjustViewBounds="true"

setBackGroundResource , Set the background to a given resource.However setImageBackground, Sets a drawable as the content of this ImageView.

that is why there is that distortion.

You should use

questionsViews.setImageResource(imageAllQ[imgCount]);

Instead of

questionsViews.setBackgroundResource(imageAllQ[imgCount]);

Cause

setImageResource is a public method of ImageView and is responsible to set the source of the ImageView to the given resource.

Sets a drawable as the content of this ImageView.

setBackgroundResource is a public method of View and is responsible to set the background of the View to the given resource.

Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

BTW

You should initialize your ImageView once inside onCreate and then re-use it instead of calling findViewById every time you change the image.

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