[Android] การสร้าง Intro ก่อนเข้าหน้าแอพด้วย Splash Screen

Mobile Application Developing- Android, iOS, Window Phone สอนเขียนโปรแกรมบนมือถือ ระบบปฏิบัติการต่าง แอนดรอยด์ ไอโอเอส วินโดโฟน สอนเขียนโปรแกรมบนมือถือ

Moderator: mindphp, ผู้ดูแลกระดาน

AePongsak
PHP VIP Members
PHP VIP Members
โพสต์: 4996
ลงทะเบียนเมื่อ: 17/09/2016 10:09 am

[Android] การสร้าง Intro ก่อนเข้าหน้าแอพด้วย Splash Screen

โพสต์ที่ยังไม่ได้อ่าน โดย AePongsak »

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:background="#3b4534" >
จะเป็นสีของพื้นหลัง
_______________________________________

โค้ด: เลือกทั้งหมด

android:src="@drawable/queen" />
จะเป็นรูปภาพที่เรานำเข้ามาใส่ไว้ใน Android Studio
1.jpg
1.jpg (18.84 KiB) Viewed 1551 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);
    }
}
___________________________________________________________________
รูปภาพ
ξν℮ry story has an end but in life every end is a neω ßeginning.
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 69