Making an android colour picker app – Part 2

Making an android colour picker app – Part 2

Picking from where we left in part-1, lets complete the colour picker application by implementing the pending bits.

ImageColorFragment

Below is the implementation of the ImageColorFragment.

public class ImageColorFragment extends Fragment {

    public static final int SOURCE_GALLERY = 0;
    public static final int SOURCE_CAMERA = 1;

    @BindView(R.id.preview)
    ImageView preview;
    @BindView(R.id.list)
    RecyclerView list;
    private ArrayList<Image> images = new ArrayList<>();


    public ImageColorFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ImageViewerFragmentArgs args = ImageViewerFragmentArgs.fromBundle(getArguments());

        if (args.getSource() == SOURCE_CAMERA) {

            ImagePicker.with(this)
                    .setFolderMode(false)
                    .setCameraOnly(true)
                    .setFolderTitle("Album")
                    .setMultipleMode(false)
                    .setSelectedImages(images)
                    .setMaxSize(10)
                    .setBackgroundColor("#212121")
                    .setAlwaysShowDoneButton(true)
                    .setRequestCode(100)
                    .setSavePath("ColorMe")
                    .setKeepScreenOn(true)
                    .start();
        } else {
            ImagePicker.with(this)
                    .setFolderMode(true)
                    .setCameraOnly(false)
                    .setFolderTitle("Album")
                    .setMultipleMode(false)
                    .setSelectedImages(images)
                    .setMaxSize(10)
                    .setBackgroundColor("#212121")
                    .setAlwaysShowDoneButton(true)
                    .setRequestCode(100)
                    .setSavePath("ColorMe")
                    .setKeepScreenOn(true)
                    .start();
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_image_color, container, false);
        ButterKnife.bind(this, view);

        list.setLayoutManager(new LinearLayoutManager(getContext()));
        return view;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == Config.RC_PICK_IMAGES && resultCode == RESULT_OK && data != null) {
            images = data.getParcelableArrayListExtra(Config.EXTRA_IMAGES);

            Glide.with(this)
                    .asBitmap()
                    .load(images.get(0).getPath())
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .placeholder(R.mipmap.ic_launcher)
                    .listener(new RequestListener<Bitmap>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            if (resource != null) {
                                Palette p = Palette.from(resource).generate();
                                extractColors(p.getSwatches());
                            }
                            return false;
                        }
                    })
                    .into(preview);
        }
    }


    private void extractColors(List<Palette.Swatch> swatches) {

        List<Integer> colors = new ArrayList<>();

        for (Palette.Swatch swatch : swatches) {
            colors.add(swatch.getRgb());
        }

        list.setAdapter(new ImageColorAdapter(getContext(), colors));
    }
}
public class ImageColorAdapter extends RecyclerView.Adapter<ImageColorAdapter.ImageViewHolder> {

    private Context context;
    private List<Integer> images;
    private LayoutInflater inflater;

    private final ColorUtils colorUtils;

    public ImageColorAdapter(Context context, List<Integer> colors) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        images = colors;
        colorUtils = new ColorUtils();

    }

    @Override
    public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ImageViewHolder(inflater.inflate(R.layout.list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ImageViewHolder holder, int position) {
        final int color = images.get(position);
        holder.slot.setBackgroundColor(color);
        holder.hexValue.setText(ColorUtils.toHex(color));
        holder.hexValue.setTextColor(ColorUtils.getTextColor(color));
        String colorName = ColorUtils.getColorNameFromRgb(ColorUtils.toHex(color));
        if (TextUtils.isEmpty(colorName))
            holder.colorName.setVisibility(View.GONE);
        else {
            holder.colorName.setText(colorName);
            holder.colorName.setTextColor(ColorUtils.getTextColor(color));
        }
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {

        LinearLayout slot;
        TextView hexValue;
        TextView colorName;

        public ImageViewHolder(View itemView) {
            super(itemView);
            slot = itemView.findViewById(R.id.color_slot);
            hexValue = itemView.findViewById(R.id.color_hex);
            colorName = itemView.findViewById(R.id.color_name);
        }
    }
}

There is a utility class where I tried implementing logic to determine a color name. It does not work perfectly and I hope to improve it if I get time. The application source code is available in Github.

Leave a Reply