Home > Mobile >  How to get the size of an image in Android?
How to get the size of an image in Android?

Time:12-14

I want to use an image in my application with Android studio, I want the image I use to be the same on all screens with different dpi / different resolutions. I want it to fill the full screen in width and height.

I'm using figma as a design tool, my question is, how will I determine the width and height ratio of this picture in "figma"? I'm confused, should I specify it as "dp" value (411 x 731) or resolution (1080 x 1920) in Figma, any help?

Image : should be compatible with mdpi,xhdpi,xxhdpi,xxxhdpi values

Phone : Nexus5x (411 x 731 dp, 420 dpi) / 1080 x 1920 resolution

CodePudding user response:

You can use constraint layout for your task. Take imageview inside it and set dimension ratio and width percentage according to your requirements. It will set the image according to device height and width. Hope this helps

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <ImageView
            android:id="@ id/imgMainIcon"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintWidth_percent="0.70"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
        
    </androidx.constraintlayout.widget.ConstraintLayout>

Or if you don't want to use constraint layout then simply try setting scale type in imageview like this

android:scaleType="centerCrop"

CodePudding user response:

if you are using image view then simply use this way.

int width=imageView.getWidth();
int height=imageView.getHeight();
  • Related