How do I maintain the ratio of ImageView in Android Studio?
I had a problem creating a SplashScreen. The image, 512x512, imported into the drawable directory does not maintain the same aspect ratio after the code is executed. The height is greater than the width, and manually adjusting 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 from 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 must state, that in the preview of the xml the image is in its proportion correct, only in the compilation the error occurs.
Note: I do not think it is necessary, but I will provide the image animation code:
<?xml version="1.0" encoding="utf-8"?>
<translate
android:duration="900"
android:fromYDelta="-2000"
android:toYDelta="0"
/>
2 answers
The problem is simple, but it really is almost a catch! You're assigning the ImageView's background, and actually the ImageView's background doesn't keep the proportions: it always distorts the image to fit the ImageView and not the other way around.
In your XML you do 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!
Modify to:
ImageView logo = (ImageView) findViewById(R.id.imgLogo);
logo.setImageResource(R.drawable.robot);
However, since you already assigned the image in design-time, you shouldn't even need to assign it again at runtime! See if you can not remove the line logo.setImageResource(R.drawable.robot);
and if everything will not work exactly as expected.
I have had some problems with images, many of them disappeared with the use of a library called picasso .
To use, simply insert in the build.gradle
dependencies:
dependencies {
.
.
.
compile 'com.squareup.picasso:picasso:2.5.2
}
In your SplashActivity
, replace the line:
logo.setBackgroundResource(R.drawable.robot);
For:
Picasso.with(getApplication()).load(R.drawable.robot).resize(512, 512).centerCrop().into(logo);
I hope I helped.