簡體   English   中英

Android的懶加載畫廊問題

[英]android lazy load gallery issue

我試圖異步地在asynctask中下載一些圖像,然后在onPostExecute()中將圖像設置為圖庫適配器。 我已經硬編碼了要在asyntask中下載的圖像,因為我什至無法到達將圖像傳遞給適配器的構造函數的部分:/沒有引發錯誤,並且postexecute Log()不會將任何內容發布到logcat

有人可以告訴我我要去哪里錯嗎?

公共類GallerythreadedtestActivity擴展了Activity {

private Gallery gallery;
private AddImgAdp adapter;
private Drawable[] image;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new downloadImages().execute();
}

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    // Adding images.
    private Integer[] Imgid = {
    R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon
    };

    public AddImgAdp(Context c) {
    cont = c;
    TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
    GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
    typArray.recycle();
    }

    public int getCount() {
    return Imgid.length;
    }

    public Object getItem(int position) {
    return position;
    }

    public long getItemId(int position) {
    return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imgView = new ImageView(cont);

    imgView.setImageDrawable(image[position]);
    // Fixing width & height for image to display
    imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);

    return imgView;
    }
    }

public class downloadImages extends AsyncTask<String, Void, Void> {

    protected void onPreExecute(){
        image=new Drawable[2];
        adapter = new AddImgAdp(getApplicationContext());
        gallery = (Gallery) findViewById(R.id.examplegallery);
    }

    protected void onProgressUpdate(Void... v){

    }

    protected Void doInBackground(final String... args) {


        image[0] = ImageOperations(getApplicationContext(),"http://t3.gstatic.com/images?q=tbn:ANd9GcQidl6KX2jRWNeCA6jT_TjWG7NlI3aRiB_AcDsA9Y5owS2cr9G6","image.jpg");
        image[1] = ImageOperations(getApplicationContext(),"http://t3.gstatic.com/images?q=tbn:ANd9GcQidl6KX2jRWNeCA6jT_TjWG7NlI3aRiB_AcDsA9Y5owS2cr9G6","image.jpg");

        return null;

    }

    protected void onPostExecute(){
        Log.d("onpost","");
        gallery.setAdapter(adapter);
        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
            Toast.makeText(GallerythreadedtestActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show();
            }
            });
    }

}

private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}

}

我可以給您一個建議,也可以嘗試作為補充解決方案,它更好地延遲加載圖像: ListView中的圖像延遲加載 如果您將xml中的list view元素更改為Gallery視圖,那么我認為這將解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM