[Android Studio] Set an arbitrary image for the application background [Java]

Original image (left) and screen sample (right)

Introduction

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.

drawable folder

I prepared a project called TestImageView for practice. image.png 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.

.xml file

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!

reference

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

[Android Studio] Set an arbitrary image for the application background [Java]
[Android development] Get an image from the server in Java and set it in ImageView! !!
Java sets the background color and background image for PowerPoint documents
[Swift] How to set an image in the background without using UIImageView.
Android Studio shows only one method for button onClick and Java throws an exception and ruins the app
Check the options set for the running Java process
Android Studio development for the first time (for beginners)
[Java] (for MacOS) How to set the classpath
What did I prepare for when I entered an Android application development project while developing the Web in Java?
[Android Studio] [Java] How to fix the screen vertically
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
Mandels to create an image of the Mandelbrot set
Build an Android image for Orange Pi 4 with Docker
[Android studio / Java] What you don't understand when you touch it for the first time
Refer to C ++ in the Android Studio module (Java / kotlin)
[Mac] Android Studio quits for an unexpected reason during installation
Java application for beginners: stream
JavaFX-Load Image in the background
I tried to make an Android application with MVC now (Java)
[Android Studio] [For beginners] Let's roughly explain the screen and directories
[Android] Dynamically set the height of ListView with copy (for beginners)
Notes for Android application development beginners
[Android Studio] [Java] Learning memos & links
Reintroducing Java 8 available from Android Studio 2.4
Run an application made with Java8 with Java6
I tried to summarize the stumbling points when developing an Android application
[AWS SDK for Java] Set a retry policy on the S3 client