Search code examples
androidandroid-actionbarandroid-actionbar-compatandroid-glide

How to load a circular appcompat actionbar logo using glide


Till now I have done the following, if I omit the circular image creation part it works fine, but I must show a circular image in actionbar

here is what I have tried so far, any help will be highly appreciated

Glide.with(mContext)
                    .load(doctorDetailsList.get(0).getDoc_imgurl().replace("200x200", Measuredwidth + "x" + Measuredwidth))
                    .placeholder(R.drawable.no_image)
                    .override(Measuredwidth, Measuredwidth)
                    .into(new Target<GlideDrawable>()
                    {
                        @Override
                        public void onLoadStarted(Drawable placeholder)
                        {

                        }

                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable)
                        {

                        }

                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation)
                        {
                            //  GlideDrawable dr = resource;
                            Bitmap bitmap = ((com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable) resource).getBitmap();
                            String filename = doctorDetailsList.get(0).getDoc_imgurl().trim().substring(doctorDetailsList.get(0).getDoc_imgurl().trim().lastIndexOf("/") + 1);
                            filename = filename.replaceAll(".jpg", "");
                            int resID = getResources().getIdentifier(filename, "data", getPackageName());
                            Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), resID);

                            //Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 200, 200, true));

                            RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), icon);
                            circularBitmapDrawable.setCircular(true);
                            // GlideDrawable gd = new GlideDrawable(resource,)

                            getSupportActionBar().setLogo(circularBitmapDrawable);
                        }

                        @Override
                        public void onLoadCleared(Drawable placeholder)
                        {

                        }

                        @Override
                        public void getSize(SizeReadyCallback cb)
                        {

                        }

                        @Override
                        public void setRequest(com.bumptech.glide.request.Request request)
                        {

                        }

                        @Override
                        public com.bumptech.glide.request.Request getRequest()
                        {
                            return null;
                        }

                        @Override
                        public void onStart()
                        {

                        }

                        @Override
                        public void onStop()
                        {

                        }

                        @Override
                        public void onDestroy()
                        {

                        }
                    });

Solution

  • could not find a way, switched to Picasso and changed the code as following

      Picasso.with(mContext)
                        .load(doctorDetailsList.get(0).getDoc_imgurl())
                        .resize(120, 120)
                        .centerCrop()
                        .placeholder(R.drawable.no_image)
                        .into(new com.squareup.picasso.Target()
                        {
                            @Override
                            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
                            {
                                RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap);
                                circularBitmapDrawable.setCircular(true);                             
                                getSupportActionBar().setLogo(circularBitmapDrawable);
                            }
    
                            @Override
                            public void onBitmapFailed(Drawable errorDrawable)
                            {
    
                            }
    
                            @Override
                            public void onPrepareLoad(Drawable placeHolderDrawable)
                            {
    
                            }
                        });