Original image (left) and screen sample (right)
Please prepare an image. I think the one that is copyright-free or the one that you created yourself is good. Please check the usage restrictions even if it is copyright-free. illustAC illust image Irasutoya etc. There seem to be several ways to display an image, but this article ends by putting the file in drawable and writing it to ImageView.
I prepared a project called TestImageView for practice. app → res → drawable Right-click drawable, click ** Show in Explorer ** to expand the ** drawable-v24 folder **, and place the image you want to use here. Open the drawable folder in Android Studio and it's OK if you have the image file. It seems that Java's variable naming convention is working. Files that start with a numerical value cannot be placed. In that case, change the file name.
actixity_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:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/momizi"/>
</androidx.constraintlayout.widget.ConstraintLayout>
This screen is displayed with the above code. Hey, the top and bottom sizes don't match. .. It's okay, how pretty. Please proceed down. If you get the error "java.lang.RuntimeException: Canvas: trying to draw too large (****** bytes) bitmap." At this stage, the image size is large. Let's make the image smaller. Online image resizing ImageView.ScaleType This article on Qiita will be helpful. [Image View] Scale Type and display image correspondence table I added android: scaleType = "centerCrop" to the ImageView in the above code.
actixity_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:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/momizi"
android:scaleType="centerCrop"/>
</androidx.constraintlayout.widget.ConstraintLayout>
This screen is displayed with the above code. Both top, bottom, left and right fit the screen!
nyan app development ([Android] ImageView 3 ways to display images) nyan app development ([Android] To fit ImageView image to Screen layout) [Image View] Scale Type and display image correspondence table
Recommended Posts