fedilab-Android-App/app/src/main/java/app/fedilab/android/drawers/CustomEmojiAdapter.java

94 lines
3.1 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.drawers;
2017-11-03 13:55:18 +01:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-11-03 13:55:18 +01:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-11-03 13:55:18 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-11-03 13:55:18 +01:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
2017-11-03 13:55:18 +01:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
2017-12-02 11:02:25 +01:00
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
2017-11-03 13:55:18 +01:00
import java.util.List;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Emojis;
2017-11-03 13:55:18 +01:00
/**
* Created by Thomas on 03/11/2017.
* Adapter to display custom emojis
*/
public class CustomEmojiAdapter extends ArrayAdapter {
private Context context;
private List<Emojis> emojis;
public CustomEmojiAdapter(@NonNull Context context, int resource, List<Emojis> emojis) {
super(context, resource, resource);
this.context = context;
this.emojis = emojis;
}
@Override
public int getCount() {
return emojis.size();
}
@Override
public Emojis getItem(int position) {
return emojis.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
final ImageView imageView;
Emojis emoji = emojis.get(position);
if (convertView == null) {
imageView = new ImageView(context);
float density = context.getResources().getDisplayMetrics().density;
imageView.setLayoutParams(new GridView.LayoutParams((int)(30*density), (int)(30*density)));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding((int)(2*density), (int)(5*density), (int)(2*density), (int)(5*density));
} else {
imageView = (ImageView) convertView;
}
2017-12-02 11:02:25 +01:00
Glide.with(context)
.asBitmap()
.load(emoji.getUrl())
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
BitmapDrawable icon = new BitmapDrawable(context.getResources(), resource);
imageView.setImageDrawable(icon);
}
});
2017-11-03 13:55:18 +01:00
return imageView;
}
}