Android:GridView在倾斜时显示图像

时间:2013-12-04 13:46:43

标签: java android image gridview

我正在尝试让GridView显示来自指定文件的所有图像。

然而,图像并没有出现在网格中,而是看起来很糟糕......请参见截图

screenshot

我不知道造成这种情况的原因是它的代码还是与图像尺寸有关。

任何关于代码解决方案的帮助都会很棒

Bellow是GridView和适配器的代码

public class MyWallpapers extends Activity {


private ImageAdapter imageAdapter;

ImageView Selected;
String ImageLocation = null;


ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wallpaper_view_activity);


    getFromSdcard();
    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);


    imagegrid.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {


            //Toast.makeText(MyWallpapers.this, "" + listFile[position].getAbsolutePath(), Toast.LENGTH_SHORT).show();


            Intent intent = new Intent(MyWallpapers.this, WallpaperCrop.class);

            intent.putExtra("FilePath",listFile[position].getAbsolutePath());
            startActivity(intent);
        }
    });
}


public void getFromSdcard()
{
    File file= new File(android.os.Environment.getExternalStorageDirectory(),"Wallpaper");

        if (file.isDirectory())
        {
            listFile = file.listFiles();


            for (int i = 0; i < listFile.length; i++)
            {

                f.add(listFile[i].getAbsolutePath());

            }
        }
}

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return f.size();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }


        Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
        holder.imageview.setImageBitmap(myBitmap);
        return convertView;
    }
}
class ViewHolder {
    ImageView imageview;


}
    }

galleryitem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView 
    android:id="@+id/thumbImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />

</RelativeLayout>

和wallpaper_view_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
    android:background="@drawable/seachpage" >

<GridView
    android:id="@+id/PhoneImageGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:src="@drawable/bottomcurve" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

你应该像下面那样在充气之后手动设置适配器的项目大小,因为你的galleryitem都有大小fill_parent,然后就会出现问题。

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(
                R.layout.galleryitem, null);

        int yourExpectedHeight = 100;
        convertView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT,
            yourExpectedHeight));

        holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }


    Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
    holder.imageview.setImageBitmap(myBitmap);
    return convertView;
}