Android 3d Cloud Tag

Source: Internet
Author: User
Tags comparable cos gety sin

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbwluz3l1zv8xmti4/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "> A, define your own view

public class Tagcloudview extends Relativelayout {relativelayout navigation_bar; TextView mtextview1;private final Float touch_scale_factor =. 8f;private float tspeed;private Tagcloud mtagcloud;private float Manglex =0;private float Mangley =0;private float centerx, centery;private float radius;private Context mcontext;pri Vate list<textview> mtextview;private list<relativelayout.layoutparams> mParams;private int shiftLeft; float dowx = 0;float dowy = 0;float cutx=100;float cuty=100;public tagcloudview (Context mcontext, int width, int height, L Ist<tag> tagList) {This (Mcontext, width, height, tagList, 6, 34, 1);} Public Tagcloudview (Context mcontext, int width, int height, list<tag> tagList, int textsizemin, int. Textsizemax, in T scrollspeed) {super (mcontext); this.mcontext= mcontext;tspeed = Scrollspeed;centerx = Width/2;centery = Height/2;radi US = math.min (CenterX * 0.95f, CenterY * 0.95f); Shiftleft = (int) (Math.min (CenterX * 0.15f, CenterY * 0.15f)); MTAGCLoud = new Tagcloud (tagList, (int) radius, textsizemin, Textsizemax); float[] TempColor1 = {0.9412f,0.7686f,0.2f,1}; RGB Alpha//{1f,0f,0f,1} red {0.3882f,0.21568f,0.0f,1} orange//{0.9412f,0.7686f,0.2f,1} light orangefloat[] TempC Olor2 = {1f,0f,0f,1}; RGB Alpha//{0f,0f,1f,1} blue {0.1294f,0.1294f,0.1294f,1} grey//{0.9412f,0.7686f,0.2f,1} light Orangemtagcloud.set TagColor1 (TempColor1);//higher Colormtagcloud.settagcolor2 (TempColor2);//lower Colormtagcloud.setradius ((int)     RADIUS); Mtagcloud.create (true);    Mtagcloud.setanglex (Manglex);    Mtagcloud.setangley (Mangley); Mtagcloud.update (); mtextview = new arraylist<textview> (); mparams = new arraylist<    Relativelayout.layoutparams> ();    Iterator<?> It=mtagcloud.iterator ();    Tag Temptag;    int i=0;                  Remove each data into the Texview while (It.hasnext ()) {temptag= (Tag) it.next ();     Temptag.setparamno (i);    Mtextview.add (New TextView (This.mcontext)); Mtextview.get (i). SetText (Temptag.getteXT ());      Mparams.add (New Relativelayout.layoutparams (Layoutparams.wrap_content, layoutparams.wrap_content));    Mparams.get (i). AddRule (Relativelayout.align_parent_left);    Mparams.get (i). AddRule (Relativelayout.align_parent_top); Mparams.get (i). SetMargins ((int) (Centerx-shiftleft + TEMPTAG.GETLOC2DX ()), (int) (CenterY + temptag.getloc2dy ()), 0,0)    ;    Mtextview.get (i). Setlayoutparams (Mparams.get (i));    Mtextview.get (i). Setsingleline (True); int mergedcolor = COLOR.ARGB ((int) (Temptag.getalpha () * 255), (int) (Temptag.getcolorr () * 255), (int) (temptag.getcolorg    () * 255), (int) (Temptag.getcolorb () * 255));    Mtextview.get (i). SetTextColor (Mergedcolor);    Mtextview.get (i). Settextsize ((int) (Temptag.gettextsize () * Temptag.getscale ()));    AddView (Mtextview.get (i));    Mtextview.get (i). Setonclicklistener (Ontagclicklistener (Temptag.geturl ()));    Set each Texview to have its own specified label for its own location so that later operations Mtextview.get (i). Settag (i);    i++; }/** is used to actively play the */New Timer (). Schedule (new TimerTask () {@overridepublic void Run () {handler.sendemptymessage (1);}}, 0,200);} @SuppressLint ("Handlerleak") Handler handler=new Handler () {@Overridepublic void Handlemessage (Message msg) { Super.handlemessage (msg); Manglex = (Cuty/radius) *tspeed * Touch_scale_factor;mangley = (-cutx/radius) *tspeed * TOUCH_ Scale_factor;changposition ();}}; @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);} /** * Trigger Event */@Overridepublic Boolean ontouchevent (motionevent e) {switch (e.getaction ()) {case Motionevent.action_down: Dowx=e.getx ();d owy=e.gety (); break;case MotionEvent.ACTION_UP:float upx=e.getx (); float upy=e.gety (); cutx=upx-dowx; Cuty=upy-dowy;break;case MotionEvent.ACTION_MOVE:mAngleX = (cuty/radius) *tspeed * Touch_scale_factor;mangley = (-cutx /radius) *tspeed * touch_scale_factor;changposition (); break;} return true;}    /** * Change position */private void Changposition () {Mtagcloud.setanglex (Manglex);    Mtagcloud.setangley (Mangley);    Mtagcloud.update (); iterator<?

> It=mtagcloud.iterator (); Tag Temptag; while (It.hasnext ()) {temptag= (Tag) it.next (); Mparams.get (Temptag.getparamno ()). SetMargins ((int) (Centerx-shiftleft + TEMPTAG.GETLOC2DX ()), (int) (CenterY + Temptag.getloc2dy ()), 0, 0); Mtextview.get (Temptag.getparamno ()). Settextsize ((int) (Temptag.gettextsize () * Temptag.getscale ())); int mergedcolor = COLOR.ARGB ((int) (Temptag.getalpha () * 255), (int) (Temptag.getcolorr () * 255), (int) (temptag.getcolorg () * 255), (int) (Temptag.getcolorb () * 255)); Mtextview.get (Temptag.getparamno ()). SetTextColor ( Mergedcolor); Mtextview.get (Temptag.getparamno ()). BringToFront (); }}/** * Click event * @param URL * @return */view.onclicklistener ontagclicklistener (final String URL) {return new View.onclicklis Tener () {@Overridepublic void OnClick (View v) {}};}}



Ii. self-defined iterators


/** * self-defined iterator * @author Administrator * */public class Tagcloud implements Iterable<object>{private list<tag> tagcloud;private int radius;private static final int default_radius = 3;private static final int text_size_max = +, TEX t_size_min= 4;private static final float[] default_color1= {0.886f, 0.725f, 0.188f, 1f};p rivate static final float[] DEFA ult_color2= {0.3f, 0.3f, 0.3f, 1f};p rivate float[] tagcolor1;private float[] TagColor2; private int Textsizemax, textsizemin;private float sin_manglex,cos_manglex,sin_mangley,cos_mangley,sin_manglez,cos_    manglez;private float manglez=0;    private float Manglex = 0;    private float Mangley = 0;    private int size=0;    private int smallest,largest;     Private Boolean Distreven = true; Public Tagcloud () {This (new arraylist<tag> ());} Public Tagcloud (list<tag> tags) {this (Tags,default_radius);} Public Tagcloud (list<tag> tags, int. radius) {This (tags, radius, default_color1, Default_color2, Text_size_min, text_sIze_max);} Public Tagcloud (list<tag> tags, int radius,int textsizemin, int. Textsizemax) {This (tags, radius, default_color1, DE Fault_color2, Textsizemin, Textsizemax);} Public Tagcloud (list<tag> tags, int. radius,float[] TagColor1, float[] tagColor2) {This (tags, radius, tagColor1, Tag   Color2, Text_size_min, Text_size_max);} Public Tagcloud (list<tag> tags, int radius, float[] tagColor1, float[] tagColor2, int textsizemin, int textsizemax) {    This.tagcloud=tags; This.radius = Radius;this.tagcolor1 = Tagcolor1;this.tagcolor2 = Tagcolor2;this.textsizemax = TextSizeMax; This.textsizemin = Textsizemin;} /** * Override method */@Overridepublic Iterator Iterator () {return tagcloud.iterator ();} /** * Create * @param distreven */public void Create (Boolean Distreven) {This.distreven =distreven;positionall (distreven); Sinecosine (Manglex, Mangley, Manglez); UpdateAll (); smallest = 9999;largest = 0;for (int i=0; i< tagcloud.size (); i++) {i NT J = tagcloud.get (i). getpopularity (); largest = Math.max (largEST, j); smallest = Math.min (smallest, j);} Tag temptag;for (int i=0; i< tagcloud.size (); i++) {Temptag = Tagcloud.get (i); int j = temptag.getpopularity (); float perc Entage = (Smallest = = largest)?

1.0f: ((float) j-smallest)/((float) largest-smallest); float[] Tempcolor = getcolorfromgradient (percentage); (RGB Alpha) int temptextsize = gettextsizegradient (percentage); Temptag.setcolorr (tempcolor[0]); Temptag.setcolorg ( TEMPCOLOR[1]); Temptag.setcolorb (tempcolor[2]); Temptag.settextsize (temptextsize);} This.size= tagcloud.size ();} /** * Create created, you need update */public void update () {if (Math.Abs (Manglex) >. 1 | | Math.Abs (Mangley) >. 1) {Sinecosine (Manglex, Mangley, Manglez); UpdateAll ();}} /** * Calculates each tag's * @param whether Distreven is calculated based on the word true to Yes, otherwise the word has overwritten */private void Positionall (Boolean distreven) {Double phi = 0;d Ouble theta = 0;int max = Tagcloud.size (); for (int i=1; i<max+1; i++) {if (Distreven) {phi = Math.acos ( -1.0 + (2.0*i-1.0 )/max); theta = math.sqrt (max*math.pi) * PHI;} Else{phi = Math.random () * (Math.PI); theta = Math.random () * (2 * Math.PI);} Tagcloud.get (i-1). Setlocx ((int) ((RADIUS * Math.Cos (theta) * Math.sin (PHI))); Tagcloud.get (i-1). Setlocy ((int) ( RADIUS * Math.sin (theta) * Math.sin (PHI)); Tagcloud.get (i-1). SETLOCZ ((int) (RADIUS * Math.Cos (PHI)));}} /** * Update all tag locations */private void UpdateAll () {int max = tagcloud.size (); for (int j=0; j<max; J + +) {float Rx1 = (tagcloud.g ET (j). Getlocx ()); float Ry1 = (Tagcloud.get (j). Getlocy ()) * Cos_manglex +tagcloud.get (j). GETLOCZ () *-sin_manglex;float RZ1 = (Tagcloud.get (j). Getlocy ()) * Sin_manglex +tagcloud.get (j). GETLOCZ () * Cos_manglex;float rx2 = rx1 * Cos_mangley + R Z1 * Sin_mangley;float Ry2 = ry1;float rz2 = rx1 *-sin_mangley + rz1 * cos_mangley;float rx3 = rx2 * Cos_manglez + ry2 * -sin_manglez;float ry3 = rx2 * Sin_manglez + ry2 * cos_manglez;float rz3 = Rz2;tagcloud.get (j). Setlocx (RX3); Tagcloud.get (j ). Setlocy (Ry3); Tagcloud.get (j). Setlocz (rz3); int diameter = 2 * radius;float per = diameter/(diameter+rz3); tagcloud.get (j). SETLOC2DX ((int) (RX3 * per)), Tagcloud.get (j). Setloc2dy ((int) (RY3 * per)), Tagcloud.get (j). Setscale (per); Tagcloud.get (j). Setalpha (PER/2);} Sort tag Collections.sort (tagcloud);} /** * Calculate Font Color * @paraM perc * @return */private float[] getcolorfromgradient (float perc) {float[] Temprgb = new Float[4];temprgb[0] = (PERC * ( Tagcolor1[0]) + ((1-PERC) * (Tagcolor2[0])), temprgb[1] = (PERC * (tagcolor1[1))) + ((1-PERC) * (Tagcolor2[1] )); temprgb[2] = (PERC * (tagcolor1[2)) + ((1-PERC) * (tagcolor2[2])); temprgb[3] = 1; return TEMPRGB;} /** * Calculate the size of the font * @param perc * @return */private int gettextsizegradient (float perc) {int size;size = (int) (Perc*textsizema x + (1-PERC) *textsizemin); return size;} /** * Calculates the x y z coordinate of the circle * @param manglex * @param mangley * @param manglez */private void Sinecosine (float manglex,float mAngle Y,float Manglez) {Double Degtorad = (math.pi/180); sin_manglex= (float) math.sin (Manglex * degtorad); cos_manglex= (float ) Math.Cos (Manglex * degtorad); sin_mangley= (float) math.sin (Mangley * degtorad); cos_mangley= (float) math.cos (MAngleY * Degtorad); sin_manglez= (float) math.sin (Manglez * degtorad); cos_manglez= (float) math.cos (Manglez * degtorad);}/** * Below is the Get Set method * @return */public int Getradius () {return radius;} public void Setradius (int radius) {This.radius = radius;} Public float[] GetTagColor1 () {return tagColor1;} public void SetTagColor1 (float[] tagcolor) {this.tagcolor1 = Tagcolor;} Public float[] GetTagColor2 () {return tagColor2;} public void SetTagColor2 (float[] tagColor2) {This.tagcolor2 = TagColor2;} Public float getrvalue (float[] color) {if (color.length>0) return Color[0];elsereturn 0;} Public float getgvalue (float[] color) {if (color.length>0) return Color[1];elsereturn 0;} Public float getbvalue (float[] color) {if (color.length>0) return Color[2];elsereturn 0;} Public float getalphavalue (float[] color) {if (Color.length >= 4) return Color[3];elsereturn 0;} public float Getanglex () {return manglex;} public void Setanglex (float manglex) {This.manglex = Manglex;} public float Getangley () {return Mangley;} public void Setangley (float Mangley) {This.mangley = Mangley;} public int GetSize () {return size;}}


Iii. Define your own data

/** * Comparable interface can define its own sorting method * @author Administrator * */public class Tag implements Comparable<tag>{private String  text, url;private int popularity;private int textsize;private float LocX, locy, Locz;    Private float loc2dx, loc2dy;private float scale;private float Colorr, colorg, Colorb, Alpha;    private static final int default_popularity = 1;     private int Paramno; Public tags (String text, int popularity) {This (text, 0f, 0f, 0f, 1.0f, popularity, "");} Public tags (string text, int popularity, string url) {This (text, 0f, 0f, 0f, 1.0f, popularity, URL);} Public tags (String text,float locX, float locy, float Locz) {This (text, LocX, Locy, Locz, 1.0f, Default_popularity, "");} Public tags (String text,float locX, float locy, float locz, float scale) {This (text, LocX, locy, Locz, scale, Default_popul ARITY, "");}    Public tags (string text,float locX, float locy, float locz, float scale, int popularity, string url) {this.text = text;    This.locx = LocX;    This.locy = Locy; This.locz = Locz;    THIS.LOC2DX = 0;    this.loc2dy=0;    This.colorr= 0.5f;    This.colorg= 0.5f;    This.colorb= 0.5f;    This.alpha = 1.0f;    This.scale = scale;    This.popularity= popularity;    This.url = URL;    } @Overridepublic int CompareTo (Tag another) {//Sort by return (int) (ANOTHER.LOCZ-LOCZ);} public float Getlocx () {return locX;} public void Setlocx (float locX) {this.locx = LocX;} public float Getlocy () {return locy;} public void Setlocy (float locy) {this.locy = Locy;} public float Getlocz () {return LOCZ;} public void Setlocz (float locz) {This.locz = Locz;} public float Getscale () {return scale;} public void Setscale (float scale) {This.scale = scale;} Public String GetText () {return text;} public void SetText (String text) {this.text = text;} public float Getcolorr () {return colorr;} public void Setcolorr (float colorr) {This.colorr = Colorr;} public float getcolorg () {return colorg;} public void setcolorg (float colorg) {this.colorg = colorg;} public float Getcolorb () {return colorb;} Public VOID Setcolorb (float colorb) {this.colorb = Colorb;} public float Getalpha () {return alpha;} public void Setalpha (float alpha) {this.alpha = Alpha;} public int getpopularity () {return popularity;} public void setpopularity (int popularity) {this.popularity = popularity;} public int gettextsize () {return textSize;} public void settextsize (int textSize) {this.textsize = textSize;} public float GETLOC2DX () {return loc2dx;} public void Setloc2dx (float loc2dx) {loc2dx = LOC2DX;} public float Getloc2dy () {return loc2dy;} public void Setloc2dy (float loc2dy) {loc2dy = Loc2dy;} public int Getparamno () {return paramno;} public void Setparamno (int paramno) {This.paramno = Paramno;} Public String GetUrl () {return URL;} public void SetUrl (String url) {this.url = URL;}}

Iv. Call


Private Tagcloudview mtagcloudview;public void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); this.requestwindowfeature (Window.feature_no_title); GetWindow (). SetFlags ( Windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen);D Isplay Display = Getwindowmanager (). Getdefaultdisplay (); @SuppressWarnings ("deprecation") int width = Display.getwidth (); @SuppressWarnings ("deprecation") int height = Display.getheight (); List<tag> mytaglist= createtags (); mtagcloudview = new Tagcloudview (this, width, height, mytaglist); Setcontentview (Mtagcloudview); Mtagcloudview.requestfocus (); Mtagcloudview.setfocusableintouchmode (true);} Private list<tag> createtags () {list<tag> templist = new arraylist<tag> (); Templist.add (New Tag ("  Google ", 7," http://www.google.com ")); Templist.add ("Yahoo", 3, "www.yahoo.com"), Templist.add (New tag ("CNN", 4, "www.cnn.com")); Templist.add (new Tag ("MSNBC", 5, "www.msnbc.com")); Templist.add ("CNBC ", 5," www.CNBC.com ")), Templist.add (" Facebook ", 7," www.facebook.com "), Templist.add (" Youtube ", 3," www.youtube.com ") Templist.add (New tag (" Blogspot ", 5," www.blogspot.com ")), Templist.add (" Bing ", 3," Www.bing.com ") Templist.add (" Wikipedia ", 8," www.wikipedia.com ") Templist.add (" Twitter "," 5 "," New Tag ") Www.twitter.com ") Templist.add (" Msn ", 1," www.msn.com "), Templist.add (" Amazon ", 3," www.amazon.com "); ); Templist.add (New Tag ("Ebay", 7, "www.ebay.com")), Templist.add ("LinkedIn", 5, "www.linkedin.com"); Templist.add (New tag ("Live", 7, "www.live.com")), Templist.add ("Microsoft", 3, "www.microsoft.com"); Templist.add (New tag ("Flicker", 1, "www.flicker.com")), Templist.add ("Apple", 5, "www.apple.com"); Templist.add (New tag ("Paypal", 5, "www.paypal.com")), Templist.add ("Craigslist", 7, "www.craigslist.com"); Templist.add ("Imdb", 2, "www.imdb.com"), Templist.add (New tag ("Ask", 4, "www.ask.com")); Templist.add(New tag ("Weibo", 1, "www.weibo.com"), Templist.add ("New Tag" ("tagin!", 8, "Http://scyp.idrc.ocad.ca/projects/tagin")) Templist.add (New tag ("Shiftehfar", 8, "www.shiftehfar.org")), Templist.add (New tag ("Soso", 5, "www.google.com")); Templist.add ("XVideos", 3, "www.xvideos.com"), Templist.add (New tag ("BBC", 5, "www.bbc.co.uk"); return Templist;}

Source code Click to open the link

Android 3d Cloud Tag

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.