0

在 Android 中编写图库视图时,我遇到了这个问题。图像(如预期)出现在屏幕的顶部屏幕中,当它们被单击时,它们会说出选择了什么图像(再次如预期)。但是,选择的图像不会显示在 imageView 中。我的代码如下:

public class MainActivity extends Activity {

Integer[] imageIDs = {
        R.drawable.fourfromunimon,
        R.drawable.fourfromunisat,
        R.drawable.fourfromunisun,  

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Gallery gallery = (Gallery) findViewById(R.id.gallery1);

    gallery.setAdapter(new ImageAdapter(this));
    gallery.setOnItemClickListener(new OnItemClickListener()
    {
        @SuppressWarnings("rawtypes")
        public void onItemClick(AdapterView parent, View v,
                int position, long id)
        {
            Toast.makeText(getBaseContext(),
                    "pic" + (position + 1) + " selected",
                    Toast.LENGTH_SHORT).show();

            // Display the image
            ImageView imageView =
                    (ImageView) findViewById(R.id.image1);
            imageView.setImageResource(imageIDs[position]);
        }
    });
}

public class ImageAdapter extends BaseAdapter {

    Context context;

    int itemBackground;

    public ImageAdapter(Context c)
    {
        context = c;

        TypedArray a = obtainStyledAttributes(
                R.styleable.Gallery1);
        itemBackground = a.getResourceId(
        R.styleable.Gallery1_android_galleryItemBackground,
        0);
        a.recycle();
    }


public int getCount(){
    return imageIDs.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;
    if (convertView == null){
        imageView = new ImageView(context);
        imageView.setImageResource(imageIDs[position]);
        imageView.setScaleType(
                ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(
                new Gallery.LayoutParams(450, 200));
    }else{
        imageView = (ImageView) convertView;
    }
    imageView.setBackgroundResource(itemBackground);
    return imageView;
    }
}

}

任何帮助将不胜感激,因为我不知道哪里可能出错。

4

2 回答 2

0

利用

imageView.invalidate();

设置新图像后。(查看此处视图的绘图部分)。

或者

imageView.invalidateDrawable(imageView.getDrawable());
于 2013-04-08T19:25:20.213 回答
0

我的 Android 导师(先生)使用 .png 格式,尝试仅使用 .png 格式并将它们全部放在 Eclipse 项目中的所有可绘制文件夹中,如果它有效或不响应

在初始化 imageIDs 之后

Integer[] imageIDs = {
    R.drawable.fourfromunimon,
    R.drawable.fourfromunisat,
    R.drawable.fourfromunisun,  
};
declare gallery and imageview here

@Override
protected void onCreate(Bundle savedInstanceState) 
 {
  you can pass the find view by id here of gallery and imageview

并尝试使用这个...

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    // TODO Auto-generated method stub
    ImageView ivv=new ImageView(context);
    ivv.setImageResource(Garr[position]);
    ivv.setScaleType(ImageView.ScaleType.FIT_XY);
    ivv.setLayoutParams(new Gallery.LayoutParams(450, 200));
    ivv.setBackgroundResource(itembackground);
    return ivv;
}

如果它有效或不响应

于 2013-04-25T13:35:47.867 回答