我Gallery
在我的项目中使用。我想要的功能如下:
- 屏幕上一次应该有 5 个可见的图像。
- 图像数量可以动态增加或减少。
- 中心图像或者我们可以说选择的图像应该大于四个图像的其余部分。(0,1,[2],3,4)。
- 中心 1 的相邻图像应小于所选图像,但大于角落处的图像。(0,[1],2,[3],4)。
- 屏幕角落的图像应小于其余图像。
我不知道如何实现这一点。我已经改变了中心的大小,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);
}
}
}