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?
Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.
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.
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.
16:9 standard aspect ratio.
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:
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"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With