GithubHelp home page GithubHelp logo

muhammadammad / avatar-view Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tangoagency/avatar-view

0.0 2.0 0.0 1.23 MB

Avatar ImageView with user's name first letter Drawable placeholder

License: MIT License

Java 100.00%

avatar-view's Introduction

Avatar View

Download Build Status Android Arsenal Avatar View

Avatar View library was implemented based on Matt Precious's Don’t Fear the Canvas lecture. I decided to create this library in order to achieve an ImageView which can smoothly display user's profile image or his username/name initial letter (in the case when the image was not provided).

Please take a look at the examples below:

Simple library presentation ExampleActivity & BindingsExample
FirstExample SecondExample

Usage

This library can be used in two ways: using standard Android methods and using Android Data Binding.

###Standard:

Step 1

Add gradle dependency:

dependencies {
    compile 'agency.tango.android:avatar-view:{latest_release}'

    //if you want to use Picasso for loading images
    compile 'agency.tango.android:avatar-view-picasso:{latest_release}'
}

Step 2

Add to your xml layout file:

<agency.tango.android.avatarview.views.AvatarView
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:av_border_color="@android:color/white"
    app:av_border_width="4dp"
    app:av_text_size_percentage="35" />

Step 3

Add to your activity:

    AvatarView avatarView;
    IImageLoader imageLoader;

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

        avatarView = (AvatarView) findViewById(R.id.avatar_view_example);

        imageLoader = new PicassoLoader();
        imageLoader.loadImage(avatarView, "http:/example.com/user/someUserAvatar.png", "User Name");
    }

If you want to use a different library than Picasso for loading images you have to create a loader which extends ImageLoaderBase class. Basically, you have to override one method. Take a look how I have done it in PicassoLoader available in the avatar-view-picasso module.

ImageLoaderBase has two constructors: one with no parameters and second one where you can pass String placeholder in order to change the default "-". You can also set it directly in AvatarPlaceholder constructor. More info about AvatarPlaceholder here.

###Data Binding:

Step 1

Add gradle dependency:

dependencies {
    compile 'agency.tango.android:avatar-view:{latest_release}'
    compile 'agency.tango.android:avatar-view-bindings:{latest_release}'

    //if you want to use Picasso for loading images
    compile 'agency.tango.android:avatar-view-picasso:{latest_release}'
}

Step 2

I will show how to edit your xml file based on User class. Let's see:

<data>
    <variable
        name="user"
        type="User" />
</data>

<agency.tango.android.avatarview.views.AvatarView
    android:layout_width="100dp"
    android:layout_height="100dp"
    bind:av_border_color="@android:color/white"
    bind:av_border_width="6dp"
    bind:av_text_size_percentage="40"
    bind:avatarUrl="@{user.avatarUrl}"
    bind:name="@{user.name}" />

Step 3

Add to your activity:

private ExampleActivityBinding binding;

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

    binding = DataBindingUtil.setContentView(this, R.layout.example_activity, new ExampleDataComponent());
    binding.setUser(new User("User Name", "http:/example.com/user/someUserAvatar.png"));
}

private static class ExampleDataComponent implements DataBindingComponent {
    public AvatarViewBindings getAvatarViewBindings() {
        return new AvatarViewBindings(new PicassoLoader());
    }
}

Take a look at AvatarViewBindings class where BindingsAdapter is configured (bind:avatarUrl and bind:name for usage in XML). In order to correctly use AvatarViewBindings you have to create class extending DataBindingComponent and pass it as a third parameter in DataBindingUtil.setContentView() method. This is obligatory because AvatarViewBindings takes an IImageLoader parameter in it's constructor. You can find more information about this topic in Deep dive into Android Data Binding talk.

I have explained PicassoLoader issue in step 3 in Standard Method part.

####AvatarPlaceholder

AvatarPlaceholder is a Drawable which is set as a AvatarView background when the image hasn't been loaded yet. It is a letter on a one-color background (just like in ex. Google, Youtube avatars). Default placeholder String (displayed when the username is null or empty) is "-". TextSizePercentage value sets how big part of the view is taken by the text. Default textSizePercentage is 33. You can change those values by passing another ones in AvatarPlaceholder constructor or not directly by using IImageLoader class methods/constructors.

####Additional information

  • Avatar background color is calculated using hashCode() method called on a given name String.
  • Default border width is 2dp and default border color is white.
  • Placeholder letters are currently always white (in future user will be able to choose a different color).
  • It is recommended to set your default placeholder String as short as possible (the best would be one letter).

####Feel free to create issues and pull requests!

avatar-view's People

Contributors

mzielu avatar radzio avatar

Watchers

 avatar  avatar

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.