Android:调整图库项目的大小

时间:2012-08-31 06:49:19

标签: android android-imageview android-gallery

我在我的项目中使用Gallery。我想要的功能如下:

  1. 屏幕上一次应该可以看到5张图像。
  2. 图像数量可以动态增加或减少。
  3. 中心图像或我们可以说所选图像应该比四幅图像的其余部分大。(0,1,[2],3,4)。
  4. 中心1的相邻图像应小于所选图像,但比角落处的图像滞后(0,[1],2,[3],4)。
  5. 屏幕四角的图像应小于其余图像的图像。
  6. 我不知道如何实现这一点。我已经调整了中心ImageView的大小,但我不知道如何设置四个图像的其余部分的大小。请提供此问题的解决方案。

    这是我改变ImageView大小的代码

    import android.content.Context;
    import android.graphics.drawable.BitmapDrawable;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    import android.widget.ImageView.ScaleType;
    
    public class InfiniteGalleryResourceAdapter extends BaseAdapter {
    /** The width of each child image */
    private static final int G_ITEM_WIDTH = 60;
    /** The height of each child image */
    private static final int G_ITEM_HEIGHT = 250;
    
    
    /** The context your gallery is running in (usually the activity) */
    private Context mContext;
    private int imageWidth;
    private int imageHeight;
    /** The array of resource ids to draw */
    private final int[] imageIds;
    
    public InfiniteGalleryResourceAdapter(Context c, int[] imageIds ) {
    
        this.mContext = c;
        this.imageIds = imageIds;
    }
    
    /**
     * The count of how many items are in this Adapter
     * This will return the max number as we want it to scroll as much as possible
     */
    @Override
    public int getCount() {
        return imageIds.length;
    }
    
    @Override
    public Object getItem(int position) {
        return position;
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // convertView is always null in android.widget.Gallery
    
    
        ImageView i = getImageView();
        int itemPos = (position % imageIds.length);
    
        try {
            // first we calculate the item position in your list, because we have said the adapters size is Integer.MAX_VALUE
            // the position getView gives us is not use-able in its current form, we have to use the modulus operator
            // to work out what number in our 'array of paths' this actually equals
            Log.d("item position", String.valueOf(itemPos));
    
            i.setImageResource(imageIds[itemPos]);
            ((BitmapDrawable) i.getDrawable()).setAntiAlias(true); // Make sure we set anti-aliasing otherwise we get jaggies (non-smooth lines)
    
        } catch (OutOfMemoryError e) {
            // a 'just in case' scenario
            Log.e("InfiniteGalleryResourceAdapter", "Out of memory creating imageview. Using empty view.", e);
        }
    
        return i;
    }
    
    /**
     * Retrieve an ImageView to be used with the Gallery
     * @return an ImageView with width and height set to DIP values
     */
    private ImageView getImageView() {
        setImageDimensions();
    
        ImageView i = new ImageView(mContext);
        i.setLayoutParams(new Gallery.LayoutParams(imageWidth, imageHeight));
        i.setScaleType(ScaleType.CENTER_INSIDE);
        return i;
    }
    
    /**
     * Sets the dimensions for each View that is used in the gallery
     * lazily initialized so that we don't have to keep converting over and over
     */
    private void setImageDimensions() {
        if (imageWidth == 0 || imageHeight == 0) {
            imageWidth = AndroidUtils.convertToPix(mContext, G_ITEM_WIDTH);
            imageHeight = AndroidUtils.convertToPix(mContext, G_ITEM_HEIGHT);
        }
    }
    }
    

0 个答案:

没有答案