ग्लाइड लाइब्रेरी के साथ छवि को गोल कैसे करें?


197

तो, किसी को पता है कि ग्लाइड के साथ गोल कोनों के साथ एक छवि कैसे प्रदर्शित की जाए? मैं ग्लाइड के साथ एक छवि लोड कर रहा हूं, लेकिन मुझे नहीं पता कि इस लाइब्रेरी में गोल पार्म्स कैसे पास किया जाए।

मुझे निम्न उदाहरण की तरह प्रदर्शन छवि चाहिए:

यहां छवि विवरण दर्ज करें


2
मैं ronded छवि देखने के लिए github.com/hdodenhof/CircleImageView का उपयोग करता हूं
मिलापांक

1
मुझे पता है कि CircleImageView के साथ Glide lib का उपयोग कैसे किया जाता है, लेकिन मैं इसे केवल Glide lib के साथ संभव तरीके से खोजता हूं। ऐसा करने के लिए Glide lib में कोई विधि है या यह समर्थित नहीं है?
mr.boyfox

जवाबों:


487

ग्लाइड V4:

    Glide.with(context)
        .load(url)
        .circleCrop()
        .into(imageView);

सरकना V3:

आप RoundedBitmapDrawableग्लाइड के साथ परिपत्र छवियों के लिए उपयोग कर सकते हैं । कोई कस्टम ImageView आवश्यक नहीं है।

 Glide.with(context).load(url).asBitmap().centerCrop().into(new BitmapImageViewTarget(imageView) {
        @Override
        protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                    RoundedBitmapDrawableFactory.create(context.getResources(), resource);
            circularBitmapDrawable.setCircular(true);
            imageView.setImageDrawable(circularBitmapDrawable);
        }
    });

6
हालांकि यह एक सीमा नहीं बनाता है।
ज़ैकटाकार्डि

1
@ jimmy0251 नहीं, आप नहीं कर सकते। ग्लाइड ड्रॉबल्स बिटमैपड्रैबल्स नहीं हैं। वे ट्रांज़ैक्शन विथेबल्स और ग्लेरड्रैबल्स हैं जो प्लेसहोल्डर से वास्तविक छवि तक पार करते हैं। RoundedBitmapDrawable उसे संभाल नहीं सकता है।
ग्रेग एनिस

7
ड्राइंग आकार अंडाकार के साथ पृष्ठभूमि सेट करें। और सीमा बनाने के लिए इमेजव्यू को पैडिंग दें।
ललित जादव

2
यदि आप कुछ समस्या दूर करते हैं तो कृपया रोमन समोसेन्को के अगले समाधान को चुनें।
पाबेल

1
.centerCrop()आप के बजाय शायद मतलब है.circleCrop()
थानासिस कपेलोनिस

66

इस पोस्ट की जाँच करें , ग्लाइड बनाम पिकासो ...
संपादित करें : लिंक की गई पोस्ट लाइब्रेरी में एक महत्वपूर्ण अंतर को नहीं बुलाती है। ग्लाइड अपने आप रिसाइकिलिंग करता है। अधिक के लिए TWiStErRob की टिप्पणी देखें ।

Glide.with(this).load(URL).transform(new CircleTransform(context)).into(imageView);

public static class CircleTransform extends BitmapTransformation {
    public CircleTransform(Context context) {
        super(context);
    }

    @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }

    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        // TODO this could be acquired from the pool too
        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
        return result;
    }

    @Override public String getId() {
        return getClass().getName();
    }
} 

दिलचस्प लगता है। क्या यह एपीआई 21 की रूपरेखा के साथ अच्छी तरह से काम करता है?
तेवोल्ड

हम्म, मैं तो इस का उपयोग कर सकते हैं। मैं बस एक कस्टम दृश्य का उपयोग करने की कोशिश करूंगा, हालांकि, मैं दूसरा बिटमैप बनाना नहीं चाहूंगा :)। इसके अलावा, आपको हमेशा बिटमैप पूल का उपयोग करना चाहिए ताकि वहां से बिटमैप प्राप्त करने की कोशिश की जा सके :)
टेओवाल्ड

public String getId()कोड में दिखाए गए तरीके का उपयोग करने से सावधान रहें क्योंकि यह सभी छवियों के लिए एक ही आईडी देता है और इस प्रकार यह हो सकता है कि ग्लाइड पुरानी गोल छवियों को सेट करेगा जबकि परिवर्तनों के बिना यह सही छवि सेट करने वाला है! मुझे नहीं पता कि ग्लाइड कैसे काम करता है, लेकिन ऐसा लगता है कि यह इमेज ट्रांसफॉर्मेशन को कैश करता है (मुझे लगता है कि हार्ड कैल्स से बचने के लिए)। और आईडी का उपयोग रूपांतरित छवि के आईडी के रूप में किया जाता है। मैंने कंस्ट्रक्टर में छवि का एक यूआरएल जोड़ा और उल्लेखित विधि लौटा दी जिसके परिणामस्वरूप आईडी आई जैसे:this.id = String.format("%s:%s",this.getClass().getSimpleName(),id);
स्टेन

2
केवल ट्रांसफॉर्मेशन आईडी के लिए स्टेन की आवश्यकता है कि वे सभी ट्रांसफॉर्मेशन के बीच अद्वितीय हैं, इसलिए यहां उपयोग सही है। कैश कीज़ में सोर्स आईडी और ट्रांसफ़ॉर्मेशन आईडी दोनों शामिल होंगे, इसलिए ट्रांसफ़ॉर्मेशन आईडी एक प्रतिस्थापन के बजाय मिक्सिन है। देखें github.com/bumptech/glide/wiki/…
सैम जूड

4
क्या प्लेसहोल्डर में परिवर्तन लागू करने का कोई तरीका है?
सिनीगामी

45

सबसे आसान तरीका (ग्लाइड 4.xx की आवश्यकता है)

Glide.with(context).load(uri).apply(RequestOptions().circleCrop()).into(imageView)

यह भी संकलन नहीं ... RequestOptions ()?
राफेल लीमा

3
@ राफेललिमा यह कोटलिन में लिखा गया है।
रोमन समोलेन्को

1
नोटिस, कि .apply()बाद में चला जाता है .load()
जॉनी फाइव

RequestOptions.circleCropTransform () है, RequestOptions () नहीं है।
मुस्कान

आपको Glide.with (संदर्भ) .load (uri) .apply (CircleCrop ()) लिखने की आवश्यकता है। (imageView) लागू फ़ंक्शन को देखें।
जसपाल

25

इस तरह आजमाएं

कोड

Glide.with(this)
    .load(R.drawable.thumbnail)
    .bitmapTransform(new CropCircleTransformation(this))
    .into(mProfile);

एक्सएमएल

<ImageView
  android:id="@+id/img_profile"
  android:layout_width="76dp"
  android:layout_height="76dp"
  android:background="@drawable/all_circle_white_bg"
  android:padding="1dp"/>

all_circle_white_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="oval">
      <solid android:color="@android:color/white"/>
  </shape>
  </item>
</selector>
  • मैं इस परिवर्तन पुस्तकालय का उपयोग करता हूं। -> https://github.com/wasabeef/glide-transformations
  • सर्कल स्ट्रोक की चौड़ाई ImageView की पैडिंग है

9

इसके बहुत ही सरल मैंने ग्लाइड लाइब्रेरी को बहुत अच्छी लाइब्रेरी और वॉली गूगल की लाइब्रेरी पर निबंध आधार देखा है

गोल छवि दृश्य के लिए usethis पुस्तकालय

https://github.com/hdodenhof/CircleImageView

अभी

// एक साधारण दृश्य के लिए:

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

  CircleImageView civProfilePic = (CircleImageView)findViewById(R.id.ivProfile);
  Glide.load("http://goo.gl/h8qOq7").into(civProfilePic);
}

// एक सूची के लिए:

@Override
public View getView(int position, View recycled, ViewGroup container) {
final ImageView myImageView;
 if (recycled == null) {
    myImageView = (CircleImageView) inflater.inflate(R.layout.my_image_view,
            container, false);
} else {
    myImageView = (CircleImageView) recycled;
}

String url = myUrls.get(position);

Glide.load(url)
    .centerCrop()
    .placeholder(R.drawable.loading_spinner)
    .animate(R.anim.fade_in)
    .into(myImageView);

  return myImageView;
}

और एक्सएमएल में

<de.hdodenhof.circleimageview.CircleImageView
   android:id="@+id/ivProfile
   android:layout_width="160dp"
   android:layout_height="160dp"
   android:layout_centerInParent="true"
   android:src="@drawable/hugh"
   app:border_width="2dp"
   app:border_color="@color/dark" />

2
पिछले उत्तर की टिप्पणी की तरह इस तरह से भी अच्छी तरह से काम नहीं करता है। कम से कम 'de.hdodenhof: Circleimageview: 1.2.2' + 'com.github.bumptech.glide: glide: 3.5.2' के लिए। जाँच की और डबल चेक किया। ग्लाइड 3.4। + और सर्कलिमेजव्यू 1.2.1
स्टेन

.CenterCrop () के लिए +1। DiamondImageView plus .asBitmap () के साथ मेरे लिए काम किया ।
felippe

1
ग्लाइड पर .dontAnimate () कॉलिंग की आवश्यकता होती है जो स्वीकार्य नहीं है
ग्रेग एनिस

+1 लेकिन यह भी ध्यान दें कि ग्लाइड के साथ इंटरनेट इमेज लोड करना
hedgehog

9

दूसरे उपाय मेरे काम नहीं आए। मैंने पाया कि वे सभी महत्वपूर्ण कमियां हैं:

  • ग्लाइड परिवर्तनों का उपयोग करने वाले समाधान प्लेसहोल्डर्स के साथ काम नहीं करते हैं
  • गोल छवि के दृश्यों का उपयोग करने वाले समाधान एनिमेशन के साथ काम नहीं करते (यानी क्रॉसफ़ेड)
  • माता-पिता की एक सामान्य विधि का उपयोग करने वाले समाधान जो अपने बच्चों को क्लिप करते हैं (यानी यहां स्वीकृत उत्तर ) ग्लाइड के साथ अच्छी तरह से काम नहीं करते हैं

यह वास्तव में दिलचस्प है कि इसके चारों ओर धूमने के बाद मुझे फ्रेस्को पुस्तकालय पृष्ठ गोल कोनों और हलकों के बारे में मिला जिसमें वे मूल रूप से समान सीमाएं सूचीबद्ध करते हैं और बयान के साथ समाप्त होते हैं:

एंड्रॉइड पर कोनों को गोल करने के लिए वास्तव में कोई अच्छा समाधान नहीं है और एक को उपरोक्त व्यापार-नापसंद के बीच चुनना होगा

अविश्वसनीय है कि इस समय हम अभी भी एक असली समाधान नहीं है। मेरे पास ऊपर दिए गए लिंक के आधार पर एक वैकल्पिक समाधान है। इस दृष्टिकोण के साथ दोष यह है कि यह मानता है कि आपकी पृष्ठभूमि एक ठोस रंग है (कोने वास्तव में पारदर्शी नहीं हैं)। आप इसे इस तरह उपयोग करेंगे:

<RoundedCornerLayout ...>
    <ImageView ...>
</RoundedCornerLayout>

जीआईएस यहां है और पूर्ण कोड यहां है:

public class RoundedCornerLayout extends RelativeLayout {
    private Bitmap maskBitmap;
    private Paint paint;
    private float cornerRadius;

    public RoundedCornerLayout(Context context) {
        super(context);
        init(context, null, 0);
    }

    public RoundedCornerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        setWillNotDraw(false);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        if (maskBitmap == null) {
            // This corner radius assumes the image width == height and you want it to be circular
            // Otherwise, customize the radius as needed
            cornerRadius = canvas.getWidth() / 2;
            maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
        }

        canvas.drawBitmap(maskBitmap, 0f, 0f, paint);
    }

    private Bitmap createMask(int width, int height) {
        Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mask);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE); // TODO set your background color as needed

        canvas.drawRect(0, 0, width, height, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);

        return mask;
    }
}

6

अब Glide V4 में आप सीधे CircleCrop () का उपयोग कर सकते हैं

Glide.with(fragment)
  .load(url)
  .circleCrop()
  .into(imageView);

प्रकारों में निर्मित

  • CenterCrop
  • FitCenter
  • CircleCrop

5

इस परिवर्तन का उपयोग करें, यह ठीक काम करेगा।

public class CircleTransform extends BitmapTransformation {
public CircleTransform(Context context) {
    super(context);
}

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    return circleCrop(pool, toTransform);
}

private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
    if (source == null) return null;

    int borderColor = ColorUtils.setAlphaComponent(Color.WHITE, 0xFF);
    int borderRadius = 3;

    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    // TODO this could be acquired from the pool too
    Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
    if (squared != source) {
        source.recycle();
    }

    Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
    if (result == null) {
        result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);

    // Prepare the background
    Paint paintBg = new Paint();
    paintBg.setColor(borderColor);
    paintBg.setAntiAlias(true);

    // Draw the background circle
    canvas.drawCircle(r, r, r, paintBg);

    // Draw the image smaller than the background so a little border will be seen
    canvas.drawCircle(r, r, r - borderRadius, paint);

    squared.recycle();

    return result;
}

@Override
public String getId() {
    return getClass().getName();
}} 

5

के लिए ग्लाइड 4.xx

उपयोग

Glide
  .with(context)
  .load(uri)
  .apply(
      RequestOptions()
        .circleCrop())
  .into(imageView)

से डॉक यह है कि कहा गया है

गोल चित्र: CircleImageView / CircularImageView / RoundedImageView को TransitionDrawable ((.crossFade) (.thumbnail () या .placeholder ()) और एनिमेटेड GIFs के साथ समस्याएँ ज्ञात हैं , एक BitmapTransformation (.circleCrop) का उपयोग v4 या v4 में उपलब्ध होगा। .dontAnimate () समस्या को ठीक करने के लिए


4

इस उत्तर के अनुसार , दोनों भाषाओं में सबसे आसान तरीका है:

Kotlin:

Glide.with(context).load(uri).apply(RequestOptions().circleCrop()).into(imageView)

जावा:

Glide.with(context).load(uri).apply(new RequestOptions().circleCrop()).into(imageView)

यह ग्लाइड 4.XX पर काम करता है


4

रोमन समोलेन्को का जवाब सही था, सिवाय इसके कि फ़ंक्शन बदल गया है। सही जवाब है

Glide.with(context)
                .load(yourImage)
                .apply(RequestOptions.circleCropTransform())
                .into(imageView);

3

मुझे छवि पर सीमा जोड़ने के लिए एक आसान और सरल समाधान मिला जिसमें रंग छवि पर ढाल या सेट करना चाहते हैं।

कदम:

  1. एक फ्रेम लेआउट लें और दो चित्र जोड़ें। आप अपनी आवश्यकता के अनुसार आकार सेट कर सकते हैं। इसके लिए imgPlaceHolder, आपको एक सफेद छवि या रंग की आवश्यकता है जिसे आप सेट करना चाहते हैं।

        <ImageView
            android:id="@+id/imgPlaceHolder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:src="@drawable/white_bg"/>

        <ImageView
            android:id="@+id/imgPic"
            android:layout_width="190dp"
            android:layout_height="190dp"
            android:layout_gravity="center"
            android:src="@drawable/image01"/>
    </FrameLayout>
  1. इस कोड को xml फाइल पर रखने के बाद, java फाइल में नीचे की लाइन डालें।

    Glide.with(this).load(R.drawable.image01).asBitmap().centerCrop().into(new BitmapImageViewTarget(imgPic) {
        @Override
        protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                    RoundedBitmapDrawableFactory.create(getResources(), resource);
            circularBitmapDrawable.setCircular(true);
            imageView.setImageDrawable(circularBitmapDrawable);
        }
    });
    
    Glide.with(this).load(R.drawable.white_bg).asBitmap().centerCrop().into(new BitmapImageViewTarget(imgPlaceHolder) {
        @Override
        protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                    RoundedBitmapDrawableFactory.create(getResources(), resource);
            circularBitmapDrawable.setCircular(true);
            imgTemp2.setImageDrawable(circularBitmapDrawable);
        }
    });

यह किसी भी अतिरिक्त पैडिंग और मार्जिन के साथ इमेजव्यू की सीमा बना देगा।

नोट : सफेद छवि सीमा के लिए अनिवार्य है अन्यथा यह काम नहीं करेगा।

हैप्पी कोडिंग :)


3

ग्लाइड लाइब्रेरी के साथ आप इस कोड का उपयोग कर सकते हैं:

Glide.with(context)
    .load(imageUrl)
    .asBitmap()
    .placeholder(R.drawable.user_pic)
    .centerCrop()
    .into(new BitmapImageViewTarget(img_profPic) {
        @Override
        protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);

            circularBitmapDrawable.setCircular(true);
            img_profPic.setImageDrawable(circularBitmapDrawable);
        }
    });

आप ग्लिड लाइब्रेरी के माध्यम से गोल छवि भी बना सकते हैं।
प्रीति २४'१ '

2

मैं पहले इसकी तलाश कर रहा था और मैंने इसे बहुत आसान तरीके से बनाया, मुझे उम्मीद है कि आप इसे पसंद करेंगे।

 //crete this method into your Utils class and call this method wherever you want to use.
    //you can set these placeHolder() and error() image static as well. I made it as comment inside this method, then no need to use [placeHolderUrl and errorImageUrl] parameters. remove it from this method.
    public static void loadImage(final Activity context, ImageView imageView, String url, int placeHolderUrl, int errorImageUrl) {
        if (context == null || context.isDestroyed()) return;

        //placeHolderUrl=R.drawable.ic_user;
        //errorImageUrl=R.drawable.ic_error;
            Glide.with(context) //passing context
                    .load(getFullUrl(url)) //passing your url to load image.
                    .placeholder(placeHolderUrl) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
                    .error(errorImageUrl)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
                    .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
                    .transform(new CircleTransform(context))//this CircleTransform class help to crop an image as circle.
                    .animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
                    .fitCenter()//this method help to fit image into center of your ImageView
                    .into(imageView); //pass imageView reference to appear the image.
    } 

CircleTransform.java

  public class CircleTransform extends BitmapTransformation {
    public CircleTransform(Context context) {
        super(context);

        if(context==null)
            return;
    }

    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;


        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
        return result;
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }

    @Override
    public String getId() {
        return getClass().getName();
    }
}

एनीमेशन में फीका के लिए fade_in.xml

    <set xmlns:android="http://schemas.android.com/apk/res/android">
<!--THIS ANIMATION IS USING FOR FADE IN -->

<alpha
    android:duration="800"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toAlpha="1.0" />

अंत में इस विधि को कॉल करने के लिए।

Utils.loadImage(YourClassName.this,mImageView,url,R.drawable.ic_user,R.drawable.ic_error);

2

आप बस RoundedCornersTransformation कंस्ट्रक्टर को कॉल कर सकते हैं, जिसमें कॉर्नर टाइप एनम इनपुट है। ऐशे ही:

Glide.with(context)
            .load(bizList.get(position).getCover())
            .bitmapTransform(new RoundedCornersTransformation(context,20,0, RoundedCornersTransformation.CornerType.TOP))
            .into(holder.bizCellCoverImg);

लेकिन पहले आपको अपने प्रोजेक्ट में ग्लाइड ट्रांसफ़ॉर्मेशन जोड़ना होगा ।


2

यहाँ ग्लाइड में अपने बिटमैप को क्रॉप करने के लिए एक अधिक मॉड्यूलर और क्लीनर तरीका है:

  1. इस तरह BitmapTransformationफिर ओवरराइड transformविधि का विस्तार करके एक कस्टम परिवर्तन बनाएँ :

ग्लाइड 4.xx के लिए

public class CircularTransformation extends BitmapTransformation {

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    RoundedBitmapDrawable circularBitmapDrawable =
            RoundedBitmapDrawableFactory.create(null, toTransform);
    circularBitmapDrawable.setCircular(true);
    Bitmap bitmap = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    circularBitmapDrawable.setBounds(0, 0, outWidth, outHeight);
    circularBitmapDrawable.draw(canvas);
    return bitmap;
    }

@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {}

}

ग्लाइड 3.xx के लिए

public class CircularTransformation extends BitmapTransformation {

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    RoundedBitmapDrawable circularBitmapDrawable =
            RoundedBitmapDrawableFactory.create(null, toTransform);
    circularBitmapDrawable.setCircular(true);
    Bitmap bitmap = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    circularBitmapDrawable.setBounds(0, 0, outWidth, outHeight);
    circularBitmapDrawable.draw(canvas);
    return bitmap;
    }

@Override
public String getId() {
    // Return some id that uniquely identifies your transformation.
    return "CircularTransformation";
    }

}
  1. फिर इसे ग्लाइड बिल्डर में सेट करें जहां आपको इसकी आवश्यकता है:
Glide.with(yourActivity)
   .load(yourUrl)
   .asBitmap()
   .transform(new CircularTransformation())
   .into(yourView);

उम्मीद है की यह मदद करेगा :)


2
private void setContactImage(@NonNull ViewHolder holder, ClsContactDetails clsContactDetails) {
    Glide.with(context).load(clsContactDetails.getPic())
        .apply(new RequestOptions().centerCrop().circleCrop().placeholder(R.mipmap.ic_launcher)).into(holder.ivPersonImage);
}

2
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'


RequestOptions options=new RequestOptions();
        options.centerCrop().placeholder(getResources().getDrawable(R.drawable.user_placeholder));
        Glide.with(this)
                .load(preferenceSingleTon.getImage())
                .apply(options)
                .into(ProfileImage);

2

सर्कल फसल + प्लेसहोल्डर + क्रॉसफेड

 Glide.with(context!!)
                    .load(randomImage)
                    .apply(RequestOptions.bitmapTransform(CircleCrop()).error(R.drawable.nyancat_animated))
                    .transition(DrawableTransitionOptions()
                            .crossFade())
                    .into(picture)

यहां छवि विवरण दर्ज करें


1

ग्लाइड संस्करण 4.6.1

Glide.with(context)
.load(url)
.apply(RequestOptions.bitmapTransform(new CircleCrop()))
.into(imageView);

1

इस मामले में मुझे छाया जोड़ने की आवश्यकता है, और छवि दृश्य ऊंचाई काम नहीं कर रही है

कार्यान्वयन "com.github.bumptech.glide: glide: 4.10.0"

एक्सएमएल

<FrameLayout
    android:id="@+id/fl_image"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_margin="10dp"
    android:background="@drawable/card_circle_background"
    android:elevation="8dp">

    <ImageView
        android:id="@+id/iv_item_employee"
        android:layout_width="60dp"
        android:layout_height="60dp"
        tools:background="@color/colorPrimary" />
</FrameLayout>

आकार की ओर खींचने योग्य

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
     <solid android:color="@color/white"/>
</shape>

सरस विन्यास

Glide.with(this)
    .asBitmap()
    .load(item.image)
    .apply(RequestOptions.circleCropTransform())
    .into(iv_item_employee)

0

आपको उस प्रकार की छवि प्रदर्शित करने के लिए CircularImageView का उपयोग करना होगा ...

आप ग्लाइड लाइब्रेरी का उपयोग कर रहे हैं जो छवियों को लोड करने के लिए उपयोग किया जाता है ।।

अपने प्रोजेक्ट में एक ClassFile बनाएं और इसे Imageview में लोड करें ... और आपको वांछित परिणाम प्राप्त होंगे ...

कोड का पालन करें ...

एक्सएमएल

 <com.yourpackage.CircularImageView
    android:id="@+id/imageview"
    android:layout_width="96dp"
    android:layout_height="96dp"
    app:border="true"
    app:border_width="3dp"
    app:border_color="@color/white"
    android:src="@drawable/image" />

CircularImageView.java

public class CircularImageView extends ImageView {
    private int borderWidth;
    private int canvasSize;
    private Bitmap image;
    private Paint paint;
    private Paint paintBorder;

    public CircularImageView(final Context context) {
        this(context, null);
    }

    public CircularImageView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.circularImageViewStyle);
    }

    public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        // init paint
        paint = new Paint();
        paint.setAntiAlias(true);

        paintBorder = new Paint();
        paintBorder.setAntiAlias(true);

        // load the styled attributes and set their properties
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CircularImageView, defStyle, 0);

        if(attributes.getBoolean(R.styleable.CircularImageView_border, true)) {
            int defaultBorderSize = (int) (4 * getContext().getResources().getDisplayMetrics().density + 0.5f);
            setBorderWidth(attributes.getDimensionPixelOffset(R.styleable.CircularImageView_border_width, defaultBorderSize));
            setBorderColor(attributes.getColor(R.styleable.CircularImageView_border_color, Color.WHITE));
        }

        if(attributes.getBoolean(R.styleable.CircularImageView_shadow, false))
            addShadow();
    }

    public void setBorderWidth(int borderWidth) {
        this.borderWidth = borderWidth;
        this.requestLayout();
        this.invalidate();
    }

    public void setBorderColor(int borderColor) {
        if (paintBorder != null)
            paintBorder.setColor(borderColor);
        this.invalidate();
    }

    public void addShadow() {
        setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
        paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {
        // load the bitmap
        image = drawableToBitmap(getDrawable());

        // init shader
        if (image != null) {

            canvasSize = canvas.getWidth();
            if(canvas.getHeight()<canvasSize)
                canvasSize = canvas.getHeight();

            BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvasSize, canvasSize, false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            paint.setShader(shader);

            // circleCenter is the x or y of the view's center
            // radius is the radius in pixels of the cirle to be drawn
            // paint contains the shader that will texture the shape
            int circleCenter = (canvasSize - (borderWidth * 2)) / 2;
            canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, ((canvasSize - (borderWidth * 2)) / 2) + borderWidth - 4.0f, paintBorder);
            canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, ((canvasSize - (borderWidth * 2)) / 2) - 4.0f, paint);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // The parent has determined an exact size for the child.
            result = specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {
            // The child can be as large as it wants up to the specified size.
            result = specSize;
        } else {
            // The parent has not imposed any constraint on the child.
            result = canvasSize;
        }

        return result;
    }

    private int measureHeight(int measureSpecHeight) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpecHeight);
        int specSize = MeasureSpec.getSize(measureSpecHeight);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else if (specMode == MeasureSpec.AT_MOST) {
            // The child can be as large as it wants up to the specified size.
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
            result = canvasSize;
        }

        return (result + 2);
    }

    public Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable == null) {
            return null;
        } else if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }
}

ध्यान दें :

आप उपयोग कर सकते हैं

CircularImageView imgIcon = (CircularImageView)findViewById(R.id.imageview);

या

ImageView imgIcon = (ImageView)findViewById(R.id.imageview);

यह आपके अन्य पुस्तकालयों को प्रभावित नहीं करेगा ... न ही छवि या कुछ और डाउनलोड करने के लिए अपना कोड बदलना होगा ... इसे बस एक्सएमएल का उपयोग करके भी परिभाषित किया जा सकता है।


3
मैंने Glide + CircularImageView (और RoundedImageView) की कोशिश की है और यदि आप प्लेसहोल्डर का उपयोग करना शुरू करते हैं (जब आपकी छवि डाउनलोड हो रही है) और उपस्थिति एनिमेशन का उपयोग करना अच्छा नहीं है। पिकासो के पास यह मुद्दा नहीं है। आप इसके बारे में और अधिक जानकारी प्राप्त कर सकते हैं: github.com/vinc3m1/RoundedImageView/issues/76
zmicer

बस ग्लाइड कॉल में .asBitmap का उपयोग करें और यह ठीक काम करेगा।
ग्रानको87

ग्लाइड पर .dontAnimate () को कॉल करने की आवश्यकता होती है जो स्वीकार्य नहीं है
ग्रेग एनिस
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.