[英]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"。
將它們放在assets
文件夾中。 它會解決你的問題。
幾件事情要記住。
圖像大小應較小(不超過 150-200kb)
不要在 Activity 上添加文本和圖像,而是使用 Illustrator 或任何其他媒體設計啟動畫面並將其保存為 png 格式。
將其存儲在res/drawable
中。
在 activity_main 中,只需添加行android:background ="@drawable/geeks"
。
在AndroidManifest.xml中,添加, android:theme="@style/Theme.<app name>.NoActionBar"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.