简体   繁体   中英

Android: Show Screen while loading

I am using AsyncTask to get data from a server and want to show a Screen while it is loading like this:

private class GetListTask extends AsyncTask {
    protected void onPreExecute() {
            Intent intent = new Intent();
                intent.setClass(Start.this, Wait.class);
                startActivity(intent);
    }

    protected Bitmap doInBackground(Object... args) {

//here i get the data from the server

        d = getImageFromURL(Start.valuesArray[Integer.parseInt(Value)][7]);
        Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
        double ratio = (double) bitmap.getWidth() / (double) bitmap.getHeight();
        Bitmap scaled = Bitmap.createScaledBitmap(bitmap, width, (int) (width / ratio), true);
        return scaled;
    }

    protected void onPostExecute(Object objResult) {
        if (objResult != null && objResult instanceof Bitmap) {
            Bitmap result = (Bitmap) objResult;

            im.setImageBitmap(result);
        }
    }
}

Now, in onPostExecute() I need to hide this "Wait" Screen again, but how do I do that? Thanks for helping!

您可以在PopupDialog中显示图像并在onPostExecute中将其关闭,而不是开始其他活动。

Use This Layout instead ...

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!--  Your actual layout. width & height fill_parent -->

    <ImageView 
        android:id="@+id/wait_image"
        android:layout_width="fill_parent"
        android:visibility="gone"
        android:layout_height="fill_parent"
        android:src="@drawable/wait_screen"
    />
</FrameLayout>

Now on preExecute() show the waiting imageview and hide the actual layout.

And on postExecute() hide the imageView & show the actual layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM