启动活动缓慢且新的活动为空。

5
这是我的第一个安卓应用,我正在从iOS开发转换过来。我试图切换到另一个活动页面,但是需要几秒钟(大约3秒)才能看到新的空白活动页面。
以下是我用于切换到新活动页面的代码:
public void openStats(View v) {
    startActivity(new Intent(MainActivity.this, StatsActivity.class));
}

这是我的新活动的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上进行测试。

下面是打开新活动时的日志:

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>

1
如果您删除 setRequestedOrientation() 调用会怎样?加载新活动仍然需要很长时间吗? - Code-Apprentice
2
你的设备通过 USB 连接到开发电脑了吗?如果你断开连接并运行你的应用程序会发生什么? - Code-Apprentice
1
@james,请检查设备上的开发者选项,您可能实际上已经将动画速度改为5倍慢了 :-) - Blundell
2
你加载了一个XML文件 setContentView(R.layout.stats_page);,那个文件里面有什么?如果只是默认的话,那就是你的设备了 :) - Blundell
2
就像我说的,stats_page.xml里面是什么? - Blundell
显示剩余15条评论
3个回答

3

2
为了解决资源加载缓慢的问题,我使用了一个自定义视图,使用Glide图片加载库来显示图片。
public class GlideImageView
        extends ImageView {
    public GlideImageView(Context context) {
        super(context);
        init(context, null, -1);
    }

    public GlideImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, -1);
    }

    public GlideImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @TargetApi(21)
    public GlideImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr);
    }

    int drawableResource = 0;

    private void init(Context context, AttributeSet attributeSet, int defStyle) {
        TypedArray a = null;
        if(defStyle != -1) {
            a = getContext().obtainStyledAttributes(attributeSet, R.styleable.GlideImageView, defStyle, 0);
        } else {
            a = getContext().obtainStyledAttributes(attributeSet, R.styleable.GlideImageView);
        }
        drawableResource = a.getResourceId(0, 0);
        a.recycle();

        ViewUtils.waitForMeasure(this, new ViewUtils.OnMeasuredCallback() {
            @Override
            public void onMeasured(View view, int width, int height) {
                if(!isInEditMode()) {
                    if(drawableResource != 0) {
                        Glide.with(getContext()).load(drawableResource).dontAnimate().into(GlideImageView.this);
                    }
                } else {
                    setImageResource(drawableResource);
                }
            }
        });
    }
}

并且

public final class ViewUtils {
    public interface OnMeasuredCallback {
        void onMeasured(View view, int width, int height);
    }

    public static void waitForMeasure(final View view, final OnMeasuredCallback callback) {
        int width = view.getWidth();
        int height = view.getHeight();

        if (width > 0 && height > 0) {
            callback.onMeasured(view, width, height);
            return;
        }

        view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override public boolean onPreDraw() {
                final ViewTreeObserver observer = view.getViewTreeObserver();
                if (observer.isAlive()) {
                    observer.removeOnPreDrawListener(this);
                }

                callback.onMeasured(view, view.getWidth(), view.getHeight());

                return true;
            }
        });
    }

    private ViewUtils() {
    }
}

并且

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="GlideImageView">
        <attr name="image_resource" format="integer"/>
    </declare-styleable>
</resources>

然后可以用作

        <the.package.GlideImageView
                android:id="@+id/image"
                android:layout_width="120dp"
                android:layout_height="120dp"
                app:image_resource="@drawable/image"/>

使用依赖项
compile('com.github.bumptech.glide:glide:3.7.0') {
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
}

1
唯一导致应用程序变慢的原因是一个1200x1200像素的图像被缩小到大约200x200像素。当我将其移除后,整个应用程序又快了。这是常见现象吗?安卓系统不能很好地处理缩放吗?无论如何,感谢您的帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接