package app.fedilab.android.imageeditor; import android.content.Context; import android.graphics.Color; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.RecyclerView; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; import app.fedilab.android.R; /** * Created by Ahmed Adel on 5/8/17. */ public class ColorPickerAdapter extends RecyclerView.Adapter { private Context context; private LayoutInflater inflater; private List colorPickerColors; private OnColorPickerClickListener onColorPickerClickListener; private ColorPickerAdapter(@NonNull Context context, @NonNull List colorPickerColors) { this.context = context; this.inflater = LayoutInflater.from(context); this.colorPickerColors = colorPickerColors; } ColorPickerAdapter(@NonNull Context context) { this(context, getDefaultColors(context)); this.context = context; this.inflater = LayoutInflater.from(context); } private static List getDefaultColors(Context context) { ArrayList colorPickerColors = new ArrayList<>(); colorPickerColors.add(ContextCompat.getColor(context, R.color.blue_color_picker)); colorPickerColors.add(ContextCompat.getColor(context, R.color.brown_color_picker)); colorPickerColors.add(ContextCompat.getColor(context, R.color.green_color_picker)); colorPickerColors.add(ContextCompat.getColor(context, R.color.orange_color_picker)); colorPickerColors.add(ContextCompat.getColor(context, R.color.red_color_picker)); colorPickerColors.add(ContextCompat.getColor(context, R.color.black)); colorPickerColors.add(ContextCompat.getColor(context, R.color.red_orange_color_picker)); colorPickerColors.add(ContextCompat.getColor(context, R.color.sky_blue_color_picker)); colorPickerColors.add(ContextCompat.getColor(context, R.color.violet_color_picker)); colorPickerColors.add(ContextCompat.getColor(context, R.color.white)); colorPickerColors.add(ContextCompat.getColor(context, R.color.yellow_color_picker)); colorPickerColors.add(ContextCompat.getColor(context, R.color.yellow_green_color_picker)); return colorPickerColors; } @NotNull @Override public ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.color_picker_item_list, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.colorPickerView.setBackgroundColor(colorPickerColors.get(position)); } @Override public int getItemCount() { return colorPickerColors.size(); } private void buildColorPickerView(View view, int colorCode) { view.setVisibility(View.VISIBLE); ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape()); biggerCircle.setIntrinsicHeight(20); biggerCircle.setIntrinsicWidth(20); biggerCircle.setBounds(new Rect(0, 0, 20, 20)); biggerCircle.getPaint().setColor(colorCode); ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape()); smallerCircle.setIntrinsicHeight(5); smallerCircle.setIntrinsicWidth(5); smallerCircle.setBounds(new Rect(0, 0, 5, 5)); smallerCircle.getPaint().setColor(Color.WHITE); smallerCircle.setPadding(10, 10, 10, 10); Drawable[] drawables = {smallerCircle, biggerCircle}; LayerDrawable layerDrawable = new LayerDrawable(drawables); view.setBackgroundDrawable(layerDrawable); } void setOnColorPickerClickListener(OnColorPickerClickListener onColorPickerClickListener) { this.onColorPickerClickListener = onColorPickerClickListener; } public interface OnColorPickerClickListener { void onColorPickerClickListener(int colorCode); } class ViewHolder extends RecyclerView.ViewHolder { View colorPickerView; ViewHolder(View itemView) { super(itemView); colorPickerView = itemView.findViewById(R.id.color_picker_view); itemView.setOnClickListener(v -> { if (onColorPickerClickListener != null) onColorPickerClickListener.onColorPickerClickListener(colorPickerColors.get(getAdapterPosition())); }); } } }