【问题标题】:How to set a default loading animation on android image view如何在android图像视图上设置默认加载动画
【发布时间】:2014-02-03 14:29:20
【问题描述】:

我正在使用异步将图像加载到网格视图/列表视图中。对于每个地方下载互联网图像,我使用一个类来处理它。问题是,如何在它上面设置一个加载图标(在图像视图的中心)?谢谢

图像加载器类:

public class ImageLoader extends AsyncTask<Object, Void, Bitmap> {

    private static String TAG = "ImageLoader";
    public InputStream input;
    public ImageView view;
    public String imageURL;

    @Override
    protected Bitmap doInBackground(Object... params) {
        try {

            view = (ImageView) params[0];
            imageURL = (String) params[1];

            URL url = new URL(imageURL);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (result != null && view != null) {
            view.setImageBitmap(result);
        }
    }
}

【问题讨论】:

    标签: android android-layout android-asynctask


    【解决方案1】:

    您可以在 GridViewListView 中使用自定义布局,即包含 ImageViewRelativeLayout 和用于加载动画的 ProgressBar

    layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />
    
        <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />
    
    </RelativeLayout>
    

    然后您需要将RelativeLayout 传递给您可以从中检索的AsyncTask

    a) 你的ImageView

    b)ProgressBar

    下载完成后(例如您输入方法onPostExecute),您执行以下操作:

    @Override
    protected void onPostExecute(Bitmap result) {
        if (result != null && image != null) {
            image.setImageBitmap(result); // change 'image' to the ImageView you retreived earlier from your RelativeLayout
        }
        progressBar.setVisibility(View.GONE); // hide the ProgressBar
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多