启动画面未显示在移动设备的整个屏幕上
splash screen is not showing on whole screen on mobile
我正在开发 android 项目,其中我正在处理闪屏代码,但闪屏图像没有显示在我手机的整个屏幕上 我的 activity 代码是 :-
public class SplashPresenter extends MvpBasePresenter<SplashView> {
@Override public void attachView(SplashView view) {
super.attachView(view);
new Handler().postDelayed(new Runnable() {
@Override public void run() {
launch();
}
}, 2000);
}
@Override public void detachView(boolean retainInstance) {
super.detachView(retainInstance);
}
private void launch() {
String imei = ImeiUtil.getIMEI();
List<ActivationInfo> infos = ActivationInfo.listAll(ActivationInfo.class);
if (infos.size() == 1) {
ActivationInfo info = infos.get(0);
/*
Log.d("I: ", "I: " + info.getBusinessName());
Log.d("I: ", "I: " + info.getMobileNumber());
Log.d("I: ", "I: " + info.getImei());
Log.d("I: ", "I: " + imei);
Log.d("I: ", "S: " + info.getStatus());
*/
if (info.getStatus() == 1 && info.getImei().equalsIgnoreCase(imei)) {
getView().start(HomeActivity.class);
} else {
//getView().start(ActivationActivity.class);
getView().start(HomeActivity.class);
}
} else {
getView().start(HomeActivity.class);
// getView().start(RegisterActivity.class);
}
}
和 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"
tools:context=".MainActivity">
<ImageView
android:contentDescription="@string/splash_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/splash_english"/>
</RelativeLayout>
<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"
tools:context=".MainActivity">
<ImageView
android:contentDescription="@string/splash_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/splash_english"
android:minHeight="1000dp"
android:minWidth="1000dp"/>
</RelativeLayout>
如果您只想让图像填满屏幕,请将 android:scaleType 更改为 fitXY 之类的内容。依靠 minHeight 和 minWidth 并使用 centerCrop 不会很好地覆盖多种设备尺寸。更好的解决方案,开发具有更好尺寸的图像或使用基于 xml 的可绘制对象
在 Activity
的 onCreate 方法中添加这一行
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
以正确的方式实现启动画面与您想象的有点不同。您看到的初始视图必须立即准备就绪,甚至在您可以在初始页面中扩充布局文件之前 activity。您不会使用布局文件。相反,将启动画面的背景指定为 activity 的主题背景。为此,首先在 res/drawable 中创建一个 XML 可绘制对象。它是 xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/gray"/>
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
接下来,您将在主题中将其设置为启动画面 activity 的背景。导航到您的 styles.xml 文件并为您的启动添加一个新主题 activity:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
</resources>
您的 SplashActivity class 应该会将您转到主 activity:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
完整的源代码可以在这里下载https://github.com/shubhamsharmacs/Splash
我正在开发 android 项目,其中我正在处理闪屏代码,但闪屏图像没有显示在我手机的整个屏幕上 我的 activity 代码是 :-
public class SplashPresenter extends MvpBasePresenter<SplashView> {
@Override public void attachView(SplashView view) {
super.attachView(view);
new Handler().postDelayed(new Runnable() {
@Override public void run() {
launch();
}
}, 2000);
}
@Override public void detachView(boolean retainInstance) {
super.detachView(retainInstance);
}
private void launch() {
String imei = ImeiUtil.getIMEI();
List<ActivationInfo> infos = ActivationInfo.listAll(ActivationInfo.class);
if (infos.size() == 1) {
ActivationInfo info = infos.get(0);
/*
Log.d("I: ", "I: " + info.getBusinessName());
Log.d("I: ", "I: " + info.getMobileNumber());
Log.d("I: ", "I: " + info.getImei());
Log.d("I: ", "I: " + imei);
Log.d("I: ", "S: " + info.getStatus());
*/
if (info.getStatus() == 1 && info.getImei().equalsIgnoreCase(imei)) {
getView().start(HomeActivity.class);
} else {
//getView().start(ActivationActivity.class);
getView().start(HomeActivity.class);
}
} else {
getView().start(HomeActivity.class);
// getView().start(RegisterActivity.class);
}
}
和 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"
tools:context=".MainActivity">
<ImageView
android:contentDescription="@string/splash_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/splash_english"/>
</RelativeLayout>
<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"
tools:context=".MainActivity">
<ImageView
android:contentDescription="@string/splash_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/splash_english"
android:minHeight="1000dp"
android:minWidth="1000dp"/>
</RelativeLayout>
如果您只想让图像填满屏幕,请将 android:scaleType 更改为 fitXY 之类的内容。依靠 minHeight 和 minWidth 并使用 centerCrop 不会很好地覆盖多种设备尺寸。更好的解决方案,开发具有更好尺寸的图像或使用基于 xml 的可绘制对象
在 Activity
的 onCreate 方法中添加这一行getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
以正确的方式实现启动画面与您想象的有点不同。您看到的初始视图必须立即准备就绪,甚至在您可以在初始页面中扩充布局文件之前 activity。您不会使用布局文件。相反,将启动画面的背景指定为 activity 的主题背景。为此,首先在 res/drawable 中创建一个 XML 可绘制对象。它是 xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/gray"/>
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
接下来,您将在主题中将其设置为启动画面 activity 的背景。导航到您的 styles.xml 文件并为您的启动添加一个新主题 activity:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
</resources>
您的 SplashActivity class 应该会将您转到主 activity:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
完整的源代码可以在这里下载https://github.com/shubhamsharmacs/Splash