Backgrand image java



我想在android studio上向背景添加一个图像,但我不知道如何做到。在下面的代码中,我有公共静态的最终颜色background_Color=Color.WHITE;,但我想改变它的背景是一个图像而不是一种颜色。我该怎么做?感谢

package com.udacity.gamedev.icicles;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;

public class Constants {
    public static final float WORLD_SIZE = 10.0f;
    public static final Color BACKGROUND_COLOR = Color.WHITE;

    public static final float PLAYER_HEAD_RADIUS = 0.5f;
    public static final float PLAYER_HEAD_HEIGHT = 4.0f * PLAYER_HEAD_RADIUS;
    public static final float PLAYER_LIMB_WIDTH = 0.1f;
    public static final int PLAYER_HEAD_SEGMENTS = 20;
    public static final Color PLAYER_COLOR = Color.BLACK;
    public static final float PLAYER_MOVEMENT_SPEED = 10.0f;
    public static final float ACCELEROMETER_SENSITIVITY = 0.5f;
    public static final float GRAVITATIONAL_ACCELERATION = 9.8f;
    public static final float ICICLES_HEIGHT = 1.0f;
    public static final float ICICLES_WIDTH = 0.5f;
    public static final Vector2 ICICLES_ACCELERATION = new Vector2(0, -5.0f);
    public static final Color ICICLE_COLOR = Color.WHITE;
    public static final float HUD_FONT_REFERENCE_SCREEN_SIZE = 480.0f;
    public static final float HUD_MARGIN = 20.0f;
    public static final String ASY_LABEL = "Hillary";
    public static final String EDIUM_LABEL = "Sanders";
    public static final String ARD_LABEL = "Next";
    public static final String ONE_LABEL = "Carson";
    public static final String EASY_LABEL = "Ted";
    public static final String MEDIUM_LABEL = "Rubio";
    public static final String HARD_LABEL = "Trump";
    public static final String SAM_LABEL = "Next";
    public static final float ASY_SPAWNS_PER_SECOND = 10;
    public static final float EDIUM_SPAWNS_PER_SECOND = 15;
    public static final float ARD_SPAWNS_PER_SECOND = 20;
    public static final float ONE_SPAWNS_PER_SECOND = 5;
    public static final float EASY_SPAWNS_PER_SECOND = 10;
    public static final float MEDIUM_SPAWNS_PER_SECOND = 15;
    public static final float HARD_SPAWNS_PER_SECOND = 20;
    public static final float SAM_SPAWNS_PER_SECOND = 25;
    // TODO: Add constants for the color of each difficulty select circle
    public static final Color ASY_COLOR = new Color(0xff0000ff);
    public static final Color EDIUM_COLOR = new Color(0xff0000ff);
    public static final Color ARD_COLOR = new Color(0, 0, 0, 1);
    public static final Color ONE_COLOR = new Color(0, 0, 1, 1);
    public static final Color EASY_COLOR = new Color(0, 0, 1, 1);
    public static final Color MEDIUM_COLOR = new Color(0, 0, 1, 1);
    public static final Color HARD_COLOR = new Color(0, 0, 1, 1);
    public static final Color SAM_COLOR = new Color(0, 0, 0, 1);
    // TODO: Add constant for the size of the difficulty world
    public static final float DIFFICULTY_WORLD_SIZE = 780.0f;
    // TODO: Add constant for the radius of the difficulty select "buttons"
    public static final float DIFFICULTY_BUBBLE_RADIUS = DIFFICULTY_WORLD_SIZE / 12;
    // TODO: Add constant for the scale of the difficulty labels (1.5 works well)
    public static final float DIFFICULTY_LABEL_SCALE = 1.5f;
    // TODO: Add Vector2 constants for the centers of the difficulty select buttons
    public static final Vector2 ASY_CENTER = new Vector2(DIFFICULTY_WORLD_SIZE / 2, DIFFICULTY_WORLD_SIZE / 2);
    public static final Vector2 EDIUM_CENTER = new Vector2(DIFFICULTY_WORLD_SIZE / 3, DIFFICULTY_WORLD_SIZE / 2);
    public static final Vector2 ARD_CENTER = new Vector2(DIFFICULTY_WORLD_SIZE / 6, DIFFICULTY_WORLD_SIZE / 2);
    public static final Vector2 ONE_CENTER = new Vector2(DIFFICULTY_WORLD_SIZE* 2 / 3, DIFFICULTY_WORLD_SIZE / 2);
    public static final Vector2 EASY_CENTER = new Vector2(DIFFICULTY_WORLD_SIZE / 2, DIFFICULTY_WORLD_SIZE / 2);
    public static final Vector2 MEDIUM_CENTER = new Vector2(DIFFICULTY_WORLD_SIZE / 3, DIFFICULTY_WORLD_SIZE / 2);
    public static final Vector2 HARD_CENTER = new Vector2(DIFFICULTY_WORLD_SIZE * 2 / 3, DIFFICULTY_WORLD_SIZE / 2);
    public static final Vector2 SAM_CENTER = new Vector2(DIFFICULTY_WORLD_SIZE / 6, DIFFICULTY_WORLD_SIZE / 2);

    public enum Difficulty {
        ASY(ASY_SPAWNS_PER_SECOND, ASY_LABEL),
        EDIUM(EDIUM_SPAWNS_PER_SECOND, EDIUM_LABEL),
        ARD(ARD_SPAWNS_PER_SECOND, ARD_LABEL),
        ONE(ONE_SPAWNS_PER_SECOND, ONE_LABEL),
        EASY(EASY_SPAWNS_PER_SECOND, EASY_LABEL),
        MEDIUM(MEDIUM_SPAWNS_PER_SECOND, MEDIUM_LABEL),
        HARD(HARD_SPAWNS_PER_SECOND, HARD_LABEL),
        SAM(SAM_SPAWNS_PER_SECOND, SAM_LABEL);
        float spawnRate;
        String label;
        Difficulty(float spawnRate, String label) {
            this.spawnRate = spawnRate;
            this.label = label;
        }
    }
}

通常,如果您想将常量图像应用于布局的背景,您可以将图像复制到项目中的可绘制文件夹中,并执行以下操作:

Drawable Drawable=ContextCompat.getDrawable(MainActivity.this,R.Drawable.storm);screenLayout.setBackground(可绘制);

只需在XML布局中添加以下行即可。

android:background="@drawable/background"

然后,将您的图像添加到可绘制文件夹中。

实现这一点的最简单方法是转到布局下的XML文件并添加到activity_mainandroid:background="@drawable/background"

参见下面的示例

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background"
tools:context=".LoginActivity">

我希望它对你有用。

最新更新