Я использую класс SqaureImageView как:
public class SqaureImageView extends AppCompatImageView {
int widthMeasureSpec;
int heightMeasureSpec;
public SqaureImageView(Context context) {
super(context);
}
public SqaureImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SqaureImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
this.widthMeasureSpec=widthMeasureSpec;
this.heightMeasureSpec=heightMeasureSpec;
}
public int getWidthMeasureSpec(){
return widthMeasureSpec;
}
public int getHeightMeasureSpec(){
return heightMeasureSpec;
}
}
Я использую этот SquareImageView в массиве Adapter следующим образом:
public class GridPostAdapter extends ArrayAdapter<String> {
private Context mContext;
private LayoutInflater mInflater;
private int layoutResource;
private String mAppend;
private ArrayList<String> imgColor;
private ArrayList<String> imgCaption;
public GridPostAdapter(Context context, int layoutResource, String append, ArrayList<String> imgColor, ArrayList<String> imgCaption) {
super(context, layoutResource, imgColor);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContext = context;
this.layoutResource = layoutResource;
mAppend = append;
this.imgColor = imgColor;
this.imgCaption = imgCaption;
}
private static class ViewHolder {
SqaureImageView image;
ProgressBar mProgressBar;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
/*
Viewholder build pattern (Similar to recyclerview)
*/
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(layoutResource, parent, false);
holder = new ViewHolder();
holder.mProgressBar = (ProgressBar) convertView.findViewById(R.id.gridImageProgressbar);
holder.image = (SqaureImageView) convertView.findViewById(R.id.gridImageView);
holder.image.setBackgroundColor(Integer.parseInt(imgColor.get(position)));
Bitmap bm = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.ic_blank);
Bitmap newImage = bm.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(newImage);
c.drawBitmap(newImage, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Integer.parseInt(String.valueOf(R.color.black)));
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(20);
c.drawText(imgCaption.get(position), 0, 5, paint);
holder.image.setImageBitmap(newImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ImageLoader imageLoader = ImageLoader.getInstance();
//https://firebasestorage.googleapis.com/v0/b/projectx-971e6.appspot.com/o/photos%2Fusers%2F8QemZCZPRabTR88Vkpv1yDXHLEn1%2Fphoto1?alt=media&token=124af00f-c02b-4785-a6d5-ecb042dd18d2
imageLoader.displayImage("", holder.image, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
if(holder.mProgressBar != null){
holder.mProgressBar.setVisibility(View.VISIBLE);
}
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
if(holder.mProgressBar != null){
holder.mProgressBar.setVisibility(View.GONE);
}
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if(holder.mProgressBar != null){
holder.mProgressBar.setVisibility(View.GONE);
}
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
if(holder.mProgressBar != null){
holder.mProgressBar.setVisibility(View.GONE);
}
}
});
return convertView;
}
}
У меня есть класс UniversalImageLoader для загрузки изображений:
public class UniversalImageLoader {
private static final int defaultImage = R.drawable.ic_android;
private static final int defaultImageBlank = R.drawable.ic_blank;
private Context mContext;
public UniversalImageLoader(Context context) {
mContext = context;
}
public ImageLoaderConfiguration getConfig(){
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(defaultImage)
.showImageForEmptyUri(defaultImageBlank)
.showImageOnFail(defaultImage)
.considerExifParams(true)
.cacheOnDisk(true).cacheInMemory(true)
.cacheOnDisk(true).resetViewBeforeLoading(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(mContext)
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.diskCacheSize(100 * 1024 * 1024).build();
return configuration;
}
/**
* this method can be sued to set images that are static. It can't be used if the images
* are being changed in the Fragment/Activity - OR if they are being set in a list or
* a grid
* @param imgURL
* @param image
* @param mProgressBar
* @param append
*/
public static void setImage(String imgURL, ImageView image, final ProgressBar mProgressBar, String append){
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(append + imgURL, image, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
if(mProgressBar != null){
mProgressBar.setVisibility(View.VISIBLE);
}
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
if(mProgressBar != null){
mProgressBar.setVisibility(View.GONE);
}
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if(mProgressBar != null){
mProgressBar.setVisibility(View.GONE);
}
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
if(mProgressBar != null){
mProgressBar.setVisibility(View.GONE);
}
}
});
}
Я хочу нарисовать текст на каждом изображении и показать его в виде сетки. Используя это, я могу видеть только цветные изображения и без текста на них. Я новичок в Android, и я не уверен, как использовать растровые изображения и холст для рисования текста на изображениях. Пожалуйста, посмотрите на мой код и укажите на изменения / улучшения.