android - 启动Activity慢,新的Activity为空

标签 android performance

这是我的第一个 Android 应用程序,我正在从 iOS 开发过渡。 我正在尝试更改为不同的 Activity 。但是,我的新空 Activity 需要多秒(大约 3 秒)才能出现。

这是我要发送到新 Activity 的代码:

public void openStats(View v) {
    startActivity(new Intent(MainActivity.this, StatsActivity.class));
}

这是我的新 Activity 的 onCreate:

public class StatsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stats_page);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

我不明白为什么要花这么长时间?我也在三星 Galaxy S5 上进行测试。

这是打开新 Activity 时的日志:

D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
I/Timeline: Timeline: Activity_launch_request id:com.example.NAME.reflexmath time:53311650
W/ResourcesManager: getTopLevelResources:/data/app/APPNAME2/base.apk / 1.0 running in APPNAME rsrc of package null
D/SecWifiDisplayUtil: Metadata value : none
D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{a1db7f7 I.E...... R.....ID 0,0-0,0}
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@58ff7f6 time:53313401

stats_page.xml:

<LinearLayout 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="fill_parent" 
android:layout_height="fill_parent" 
android:baselineAligned="false" 
android:orientation="vertical" 
android:background="@android:color/holo_blue_light"> 

<LinearLayout 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="0.1"></LinearLayout> 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="0dp" 
android:layout_weight="0.8"> 

<ImageView 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
app:srcCompat="@drawable/reflexmath5" 
android:id="@+id/imageView10" 
android:layout_weight="1" 
android:layout_gravity="center" 
android:cropToPadding="false" 
android:adjustViewBounds="false" 
android:scaleType="fitCenter" /> 
</LinearLayout> 

<LinearLayout 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="0.1"></LinearLayout> 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="0dp" 
android:layout_weight="0.7" 
android:orientation="horizontal" 
android:layout_marginLeft="15dp" 
android:layout_marginRight="15dp" 
android:weightSum="1" 
android:id="@+id/progView"> 

<ImageView 
android:layout_width="0dp" 
android:layout_height="match_parent" 
app:srcCompat="@drawable/hbmlogo" 
android:id="@+id/imageView11" 
android:elevation="1dp" 
android:layout_weight="0.3" 
android:scaleType="fitCenter" /> 

<ProgressBar 
style="@style/Widget.AppCompat.ProgressBar.Horizontal" 
android:layout_height="match_parent" 
android:id="@+id/xpBar" 
android:scaleY="5" 
android:progress="50" 
android:layout_weight="0.7" 
android:layout_width="0dp" 
android:layout_marginLeft="-13dp" /> 
</LinearLayout> 

<LinearLayout 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="0.25"> 

<TextView 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
android:text="HIGH SCORE 3456" 
android:textColor="@android:color/background_light" 
android:textSize="1sp" 
android:id="@+id/highScoreLbl" /> 
</LinearLayout> 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="0dp" 
android:layout_weight="1" > 

<ImageButton 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
app:srcCompat="@drawable/play" 
android:id="@+id/imageButton9" 
android:layout_weight="1" 
android:scaleType="fitCenter" 
android:background="@android:color/transparent" /> 

<ImageButton 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
app:srcCompat="@drawable/stats" 
android:id="@+id/imageButton10" 
android:layout_weight="1" 
android:scaleType="fitCenter" 
android:background="@android:color/transparent" 
android:onClick="openStats" /> 
</LinearLayout> 

<LinearLayout 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="0.1"></LinearLayout> 

<LinearLayout 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="0.4"> 

<ImageButton 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
app:srcCompat="@drawable/audioon" 
android:id="@+id/imageButton12" 
android:layout_weight="1" 
android:background="@android:color/transparent" 
android:scaleType="fitCenter" /> 

<ImageButton 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
app:srcCompat="@drawable/adson" 
android:id="@+id/imageButton13" 
android:layout_weight="1" 
android:background="@android:color/transparent" 
android:scaleType="fitCenter" /> 
</LinearLayout> 

<LinearLayout 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="0.1"></LinearLayout> 

</LinearLayout>

最佳答案

使用大图像并允许系统缩放它们将占用大量内存,因此在某些/大多数设备上速度很慢。

您应该为可用的屏幕尺寸提供正确尺寸的图像,这样需要的缩放比例最少。

在这里阅读更多:https://developer.android.com/guide/practices/screens_support.html

关于android - 启动Activity慢,新的Activity为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41402901/

相关文章:

php - 如何有效地从数组中获取特定值?

java - Dagger 与二传手进行测试

Android TabLayout tabPaddingTop 和 tabPaddingBottom 未被删除

java - android捕获的视频质量差,缓冲视频时出错

android - 检查屏幕是否处于无 Activity 的多窗口模式

algorithm - 从复杂性估计程序执行时间

performance - React - 解构时的 defaultProps 与 ES6 默认参数(性能问题)

sql-server - 请帮我做这个查询(sql server 2008)

java - 为什么 ConcurrentHashMap::putIfAbsent 比 ConcurrentHashMap::computeIfAbsent 快?

android - 如何在操作栏中隐藏搜索 View