选择图像时,图库视图不会显示完整的ImageView

时间:2013-04-08 19:04:16

标签: android android-gallery

在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;
    }
}

}

任何帮助都会非常感激,因为我没有最模糊的想法,我可能会出错。

2 个答案:

答案 0 :(得分:0)

使用

imageView.invalidate();

设置新图像后。 (看the Drawing section of View in here)。

或者

imageView.invalidateDrawable(imageView.getDrawable());

答案 1 :(得分:0)

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

初始化imageID后

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;
}

如果有效或无法做出回应