我在我的项目中使用Gallery
。我想要的功能如下:
我不知道如何实现这一点。我已经调整了中心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);
}
}
}