Reputation: 406
🌟Text Code of MainActivity.java and activity_main.xml exist at the bottom of the article🌟
I am going to set Images that have the same ratio (16:9) to ImageView, But I met up with a small problem.
I want to set My ImageView
remain 16:9 ratio, which of the picture being handed over(1920 * 1080)
if I use
wrap_content
, it remains ratio of the given picture, But I can't resize the size ofImageView
.
.
if I use fixed numbers that remain 16:9 ratio like 400dp * 225dp
I can resize the size I want, and modify the position by Screen scale by usingapp:layout_constraintHorizontal_bias=
andapp:layout_constraintVertical_bias=
But in other Device, the Size of
ImageView
is too small or too big for its Screen Space. because the value is fixed
So, I Want to set ImageView
Size and Position by Device's Screen Scale with remaining its ratio(16:9)
How can I do it?
I've looked at a number of questions, but I've found a way "to match the imageView ratio to the picture" not "to set the size of ImageView relative to the size of the screen" cause my poor at searching.
Is there any method way to express as follows? this is not real code, it is just pseudo-code to explain easily what I really want to make.
android:layout_width="wrapcontent * 0.8"
android:layout_height="wrapcontent * 0.8" //<!-- same as "layout_width * 9 / 16 " because ratio of give picture will be always 16 : 9 -->
app:layout_constraintHorizontal_bias=" ((Screen_width - ImageView_width) / 2) / 100 " //<!-- pseudo percentage that to stay in Screen's center -->
app:layout_constraintVertical_bias= "0.3" //<!-- pseudo percentage that to stay in Screen's custom upper part-->
I apologize for the confusion in your explanation and hope you have a peaceful day. MainActivity.The code for java and activity_main.xml is below.
MainActivity.java
package com.example.catexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// I Want to Resize and Reposition 'ImageView' by Screen Scale remain its ratio( 16 : 9)
ImageView imageView = findViewById(R.id.kitti_DD);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/kitti_DD"
android:layout_width="400dp"
android:layout_height="225dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintVertical_bias="0.3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/kittisaurus_dd" />
</androidx.constraintlayout.widget.ConstraintLayout>
ps.The cute cat's name used as an example is DD and it originated from a video of a 크집사 ,not 크림히어로즈.
Thank you from the bottom of my heart for reading the long article!😻
Upvotes: 1
Views: 204
Reputation: 1387
Set the width and height to 0dp and add the app:layout_constraintDimensionRatio
attribute and set its value to your desired aspect ratio, e.g.: 4:3, 16:9, etc.
Upvotes: 1