Android custom control's circular diffusion view (Diffuseview)

Source: Internet
Author: User

    • Achieve results
    • Use
    • Property method
    • Code
    • SOURCE download
    • Reference links

Achieve results

Use

In XML:

<Com.airsaid.diffuseview.widget.DiffuseView Android:id="@+id/diffuseview"Android:layout_width="match_parent"Android:layout_height="0DP"Android:layout_weight="1"App:diffuse_color="@color/coloraccent"App:diffuse_corecolor="@color/colorprimarydark"App:diffuse_coreimage="@android:d Rawable/ic_menu_search"App:diffuse_coreradius=" -"App:diffuse_maxwidth=" -"App:diffuse_width="4"/>

In the code:

Diffuseview Mdiffuseview =//  start diffusion mdiffuseview.stop (); // Stop Spreading
Properties & Methods
Property name Java Methods function
Diffuse_color SetColor (int colorid) Set the diffuse circle color
Diffuse_corecolor Setcorecolor (int colorid) Set Center circle Color
Diffuse_coreimage Setcoreimage (int imageId) Set the center circle picture
Diffuse_coreradius Setcoreradius (int radius) Set Center circle Radius
Diffuse_maxwidth Setmaxwidth (int maxWidth) Set Maximum spread width
Diffuse_width Setdiffusewidth (int width) Set the spread circle width, the smaller the value the wider
Code
/** * Created by zhouyou on 2016/9/27. * Class DESC: * * This is a custom circle spread view*/ Public classDiffuseview extends View {/** Diffuse circle Color*/    Private intMcolor =getresources (). GetColor (r.color.coloraccent); /** Circle Center Color*/    Private intMcorecolor =getresources (). GetColor (R.color.colorprimary); /** Circle Center Picture*/    PrivateBitmap Mbitmap; /** Center Circle Radius*/    Private floatMcoreradius = Max; /** Diffusion Circle width*/    Private intMdiffusewidth =3; /** Maximum width*/    PrivateInteger Mmaxwidth =255; /** Whether it is spreading*/    PrivateBoolean misdiffuse =false; //Transparency Collection    PrivateList<integer> Malphas =NewArraylist<>(); //Diffusion Circle radius set    PrivateList<integer> mwidths =NewArraylist<>(); PrivatePaint Mpaint;  PublicDiffuseview (Context context) { This(Context,NULL); }     PublicDiffuseview (Context context, AttributeSet attrs) { This(Context, Attrs,-1); }     PublicDiffuseview (context context, AttributeSet attrs,intdefstyleattr)        {Super (context, attrs, defstyleattr);       Init (); TypedArray a= Context.obtainstyledattributes (Attrs, R.styleable.diffuseview, Defstyleattr,0); Mcolor=A.getcolor (R.styleable.diffuseview_diffuse_color, Mcolor); Mcorecolor=A.getcolor (R.styleable.diffuseview_diffuse_corecolor, Mcorecolor); Mcoreradius=a.getfloat (R.styleable.diffuseview_diffuse_coreradius, Mcoreradius); Mdiffusewidth=A.getint (R.styleable.diffuseview_diffuse_width, mdiffusewidth); Mmaxwidth=A.getint (R.styleable.diffuseview_diffuse_maxwidth, mmaxwidth); intImageId = A.getresourceid (R.styleable.diffuseview_diffuse_coreimage,-1); if(ImageId! =-1) Mbitmap =Bitmapfactory.decoderesource (Getresources (), imageId);    A.recycle (); }    Private voidinit () {Mpaint=NewPaint (); Mpaint.setantialias (true); Malphas.add (255); Mwidths.add (0); } @Override Public voidinvalidate () {if(Haswindowfocus ()) {super.invalidate (); }} @Override Public voidOnDraw (canvas canvas) {//Draw a diffusion circleMpaint.setcolor (Mcolor);  for(inti =0; I < malphas.size (); i++) {            //Set TransparencyInteger alpha = Malphas.Get(i);            Mpaint.setalpha (Alpha); //Draw a diffusion circleInteger width = mwidths.Get(i); Canvas.drawcircle (getwidth ()/2, GetHeight ()/2, Mcoreradius +width, mpaint); if(Alpha >0&& Width <mmaxwidth) {Malphas.Set(I, alpha-1); Mwidths.Set(I, Width +1); }        }        //to add a new diffusion circle when the diffusion circle is diffused to the specified width        if(Mwidths.Get(Mwidths.size ()-1) = = Mmaxwidth/mdiffusewidth) {Malphas.add (255); Mwidths.add (0); }        //over 10 diffusion circles, remove outermost        if(Mwidths.size () >=Ten) {Mwidths.remove (0); Malphas.remove (0); }        //Draw Center circle and pictureMpaint.setalpha (255);        Mpaint.setcolor (Mcorecolor); Canvas.drawcircle (getwidth ()/2, GetHeight ()/2, Mcoreradius, Mpaint); if(Mbitmap! =NULL) {Canvas.drawbitmap (Mbitmap, getwidth ()/2-Mbitmap.getwidth ()/2, GetHeight ()/2-Mbitmap.getheight ()/2, Mpaint); }        if(Misdiffuse) {invalidate (); }    }    /** * start to spread*/     Public voidstart () {Misdiffuse=true;    Invalidate (); }    /** * Stop spreading*/     Public voidStop () {Misdiffuse=false; }    /** * Whether in the spread*/     PublicBoolean isdiffuse () {returnMisdiffuse; }    /** * Set diffusion circle color*/     Public voidSetColor (intColorid) {Mcolor=Colorid; }    /** * Set Center circle color*/     Public voidSetcorecolor (intColorid) {Mcorecolor=Colorid; }    /** * Set the center circle picture*/     Public voidSetcoreimage (intimageId) {Mbitmap=Bitmapfactory.decoderesource (Getresources (), imageId); }    /** * Set Center circle Radius*/     Public voidSetcoreradius (intradius) {Mcoreradius=radius; }    /** * Set the diffusion circle width ( the smaller the value the greater the width)*/     Public voidSetdiffusewidth (intwidth) {Mdiffusewidth=width; }    /** * Set the maximum width*/     Public voidSetmaxwidth (intmaxWidth) {Mmaxwidth=MaxWidth; }}
Source Download

Github:https://github.com/airsaid/diffuseview

Android custom control's circular diffusion view (Diffuseview)

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.