簡體   English   中英

在啟動畫面中顯示圖像

[英]display image in a splash screen

我是一名新手 android 開發人員。 我正在嘗試創建啟動畫面。 我有一個png圖像。 我創建了一個帶有文本框的空啟動屏幕。我有 2 個活動 - MainActivity 和 SecondActivity。

代碼:

public class MainActivity extends AppCompatActivity {


    private static int SPLASH_SCREEN_TIME_OUT=2000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i=new Intent(MainActivity.this,
                        SecondActivity.class);
                //Intent is used to switch from one activity to another.

                startActivity(i);
                //invoke the SecondActivity.

                finish();
                //the current activity will get finished.
            }
        }, SPLASH_SCREEN_TIME_OUT);
    }


}


public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
}

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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="250dp"
        android:layout_height="200dp"
        app:srcCompat="@drawable/geeks"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.447" />
</androidx.constraintlayout.widget.ConstraintLayout>

但是 app:srcCompat="@drawable/geeks" 是紅色的。 表示沒有解決。 我應該在哪里復制初始屏幕的 png 圖像? 我沒有在 android 工作室的任何地方復制圖像。 沒有圖像視圖,啟動畫面運行良好,顯示 hello world。

嘗試改寫 android: src ="@drawable/geeks"。

首先你需要把你的圖像放在res/drawable文件夾中:

在此處輸入圖像描述

然后通過app:srcCompatandroid:src屬性在您的activity_main.xml中的ImageView訪問它:

app:srcCompat="@drawable/company_logo"

android:src="@drawable/company_logo"

將它們放在assets文件夾中。 它會解決你的問題。

幾件事情要記住。

  1. 圖像大小應較小(不超過 150-200kb)

  2. 不要在 Activity 上添加文本和圖像,而是使用 Illustrator 或任何其他媒體設計啟動畫面並將其保存為 png 格式。

  3. 將其存儲在res/drawable中。

  4. 在 activity_main 中,只需添加行android:background ="@drawable/geeks"

  5. 在AndroidManifest.xml中,添加, android:theme="@style/Theme.<app name>.NoActionBar"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM