私はGallery
自分のプロジェクトで使用しています。欲しい機能は以下の通りです。
- 画面には一度に 5 つの画像が表示されます。
- 画像の数は動的に増減できます。
- 中央の画像または選択した画像は、残りの 4 つの画像よりも大きくする必要があります (0,1,[2],3,4)。
- 中央の 1 つの隣接する画像は、選択した画像よりも小さくする必要がありますが、コーナーの画像よりも大きくする必要があります (0、[1]、2、[3]、4)。
- 画面の隅にある画像は、残りの画像よりも小さくする必要があります。
これを実装する方法がわかりません。中央のサイズを変更しましたImageView
が、残りの 4 つの画像のサイズを設定する方法がわかりません。この問題の解決策を提供してください。
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);
}
}
}