在图库中如何给android中的每个图像赋予不同的背景

时间:2012-03-01 11:47:24

标签: android android-emulator

我正在做一个应用程序在这里我需要显示图像horiznatally.so使用库我显示图像。但我需要不同的背景到gallery.i中的每个图像我不知道该怎么办。  我给了画廊的背景,但单个相同的背景来到整个画廊。但我需要不同的背景到每个图像。任何人帮助我。我使用下面的代码treid。

       example .class:
        public class example extends Activity {
/** Called when the activity is first created. */
  private Gallery gallery;
           private ImageView imgView;
      private Integer[] Imgid = {
        R.drawable.popup2, 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));
     }
        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;

    }

            @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
          ImageView imgView = new ImageView(cont);
                    imgView.setImageResource(Imgid[position]);
            imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
               imgView.setScaleType(ImageView.ScaleType.FIT_XY);
              imgView.setBackgroundResource(GalItemBg);
                return imgView;
    }
       }
    }

3 个答案:

答案 0 :(得分:1)

查看ImageAdapter的这个示例。你应该有一些列表,其中包含对drawables中图像的引用,并使用imageView.setImageResource(mImageIds [position])将它们设置在适配器的getView方法中;

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7
    };

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        attr.recycle();
    }

    public int getCount() {
        return mImageIds.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 imageView = new ImageView(mContext);

        imageView.setImageResource(mImageIds[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
}

答案 1 :(得分:0)

我认为GalItemBg变量是在适配器类的构造函数中初始化的,调用getView方法时它不会更改,因此你有相同的背景。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView imgView = new ImageView(cont);
    imgView.setImageResource(Imgid[position]);
    imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(Imgid[position]);
    return imgView;
}

答案 2 :(得分:0)

您必须使用适配器的getView方法来设置不同Imageview的背景图像 //存储背景图像 Integer [] backGroundImage = {R.drawable.background1,R.drawable.background2,R.drawable.background3};

public View getView(int position,View convertView,ViewGroup parent){

ImageView imageView = new ImageView(cont);
 -----
 -----
 ----

//设置地面图像     imageView.setBackgroundResource(将backgroundImage [COUNT]); }