如何将图像宽度设置为listview的宽度

时间:2014-07-25 16:03:13

标签: android image listview imageview adapter

我有一个应用程序,我可以从SD卡中选择图像。选择图像后,例如从照片文件夹中,您可以在ListView中查看所选图像。

使用适配器将选定的图像传递到列表视图,该适配器获取每个图像的路径数组。

一切正常,但如何使图像适合列表视图行的宽度?

提前感谢。

public class QueuedImagesActivity extends Activity {

    private static final String TAG = QueuedImagesActivity.class.getSimpleName();
    private ImageAdapter adapter;
    private ListView imageList;
    private ApplicationObj appObj;
    private Intent[] uniquePhotoChunks;
    private String path;
    private ArrayList<String> imagePaths = new ArrayList<String>(); 

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_listview);

        appObj = (ApplicationObj) getApplication();

        // Get the queued chunks
        try {
            boolean includeChunksCurrentlyBeingProcessed = true;
            boolean returnUniqueUris = true;
            uniquePhotoChunks = appObj.getQueuedChunks(includeChunksCurrentlyBeingProcessed, returnUniqueUris);
            Log.d(TAG, "There are " + uniquePhotoChunks.length + " photo paths sent back from getQueuedChunks");

            //get the URI out from the Intent with getDataString() and store in Array that the adapter will use
            for(int i = 0; i < uniquePhotoChunks.length; i++){

                path = uniquePhotoChunks[i].getDataString();
                imagePaths.add(path);

                Log.d(TAG, "path in QueuedImagesActivity = " + path);

            }

            //pass the array to the adapter
            imageList = (ListView) findViewById(R.id.listView1);
            adapter = new ImageAdapter(getBaseContext(), imagePaths);
            imageList.setAdapter(adapter);   

        }
        catch (Exception e) {
            Log.e(TAG, "There was a problem showing the queued images", e);
            Toast.makeText(this, "There was a problem showing the queued images", Toast.LENGTH_LONG).show();

            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }


    }//end of onCreate
}

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="List of Images"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true" >
</ListView>

</RelativeLayout>

public class ImageAdapter extends BaseAdapter {

    private static final String TAG = ImageAdapter.class.getSimpleName();

    static class RowItemHolder{
        ImageView imageView;
    }
    private Context context;
    private ArrayList<String> imagePaths= new ArrayList<String>();

    public ImageAdapter(Context baseContext, ArrayList<String> imagePaths) {

        this.context= baseContext;
        this.imagePaths= imagePaths;
    }

    @Override
    public int getCount() {

        return imagePaths.size();
    }

    @Override
    public Object getItem(int position) {

            return null;
    }

    @Override
    public long getItemId(int position) {

            return 0;
    }

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

    View view;
    view= convertView;
    RowItemHolder holder = null;
    if(view== null){
            LayoutInflater in =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = in.inflate(R.layout.image_view, parent, false);
            holder= new RowItemHolder();
            holder.imageView=(ImageView) view.findViewById(R.id.imageView1);
        view.setTag(holder);
    } else{
            holder = (RowItemHolder) view.getTag();
    }


    Log.e(TAG, "imagePaths.get(position) = " + imagePaths.get(position));



    holder.imageView.setImageURI(Uri.parse(imagePaths.get(position)));

    return view;
}
}

[EDIT1]

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

尝试将属性android:scaleType="fitXY"放在ImageView中:

<ImageView
  android:id="@+id/imageView1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:src="@drawable/ic_launcher"
  android:scaleType="fitXY" />

希望这有帮助。

答案 1 :(得分:0)

在R.Layout中设置图像视图的layout_width。 image_view to match_parent。

答案 2 :(得分:0)

当ImageView设置为背景时,ImageView将仅拉伸图像以填充视图边界。设置为src的图像将保持其原始大小或缩小以适合视图。

所以你需要先得到drawable然后再调用holder.imageView.setBackground(yourImageDrawable)

您可以通过以下方式从URI检索drawable: Retrieve drawable resource from Uri

因此它最终应该看起来像下面的代码

Uri uri = Uri.parse(imagePaths.get(position));
Drawable drawable;
try {
    InputStream inputStream = context.getContentResolver().openInputStream(uri);
    drawable = Drawable.createFromStream(inputStream, uri.toString() );
} catch (FileNotFoundException e) {
    drawable = context.getResources().getDrawable(R.drawable.default_image);
}

holder.imageView.setBackgroundDrawable(drawable);