How do I keep the Imageview ratio in Android Studio?

Asked

Viewed 938 times

0

I had a problem creating a Splashscreen. The 512x512 image imported into the drawable directory does not maintain the same ratio after running the code. The height gets larger than the width, and manually adjust 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 by 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 mention, that in the xml preview the image is in its correct proportion, only in the compilation occurs the error.

Note: I do not think it is necessary, but I will make available the code of the animation of the image:

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

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

2 answers

2

The problem is simple, but it really is almost a prank! You are assigning the background from Imageview, and actually the Imageview background doesn’t maintain the proportions: it at all times distorts the image to fit in Imageview and not the other way around.

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

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

Note that you should use src and not srcCompat, for 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!

Modify to:

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

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

0

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

To use, simply insert into the dependencies of build.gradle:

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

In his SplashActivity, 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’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.