Я создал приложение, которое отображает изображения из рисунков в сетку с помощью настраиваемого адаптера сетки с методом изменения размера
это пользовательский адаптер gridview
public class PTSCustomGridView extends BaseAdapter{
private Context mContext;
private final String[] gridViewString;
private final int[] gridViewImageId;
public PTSCustomGridView(Context context, String[] gridViewString, int[] gridViewImageId) {
mContext = context;
this.gridViewImageId = gridViewImageId;
this.gridViewString = gridViewString;
}
@Override
public int getCount() {
return gridViewString.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View convertView, ViewGroup parent) {
View gridViewAndroid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
gridViewAndroid = new View(mContext);
gridViewAndroid = inflater.inflate(R.layout.custom_gridview, null);
TextView textViewAndroid = (TextView) gridViewAndroid.findViewById(R.id.android_gridview_text);
ImageView imageViewAndroid = (ImageView) gridViewAndroid.findViewById(R.id.android_gridview_image);
textViewAndroid.setText(gridViewString[i]);
//imageViewAndroid.setImageResource(gridViewImageId[i]);
imageViewAndroid.setImageBitmap(decodeSampledBitmapFromResource(mContext.getResources(), gridViewImageId[i], 50, 50));
} else {
gridViewAndroid = (View) convertView;
}
return gridViewAndroid;
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
это код для генерации изображений в gridview
final String[] gridViewString = {
"Excuse Me", "I'm Sorry", "Nice to Meet You", "What's Your Phone Number", "Can You Repeat that, Please?", "Thank You",
};
final int[] gridViewImageId = {
R.drawable.excuseme, R.drawable.imsorry, R.drawable.nicemeet, R.drawable.phonenumber, R.drawable.repeatplease, R.drawable.thankyou,
};
PTSCustomGridView adapterViewAndroid = new PTSCustomGridView(PictureToSpeechActivity.this, gridViewString, gridViewImageId);
это код для получения изображения из базы данных
public void getImage(){
int count = 0;
Cursor c = db.rawQuery("SELECT ptsname, ptschoice FROM ptschoices", null);
while(c.moveToNext()){
/*byte[]image = c.getBlob(1);
Bitmap bitmap = BitmapFactory.decodeByteArray(image,0, image.length);*/
count++;
}
Toast.makeText(PictureToSpeechActivity.this, "Have Result "+count, Toast.LENGTH_LONG).show();
}
Проблема заключается в том, как я могу изменить свой настраиваемый адаптер для получения растрового изображения вместо идентификатора ресурса и как я могу отобразить его в gridview, учитывая, что у меня также есть метод изменения размера на настраиваемом адаптере