【问题标题】:Add an image to an android splash screen in intellij android studio? [duplicate]将图像添加到 intellij android studio 中的 android 启动画面? [复制]
【发布时间】:2014-10-26 03:57:57
【问题描述】:

好的,我想做的是在启动画面中添加一张图片,这样它就会在应用启动之前显示图片。我认为(?)我找到了正确的代码来实际执行启动屏幕,但我无法在其中获取图像。从我读过的内容来看,它需要在一个 png 文件中,但我如何将它从我计算机上的文件移动到代码中,然后我从那里去哪里?

【问题讨论】:

  • I think(?) I found the right code to actually do the splash screen 请注意解释您的尝试。这将为我们提供一个起点,帮助解释如何从您所在的位置继续。
  • stackoverflow.com/questions/5486789/… 这是我试过的代码
  • 那么你需要做的就是把你的图片放在app/main/src/res/drawable文件夹中,并命名为spalsh.png。我强烈建议您阅读 Android Studio 项目的目录结构。
  • @AhmadVatani 请发布一个总结您的解决方案的答案。

标签: android intellij-idea android-studio png splash-screen


【解决方案1】:

假设您有完全How do I make a splash screen? 中给出的代码,那么您只需将您的图片保存为splash.pngapp/main/src/res/drawable 文件夹中。确保在运行之前清理并重建您的项目。请注意,您可以为 PNG 指定任何您想要的名称。只需更改 android:src="@drawable/splash" 中的 splash 以匹配您使用的名称。另外,我强烈建议您了解 Android Studio 项目中的目录结构。

【讨论】:

    【解决方案2】:

    我的帖子here 回答了这个问题。

    要将图像从文件“移动”到您的代码中,您需要将其放入您的可绘制文件夹中,然后使用它在某个地方引用它

    @drawable/image
    

    为了更好地了解如何更改初始屏幕图像,请阅读以下内容。

    添加启动画面

    首先,您需要一个启动画面图像。因为安卓设备进来了 各种分辨率,您可能希望将多个启动画面作为 谷歌支持多屏的最佳实践中描述。 为简单起见,我们将只在此处发送一个 480x800 的分辨率。这应该 很好地支持大多数手机尺寸,Android 会将其缩放为 尽力而为。

    将您想要的图像/gif 添加到 Resources\Drawable

    您需要在 layout.xml 文件中定义启动画面

    <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    
              <ImageView id="@+id/splashscreen" android:layout_width="wrap_content"
                      android:layout_height="fill_parent"
                      android:src="@drawable/splash"
                      android:layout_gravity="center"/>
    
              <TextView android:layout_width="fill_parent"  <!-- Not needed->-->
                        android:layout_height="wrap_content"
                        android:text="Hello World, splash"/> <!--Not Needed -->
    
      </LinearLayout>
    

    还有你的活动:

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    
    public class Splash extends Activity {
    
        /** Duration of wait **/
        private final int SPLASH_DISPLAY_LENGTH = 1000;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.splashscreen);
    
            /* New Handler to start the Menu-Activity 
             * and close this Splash-Screen after some seconds.*/
            new Handler().postDelayed(new Runnable(){
                @Override
                public void run() {
                    /* Create an Intent that will start the Menu-Activity. */
                    Intent mainIntent = new Intent(Splash.this,Menu.class);
                    Splash.this.startActivity(mainIntent);
                    Splash.this.finish();
                }
            }, SPLASH_DISPLAY_LENGTH);
        }
    }
    

    【讨论】:

    • 那是 C# 吗?我认为 Java 答案会更有帮助,因为 OP 用android-studio 标记了这个问题。
    • 是的,最后一部分是用 C# 编写的,我差点忘了说这需要Xamarin 才能使用。我将更正代码以在 android studio 中工作
    • 如果你想节省时间,这里已经有一个 Java 答案。在问题的 cmets 中链接到它的 OP,我已投票关闭作为副本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多