2014-04-28 17 views
1

這裏是代碼請指導我進行更改。如何在Galleryview中設置URL的圖片

package org.androidpeople.gallery; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.AdapterView.OnItemClickListener; 

public class GalleryExample extends Activity { 

private Gallery gallery; 

private Integer[] Imgid = { 
     R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7 
}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 



    gallery = (Gallery) findViewById(R.id.examplegallery); 
    gallery.setAdapter(new AddImgAdp(this)); 

    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 

     } 
    }); 

} 

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

    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.setImageResource(Imgid[position]); 
     imgView.setLayoutParams(new Gallery.LayoutParams(500, 500)); 
     imgView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     imgView.setBackgroundResource(GalItemBg); 

     return imgView; 
    } 
} 

} 

回答

1

這裏是我的代碼爲你工作...

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 
    ImageLoader imageLoader; 
    private Gallery gallery; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final String url="http://www.hugosys.in/www.nett-torg.no/wp-content/uploads/2014/04/Lighthouse3-300x225.jpg"; 
     String [] strings={"https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg", 
      "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg", 
      "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg", 
      "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s1024/Antelope%252520Butte.jpg", 
      "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s1024/Antelope%252520Hallway.jpg", 
      "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s1024/Antelope%252520Walls.jpg", 
      "https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg", 
      "https://lh3.googleusercontent.com/-s-AFpvgSeew/URquc6dF-JI/AAAAAAAAAbs/Mt3xNGRUd68/s1024/Backlit%252520Cloud.jpg", 
      "https://lh5.googleusercontent.com/-bvmif9a9YOQ/URquea3heHI/AAAAAAAAAbs/rcr6wyeQtAo/s1024/Bee%252520and%252520Flower.jpg"}; 
     imageLoader=new ImageLoader(MainActivity.this); 
     gallery = (Gallery) findViewById(R.id.gallery1); 
     gallery.setAdapter(new AddImgAdp(this,strings)); 
    } 
    public class AddImgAdp extends BaseAdapter { 
     int GalItemBg; 
     private Context cont; 
     String [] strings; 
     public AddImgAdp(Context c,String [] strings) { 
      cont = c; 
      this.strings=strings; 
     } 

     public int getCount() { 
      return strings.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.setLayoutParams(new Gallery.LayoutParams(500, 500)); 
      imgView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      imageLoader.DisplayImage(strings[position], imgView); 
      return imgView; 
     } 
    } 
} 

這裏的圖像加載器和文件緩存處理類..

FileCache.java

ImageLoader.java

Memorycache.java

Utils.java

+0

只是ossssmmmm ... !!!! – Gattsu

+1

感謝bro @mnn – PankajSharma

2

可以使用universalImageLoaderhttp://regupathybalan.blogspot.in/2013/05/android-universal-image-loader.html

編輯答案 -

請試試這個代碼 -

package org.androidpeople.gallery; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.AdapterView.OnItemClickListener; 

public class GalleryExample extends Activity { 

private Gallery gallery; 

private Integer[] Imgid = { 
     R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7 
}; 


protected ImageLoader imageLoader = ImageLoader.getInstance(); 
    private DisplayImageOptions options; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


     imageLoader.init(ImageLoaderConfiguration 
       .createDefault("Your activity context")); 
     options = new DisplayImageOptions.Builder().cacheInMemory(true) 
       .cacheOnDisc(true).considerExifParams(true) 
       .bitmapConfig(Bitmap.Config.RGB_565).build(); 

    gallery = (Gallery) findViewById(R.id.examplegallery); 
    gallery.setAdapter(new AddImgAdp(this)); 

    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 

     } 
    }); 

} 

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

    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.setImageResource(Imgid[position]); 
     imgView.setLayoutParams(new Gallery.LayoutParams(500, 500)); 
     imgView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     imageLoader.displayImage(
          "Your Image URL", 
          imgView, options); 

     return imgView; 
    } 
} 

} 

希望這代碼可以幫助您按照本教程! 如果它不工作,請讓我知道我會盡力幫助你。

+1

親愛的我看不到Galleryview那裏... !!!! – Gattsu

+0

你可以使用這段代碼在imageview中設置圖片的url。並且對於圖庫視圖,您必須使用基礎適配器。 –

+0

我可以有代碼嗎? – Gattsu