Android's method of using Circleimageview and cardview to make rounded avatars _android

Source: Internet
Author: User

Round avatars are common in our everyday apps, because the rounded heads are more beautiful.

The method of using a circular picture may be that we crop the picture directly into a circle and use it in the app, and use a custom view to automatically crop any picture we set to a circle.

Effect Chart:

Here use GitHub on Circleimageview

Github:https://github.com/hdodenhof/circleimageview

CardView is the name of the card View,cardview inherited Framelayout, so put the internal control when you need to pay attention to

You can set shadows, rounded corners, and so on.

The Circleimageview can also be stroked for avatar settings.

We create a new project and select navigation Drawer activity to automatically generate the initial layout.

Modify Nav_header_main, add fillet avatar

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" "@dimen/nav_header_height"
android:background= "@drawable/side_nav_bar"
android:gravity= "bottom"
android:orientation= "vertical"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_ Horizontal_margin "
android:paddingright=" @dimen/activity_horizontal_margin "
android:paddingtop=" @ Dimen/activity_vertical_margin "
android:theme=" @style/themeoverlay.appcompat.dark ">
< 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/darth_vader"
app:civ_border_width= "2DP"
/>

And then modify Content_main, add Recyclerview, remember guide bag

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://" Schemas.android.com/apk/res/android "
xmlns:app=" Http://schemas.android.com/apk/res-auto "
xmlns:tools=" Http://schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_ Parent "
android:paddingbottom=" @dimen/activity_vertical_margin "
android:paddingleft=" @dimen/activity_ Horizontal_margin "
android:paddingright=" @dimen/activity_horizontal_margin "
android:paddingtop=" @ Dimen/activity_vertical_margin "
app:layout_behavior=" @string/appbar_scrolling_view_behavior "
tools: context= "com.example.xw.design2.MainActivity"
tools:showin= "@layout/app_bar_main" >
< Android.support.v7.widget.RecyclerView
android:id= "@+id/rv"
android:layout_width= "Match_parent"
android:layout_height= "match_parent" ></android.support.v7.widget.RecyclerView>
</ Relativelayout>

Add Item layout, CardView, remember guide package

<?xml version= "1.0" encoding= "Utf-8"?> <android.support.v7.widget.cardview android:layout_
height= "Wrap_content"
android:layout_width= "match_parent"
xmlns:card_view= "http://schemas.android.com/" Apk/res-auto "
card_view:contentpadding=" 10DP "
card_view:cardbackgroundcolor=" #303069 "
Card_view: cardcornerradius= "10DP"
card_view:cardpreventcorneroverlap= "true"
card_view:cardusecompatpadding= "true "
xmlns:android=" http://schemas.android.com/apk/res/android ">
<textview
android:id=" @+id/ TV "
android:textcolor=" #fff "
android:layout_width=" wrap_content "
android:layout_height=" Wrap_ Content "/>

Next, add the code to the mainactivity and use our CardView

1. Add member variables and data sources

Private Recyclerview Mrecyclerview;
Private string[] data={"in 2014, with the introduction of Google's new design language Material, the new Android Support Library V7, which includes the Material design language for C The realization of ARD card concept--cardview. "
," after a long time believing that many Android developers have applied this control, it may be a bit late to write this article, but for the developers who are just starting to use and other students who have been using it for some time but have not done so well should be able to help a little. "
," Google introduced the Material design in the Android Lollipop Shadow (elevation) and Z-axis displacement, the purpose is to highlight the interface between the different elements of the hierarchical relationship "
," next summer, Free agent Blake Griffin may return to Oklahoma City to join forces with Russell-Brooks. If it does, can the Thunder force really threaten the warriors? "};

2. Create Viewholder

Class Myholder extends recyclerview.viewholder{
private TextView Mtextview;
Public Myholder (View itemview) {
super (Itemview);
mtextview= (TextView) Itemview.findviewbyid (r.id.tv);
}

3. Create Adapter

Class Myadapter extends recyclerview.adapter<myholder>{
@Override public
myholder Oncreateviewholder ( ViewGroup parent, int viewtype) {
layoutinflater layoutinflater=layoutinflater.from (Getapplicationcontext ());
View v=layoutinflater.inflate (r.layout.item,parent,false);
Myholder holder=new Myholder (v);
return holder;
}
@Override public
void Onbindviewholder (myholder holder, int position) {
Holder.mTextView.setText (data[ Position]);
}
@Override public
int GetItemCount () {return
data.length}
}
}

4.oncreate () method set Adapter

mrecyclerview= (Recyclerview) Findviewbyid (R.ID.RV);
Mrecyclerview.setlayoutmanager (This) (new Linearlayoutmanager);
Mrecyclerview.setadapter (New Myadapter ());

The above is a small set of Android to introduce the use of Circleimageview and CardView to make round head of the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.