Splash Screen คือหน้าต่าง UI ที่จะแสดงเป็นอันดับแรกเมื่อกด รัน จากนั้นก็จะเข้าสู่หน้าหลักอัตโนมัติ ส่วมมากใช้แสดงโลโก้ ...
สร้างโปรเจ็ค
Android Studio โดยจะประกอบไปด้วย
-
MainActivity.java
-
SplashScreen.java
ส่วนนี้จะเป็นส่วนของโค้ด
-
activity_main.xml
-
splashsceen.xml
ส่วนนีจะเป็นส่วนของการออกแบบหน้าจอ
ก่อนอื่นออกแบบหน้าจอที่เป็นหน้าแรกก่อน
โค้ดใน
splashsceen.xml
โค้ด: เลือกทั้งหมด
<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:background="#3b4534" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/toon" />
</RelativeLayout>
เราสามารถออกแบบหน้าจอนี้ได้ตามใจชอบ
จะเป็นสีของพื้นหลัง
_______________________________________
จะเป็นรูปภาพที่เรานำเข้ามาใส่ไว้ใน Android Studio
- 1.jpg (18.84 KiB) Viewed 1574 times
สามารถลากรูปเข้ามาไว้ใน Drawable ได้เลย
_______________________________________
โค้ดใน
SplashScreen.java
โค้ด: เลือกทั้งหมด
public class SplashScreen extends MainActivity
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable)){
@Override
public void run() {
Intent splashIntent = new Intent(SplashScreen.this,MyActivity.class);
SplashScreen.this.startActivity(splashIntent);
SplashScreen.this.finish();
}
},SPLASH_TIME_OUT);
}
}
*กำหนดไว้ SPLASH_TIME_OUT = 3000 หรือประมาณ 3 วินาที 1000 = 1 วินาที
____________________________________________________________________
ในหน้า AndroidManifest.xml แทรก Activity ที่จะทำการ รัน เป็นหน้าแรก SplassScreen
โค้ด: เลือกทั้งหมด
<activity android:name=”.SplashScreenActivity” android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.MainActivity”>
</activity>
____________________________________________________________________
โค้ดใน
activity_main.xml
โค้ด: เลือกทั้งหมด
<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:background="#3b92d1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Main Page"
android:textColor="#BBBBBB"
android:textSize="10sp" />
</RelativeLayout>
____________________________________________________________________
โค้ดใน MainActivity.java
โค้ด: เลือกทั้งหมด
import android.os.Bundle;
import android.view.Window;
import android.app.Activity;
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
}
___________________________________________________________________
Splash Screen คือหน้าต่าง UI ที่จะแสดงเป็นอันดับแรกเมื่อกด รัน จากนั้นก็จะเข้าสู่หน้าหลักอัตโนมัติ ส่วมมากใช้แสดงโลโก้ ...
สร้างโปรเจ็ค [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/3505-android-studio.html]Android Studio[/url] โดยจะประกอบไปด้วย
- [b]MainActivity.java[/b]
- [b]SplashScreen.java[/b]
ส่วนนี้จะเป็นส่วนของโค้ด
- [b]activity_main.xml[/b]
- [b]splashsceen.xml[/b]
ส่วนนีจะเป็นส่วนของการออกแบบหน้าจอ
ก่อนอื่นออกแบบหน้าจอที่เป็นหน้าแรกก่อน
โค้ดใน [u]splashsceen.xml[/u]
[code]<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:background="#3b4534" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/toon" />
</RelativeLayout>[/code]
เราสามารถออกแบบหน้าจอนี้ได้ตามใจชอบ
[code] android:background="#3b4534" >[/code]
จะเป็นสีของพื้นหลัง
_______________________________________
[code]android:src="@drawable/queen" />[/code]
จะเป็นรูปภาพที่เรานำเข้ามาใส่ไว้ใน Android Studio
[attachment=0]1.jpg[/attachment]
สามารถลากรูปเข้ามาไว้ใน Drawable ได้เลย
_______________________________________
โค้ดใน [u]SplashScreen.java[/u]
[code]public class SplashScreen extends MainActivity
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable)){
@Override
public void run() {
Intent splashIntent = new Intent(SplashScreen.this,MyActivity.class);
SplashScreen.this.startActivity(splashIntent);
SplashScreen.this.finish();
}
},SPLASH_TIME_OUT);
}
}[/code]
[color=#FF0000]
*กำหนดไว้ SPLASH_TIME_OUT = 3000 หรือประมาณ 3 วินาที 1000 = 1 วินาที[/color]
____________________________________________________________________
ในหน้า AndroidManifest.xml แทรก Activity ที่จะทำการ รัน เป็นหน้าแรก SplassScreen
[code]
<activity android:name=”.SplashScreenActivity” android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.MainActivity”>
</activity>[/code]
____________________________________________________________________
โค้ดใน [u]activity_main.xml [/u]
[code]<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:background="#3b92d1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Main Page"
android:textColor="#BBBBBB"
android:textSize="10sp" />
</RelativeLayout>[/code]
____________________________________________________________________
โค้ดใน MainActivity.java
[code]
import android.os.Bundle;
import android.view.Window;
import android.app.Activity;
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
}[/code]
___________________________________________________________________