How do I maintain the ImageView aspect ratio in Android Studio?

0

I had a problem creating a SplashScreen. The image, 512x512, imported into the drawable directory does not maintain the same ratio after the code is executed. The height is larger than the width, and adjusting manually works on one device, but on another the problem remains.

SplashActivity class:

public class SplashScreenActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);

    ImageView logo = (ImageView) findViewById(R.id.imgLogo);
    logo.setBackgroundResource(R.drawable.robot);
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.move_up);
    logo.setAnimation(anim);

    //Handler: Aplica as mudanças de interface da SplashScreen
    try {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                Intent intent = new Intent(SplashScreenActivity.this,
                        MainActivity.class);

                startActivity(intent);

                SplashScreenActivity.this.finish();
            }
        }, 4000);
    } catch(Exception e){}
}
public void onBackPressed(){
    this.finish();
    super.onBackPressed();
}
 }

xml da activity:

<?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"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/colorPrimaryDark">

    <TextView
        android:text="@string/inicializando"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/inicioTexto"
        android:textSize="25sp"
        android:layout_marginBottom="170dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ffffff"/>

    <ProgressBar
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="90dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="315dp"
        app:srcCompat="@drawable/robot"
        android:id="@+id/imgLogo"
        android:layout_above="@+id/inicioTexto"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="26dp"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:adjustViewBounds="true"/>

</RelativeLayout>

AndroidManifest

   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SplashScreenActivity"
        android:theme="@style/SplashScreenTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!--Nova MainActivity criada -->

    <activity android:name=".MainActivity"></activity>
</application>

I should note that in the preview of xml the image is in its correct proportion, only in compilation does the error occur.

Note: I do not think it's necessary, but I'll provide the animation code for the image:

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

<translate
    android:duration="900"
    android:fromYDelta="-2000"
    android:toYDelta="0"
    />

    
asked by anonymous 24.10.2016 / 04:56

2 answers

2

The problem is simple, but it really is almost a catch! You are assigning the background of ImageView, and the ImageView background does not really maintain its proportions: it always distorts the image to fit in ImageView, / p>

In your XML you do it correctly (so it appears right in design time):

<ImageView
    ...
    app:srcCompat="@drawable/robot"
    ...
    />

Note that you should use src and not srcCompat , since src is already converted to srcCompat automatically .

However, in your code you use:

ImageView logo = (ImageView) findViewById(R.id.imgLogo);
logo.setBackgroundResource(R.drawable.robot);

And that causes the problem!

Change to:

ImageView logo = (ImageView) findViewById(R.id.imgLogo);
logo.setImageResource(R.drawable.robot);

However, since you already assigned the image in design-time, you should not even have to assign it again in runtime! See if you can not remove the logo.setImageResource(R.drawable.robot); line and everything will not work exactly as expected.

    
24.10.2016 / 05:18
0

I've had some problems with images, many of them disappeared with the use of a library called picasso .

To use, simply enter in the dependencies of build.gradle :

dependencies {
.
.
.
compile 'com.squareup.picasso:picasso:2.5.2
}

At your% w / o, replace the line:

logo.setBackgroundResource(R.drawable.robot);

To:

Picasso.with(getApplication()).load(R.drawable.robot).resize(512, 512).centerCrop().into(logo);

I hope I have helped.

    
26.10.2016 / 14:18