Git Product home page Git Product logo

circleimageview's Introduction

CircleImageView

A fast circular ImageView perfect for profile images. This is based on RoundedImageView from Vince Mi which itself is based on techniques recommended by Romain Guy.

CircleImageView

It uses a BitmapShader and does not:

  • create a copy of the original bitmap
  • use a clipPath (which is neither hardware accelerated nor anti-aliased)
  • use setXfermode to clip the bitmap (which means drawing twice to the canvas)

As this is just a custom ImageView and not a custom Drawable or a combination of both, it can be used with all kinds of drawables, i.e. a PicassoDrawable from Picasso or other non-standard drawables (needs some testing though).

Gradle

dependencies {
    ...
    implementation 'de.hdodenhof:circleimageview:3.1.0'
}

Usage

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:src="@drawable/profile"
    app:civ_border_width="2dp"
    app:civ_border_color="#FF000000"/>

Limitations

  • The ScaleType is always CENTER_CROP and you'll get an exception if you try to change it. This is (currently) by design as it's perfectly fine for profile images.
  • Enabling adjustViewBounds is not supported as this requires an unsupported ScaleType
  • If you use an image loading library like Picasso or Glide, you need to disable their fade animations to avoid messed up images. For Picasso use the noFade() option, for Glide use dontAnimate(). If you want to keep the fadeIn animation, you have to fetch the image into a Target and apply a custom animation yourself when receiving the Bitmap.
  • Using a TransitionDrawable with CircleImageView doesn't work properly and leads to messed up images.

FAQ

How can I use a VectorDrawable with CircleImageView?

Short answer: you shouldn't. Using a VectorDrawable with CircleImageView is very inefficient. You should modify your vectors to be in a circular shape and use them with a regular ImageView instead.

Why doesn't CircleImageView extend AppCompatImageView?

Extending AppCompatImageView would require adding a runtime dependency for the support library without any real benefit.

How can I add a selector (e.g. ripple effect) bound to a circle?

There's currently no direct support for a circle bound selector but you can follow these steps to implement it yourself.

How can I add a gap between image and border?

Adding a gap is also not supported directly but there's a workaround.

Changelog

  • 3.1.0
    • Align bitmap paint flags with BitmapDrawable (improves scaling)
  • 3.0.2
    • Fix NPE during initialization on API level <= 19
    • Fix wrong outline being provided if circular transformation is disabled
  • 3.0.1
    • Fix touch event not fired if view is empty
    • Fix touchable area limited to a circle even if transformation is disabled
  • 3.0.0
    • Limit touch event handling to circle area
    • Migrate to AndroidX
    • Remove deprecated properties and methods
  • 2.2.0
    • Add support for elevation
    • Add circle background color attribute to replace fill color
  • 2.1.0
    • Add support for padding
    • Add option to disable circular transformation
    • Fix hairline gap being drawn between image and border under some conditions
    • Fix NPE when using tint attribute (which is not supported)
    • Deprecate fill color as it seems to cause quite some confusion
  • 2.0.0
    • BREAKING: Custom xml attributes are now prefixed with "civ_"
    • Graceful handling of incompatible drawables
    • Add support for a fill color shown behind images with transparent areas
    • Fix dimension calculation issues with small images
    • Fix bitmap not being cleared when set to null
  • 1.3.0
    • Add setBorderColorResource(int resId)
    • Add resource type annotations
    • Add border_overlay attribute to allow drawing border on top of the base image
  • 1.2.2
    • Add ColorFilter support
  • 1.2.1
    • Fix ColorDrawables not being rendered properly on Lollipop
  • 1.2.0
    • Add support for setImageURI(Uri uri)
    • Fix view not being initialized when using CircleImageView(Context context)
  • 1.1.1
    • Fix border being shown although border width is set to 0
  • 1.1.0
    • Add support for ColorDrawables
    • Add getters and setters for border color and border width
  • 1.0.1
    • Prevent crash due to OutOfMemoryError
  • 1.0.0
    • Initial release

License

Copyright 2014 - 2020 Henning Dodenhof

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

circleimageview's People

Contributors

alexander-prime avatar caleb-allen avatar candalo avatar carllee avatar donlianggit avatar hdodenhof avatar jobgetabu avatar keithchad avatar mouse256 avatar mutahirqureshi avatar myanimal avatar start141 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

circleimageview's Issues

Trouble while loading via Robospice

I had the same problem that #5 with RoboSpice library and his SpiceArrayAdapter which use a fade animation.

This SpiceArrayAdapter doesn't provide function to disable this feature, and we can't extends to override some function because the relevant code is in private inner class.

So the basic "solution" that i fund is to extends from CircleImageView for :

@Override
public void setImageDrawable(Drawable drawable){
    if(drawable instanceof TransitionDrawable){
        TransitionDrawable transitionDrawable=(TransitionDrawable) drawable;

        super.setImageDrawable(transitionDrawable.getDrawable(1)); //love magic numbers !
    }
    else super.setImageDrawable(drawable);
}

So we may be we shoud add this in the Limitations section of README ?

Setting a color drawable as the imageDrawable does not work in Android 5.0

In my use case, the circle should sometimes display an image (which I load through Glide) and sometimes should have a solid color (which I set .setImageDrawable(new clorDrawable(Color.parseColor(....))).
The latter works fine in < Android 5.0, but only displays a gray color in Android 5.0
Any ideas?

Allow CircleImageView to work with ColorDrawables

I use ColorDrawables as placeholders while profile pictures are being loaded from the network. However, ColorDrawables have an intrinsic height and width of -1, causing Bitmap.createBitmap to fail in CircleImageView.getBitmapFromDrawable.

Shadow effect

Will it be possible to add shadows to the CircleImageView? Or is it already possible and I just don't know why? :-)

Set minSdkVersion to 8

Are there any issues setting minSdkVersion to 8? Or even a lower value?
At least building with min API 8 works.

Error while compiling build.gradle

In the last couple of hours I'm been trying to use CircleImageView with Android Studio. It worked great with Eclipse, however with Android Studio I'm getting the following error:

Error:No such property: GROUP for class: org.gradle.api.publication.maven.internal.ant.DefaultGroovyMavenDeployer

If I comment the line "apply from: 'https://raw.github.com/hdodenhof/gradle-mvn-push/master/gradle-mvn-push.gradle'" I won't get any error however when I open the xml file I get:

The following classes could not be found: de.hdodenhof.circleimageview.CircleImageView

How can I solve this?
Thanks in advance!

Shadow

please create app:shadow="2dp"

Hides default view(color) of ImageView

Hello,

I am trying to display circle image on profile screen as soon as onActivityResult call is reached, it initially disappears the current view i.e White color of imageview and then it loads. Why it doesn't stay to white until it gets loaded?

<com.kickzexchange.custom.CircleImageView android:id="@+id/ivProfileImage" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="10dp" android:src="@color/White" app:border_color="@color/Maroon" app:border_width="2dp">

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == SELECT_IMAGE_RQ_CODE) {

        if (resultCode == Activity.RESULT_OK) {

            ivProfileImage.setImageResource(mActivity.getResources()
                    .getColor(R.color.White));
            ivProfileImage.setBorderWidth(2);
            ivProfileImage.setBorderColor(getResources().getColor(
                    R.color.Maroon));

            Uri selectedImageUri = data.getData();

            Picasso.with(this).load(selectedImageUri)
                    .into(ivProfileImage);
        }
    }

}

Memory inefficiency

You are creating a bitmap for a color drawable instead of drawing color right into canvas
screen shot 2015-02-24 at 11 11 15 am

Circleimageview seems to define the height of the parent cardview.

On using CircleImageView as a child of cardview, whatever the layout_height of the cardview may be, it takes the height of CircleImageView.

To put it in a different way - the cardview's height somehow becomes wrap content, and becomes just large enough to fit the CircleImageCiew.

Problem About Using CircleImageView in Maven Porject

when I use CicleImageView in my maven project by dependencing Maven Repository,my projectc can`t find CircleImageView。But I find that your maven repository (version 1.2.1) has a values.xml file in res/values directory instand of a attrs.xml file in res/values directory which is in your github project。when I use attrs.xml replace values.xml,my maven project run success。Please insure your values.xml file in your maven repository 。

picasso conflict

have conflict with picasso .when i load drawable twice.

because the method "setImageDrawable()" cause invalidate twice.
I resolve by:
@OverRide
public void setImageDrawable(Drawable drawable) {
mBitmap = getBitmapFromDrawable(drawable);
setup();
super.setImageDrawable(drawable);
}

Transition

In order to support Transition animation you have to implement a RoundedDrawable and round all the layers of LayerDrawable inside TransitionDrawable
I've implemented this in my project, so I can contribute
This is very memory efficient

Animation Transition with UniversalImageLoader

I am trying to set a custom animation when a drawable is set on the CircleImageView, but the view doesn't seem to be refreshed whenever it has finished loading. I am using the view on each of my adapter's rows. The view seems to be stuck with the default drawable it has before I request the Universal Image Loader to load, and it is only updated whenever I cause the row to be recycled (scroll it away and then back in)

here is the code of the displayer:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {


    if ((loadedFrom == LoadedFrom.NETWORK) || (loadedFrom == LoadedFrom.DISC_CACHE)) {

        ImageView imageView = (ImageView) imageAware.getWrappedView();
        Drawable previous = imageView.getDrawable();
        final TransitionDrawable td =
                new TransitionDrawable(new Drawable[]{
                        (previous == null ? transparent : previous),
                        new BitmapDrawable(mResources, bitmap)
                });

        td.setCrossFadeEnabled(true);

        imageAware.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);


    } else {
        imageAware.setImageBitmap(bitmap);
    }

}

Are there any workarounds for this?

Attribute "border_color" has already been defined

Dependency

compile 'de.hdodenhof:circleimageview:1.3.0'

Getting error when building:

Error:(2) Attribute "border_color" has already been defined
Error:(2) Attribute "border_width" has already been defined

I don't have these values in my values/colors.xml or anywhere else.
This problem occured when adding this library to my gradle file.

Background drawable does not draw to circle

I am having issues when i set the background of the circular image view. The resource specified in the background dows not draw to circle.
Basically, I am having a trapsparent background image, If I do not set the background, the view in the back of circular image view gets displated in the remaining space. But when i try to set the background resource (Basically a color), it is being drawn to full layout (REMAINING SPACE OUTSIDE THE CIRCLE {AS CircleImageView is basically a rectangular canvas, with a circular image on top of it.})

scaleTyle

are you planning to support other scaleTypes apart from default which you support?

If Not >>> can you guide or add pointers to do the same.

Set fixed size for image

hi i want to set size for all images
for example i have four images 200_100 and 150_150 and 300*200 and 1500 *1000

i want to set size for all image 200*300
what should i do ?
thanks alot

Problems with Universal Image Loader

Is it support the downloaded image from network using Universal Image Loader ? I am getting error when try to set image downloaded from network using UIL .

Picasso fading getting stuck

I'm using Picasso to load images into your image view and the fade is causing problems for it. Because you are getting the bitmap from the drawable it's only getting the drawable in a static state. It will only show the placeholder if the image hasn't loaded yet, it will show a cross faded image if the image is being faded, or it could show the actual image if the load is quick enough to be done before it gets the Bitmap.

You can work around this by disabling the fade, but it would be great if this wasn't necessary.

How to set background color to circleImageView

I have used your class in my app.Actually i wanted to set background to whole circle imageview but it is not working,when i am setting background color it is setting in square shape,but i need in circle imageview only

Loading Internet Images with Glide

Till now I used to use Picasso for loading images. Right now I'm moving to Glide and there's an issue with that.

When loading images from the internet for the first time, it doesn't show up in the CircleImageView (Glide isn't able to replace the placeholder image).

Setup:

I have an activity with just a CircleImageView. I use the below code to load image using glide

Glide.with(this)
    .load("https://s3.amazonaws.com/xyz.png")
    .placeholder(R.drawable.abc)
    .into(imgView);

Steps to Reproduce:

  1. Fresh install and open the app
  2. The placeholder shows up, while the image is getting downloaded
  3. The placeholder is never replaced by the actual image, even though the image has been downloaded and cached
  4. Go back
  5. Re-open the app
  6. The image appears

Note: I have tried replacing the CircleImageView with a regular ImageView and it works perfectly.

Problem with 0px width border

1px border is drawn even when border width is set to ;

Cause:

public void setStrokeWidth (float width)

Added in API level 1
Set the width for stroking. Pass 0 to stroke in hairline mode. Hairlines always draws a single pixel independent of the canva's matrix.

Problems with Lollipop - black image

Hi,

I'm facing a quite interesting problem on Android Lollipop - if I set Bitmap to CircleImageView with setImageBitmap() method, it just shows black image:
monosnap 2015-01-05 16-19-32

Image is selected from Gallery app, and I can view it in debug mode (bitmap is not corrupted).

Everything is working OK with default ImageView:
monosnap 2015-01-05 16-23-12

This behaviour is random - some images are working correctly, some are not, and it occurs only on Android Lollipop.

Do you have any suggestions? I've checked app logs, everything is working OK - no OutOfMemory exceptions or something similar.

getDrawingCache return null

I'm trying to get a Bitmap from CircleImageView using getDrawingCache and always get null. I do this...

    CircleImageView civProfilePic = new CircleImageView(this);
    civProfilePic.setBorderWidth(5);
    civProfilePic.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.user2));
    Bitmap roundImage = civProfilePic.getDrawingCache();

and roundImage == null

Drawable with colorFilter

Hi man,
thanks for sharing your work with us.
Working with your library I discover a problem with drawable and color filter.
If i write something like:
Drawable drawable = getResources().getDrawable(R.drawable.generic_image); drawable.setColorFilter(COLOR.RED, PorterDuff.Mode.SRC_IN); circleImageView.setImageDrawable(drawable);

The drawable will be load correctly but without the color filter.
Using the same code with a normal ImageView the drawable will be rendered correctly.
This problem can be generated with every image (advice: use a gray image the color filter will be more effective).
I'm here if you need more info.
Thanks again.

Padding of the image

Hi! Thanks a lot for this great View!
Is it possible to setup padding of the image to make it look smaller inside the circle?
android:padding - works OK for default ImageView, but not for CircleImageView.

I see the border_width attribute, but it makes image to be cut in circle and resize, not just scale it as-is.

Trouble while loading via Picasso

I use your library (btw: great work!) for user profile images in my app. User images come via Facebook and are loaded using Picasso.

However, when I open a fragment / activity that contains a CircleImageView, it gets first filled with my placeholder image. "Behind the scenes", Picasso starts loading the image and finally puts it in my CircleImageView object. But it's nit displayed immediately and I still see the placeholder! I have to perform somehow a "view reloading" action, like scrolling down in a ListView and then scroll up again back to the CircleImageView. Only at this point the real picture gets displayed instead of the placeholder image.

It seems like the CircleImageView does not redraw its content if Picasso puts the loaded content in there. In your README you stated that the library can be used with Picasso .. did you have the same problems? Are any workarounds known?

Not Working in layout weight

           <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center"
    android:layout_weight="1">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="96dp"



        android:src="@drawable/ic_launcher"
        android:layout_gravity="center"
        />

</LinearLayout>




<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1"
    android:orientation="vertical">
<suntek.com.expensemanager.ImageView.CircleImageView
    android:layout_width="match_parent"
    android:layout_height="96dp"


    android:src="@drawable/ic_launcher"
    android:layout_gravity="center"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
    </LinearLayout>

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical">
    <suntek.com.expensemanager.ImageView.CircleImageView
        android:layout_width="match_parent"
        android:layout_height="96dp"


        android:src="@drawable/ic_launcher"
        android:layout_gravity="center"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       />
</LinearLayout>

unnamed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.