Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ImageView - 16:9

I need to set the Bitmap into ImageView to keep ratio 16:9. Do you have any advice? The main solution is probably in the override method onMeasure of custom ImageView but how?

like image 989
user997777 Avatar asked Nov 08 '16 19:11

user997777


People also ask

What is the ImageView in Android?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.

What is aspect ratio of an image in Android?

The aspect ratio is a measured according to width:height. If the aspect ratio is 4:3, the width should be 4, whereas the height should be 3. This ratio sets the image accordingly.

How do I find the aspect ratio of my Android phone?

To determine the aspect ratio of a screen: Measure the width and height of the screen. Divide the width by the height. Compare the result with the popular aspect ratios, e.g., 16:9 , to determine which standard your screen follows.

What is aspect ratio in Android Studio?

16:9 standard aspect ratio.


2 Answers

Since PercentFrameLayout and PercentRelativeLayout were deprecated in API level 26.0.0, I'd suggest you to consider using of ConstraintLayout to keep ratio 16:9 for your ImageView. ConstraintLayout is really powerful tool to build Responsive UI for Android platform, you can find more details here Build a Responsive UI with ConstraintLayout.

Here is the example how to build your ImageView into ConstraintLayout to keep 16:9 ratio:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        app:srcCompat="@mipmap/ic_launcher"
        app:layout_constraintDimensionRatio="H,16:9"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Don't forget to add constraint-layout dependency to your module's build.gradle file

implementation "com.android.support.constraint:constraint-layout:1.0.2"

Or instead of editing your XML file, edit your layout directly in Layout Editor:

Layout Editor

like image 64
Eugene Brusov Avatar answered Sep 29 '22 14:09

Eugene Brusov


EDIT 01/03/2019: After all this time, I strongly recommend using ConstraintLayout that @Eugene Brusov introduced below. I personally would never use my image view that does Math by overriding onMeasure.

EDIT 03/29/2018: My answer below may be simple, but may be too much of raw answer. If you want to utilize what's already given by Google's ConstraintLayout, follow this answer or scroll down and see @Eugene Brusov's answer.

Old answer:

public class CustomImageView extends ImageView {

    // some other necessary things

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMeasuredWidth();

        //force a 16:9 aspect ratio             
        int height = Math.round(width * .5625f);
        setMeasuredDimension(width, height);
    }
}

Then in your xml

<path.to.package.CustomImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/img"/>
like image 41
Saehun Sean Oh Avatar answered Sep 29 '22 14:09

Saehun Sean Oh