Android Learning Tutorials High imitation Android micro-letter 6.0 (2) _android

Source: Internet
Author: User
Tags drawtext

Examples of this article for you to share the Android imitation Android micro-letter 6.0 of the specific code for your reference, the specific contents are as follows

Code for Wechat6activity.java:

Package Siso.geekworld;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import android.support.v4.app.FragmentActivity;
Import Android.support.v4.app.FragmentPagerAdapter;
Import Android.support.v4.view.ViewPager;
Import Android.view.Menu;
Import Android.view.View;
Import android.view.ViewConfiguration;

Import Android.view.Window;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import java.util.ArrayList;

Import java.util.List; Import Viewhelper.
Changecoloriconwithtext; Import Viewhelper.

Tabfragment; public class Wechat6activity extends fragmentactivity implements View.onclicklistener,viewpager.onpagechangelistener

 {Private Viewpager Viewpager;

 Private list<fragment> mtabs = new arraylist<> ();

 Private string[] Mtitles = new string[]{"A-Fragment", "Second Fragment", "Third Fragment", "Fourth Fragment"};

 Private Fragmentpageradapter adapter; Private List<changecoloriconwithtext> Mtabindicators = new arraylist<> ();
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.LAYOUT.ACTIVITY_WECHAT6);
  Setoverflowbuttonalways ();

  Getactionbar (). setdisplayshowhomeenabled (false);
  Initview ();
  Initdatas ();
  Initevents ();
 Viewpager.setadapter (adapter);
 }//Initialize all events private void initevents () {Viewpager.addonpagechangelistener (this);
  }//Initializes all data private void Initdatas () {for (String mtitle:mtitles) {tabfragment tabfragment = new Tabfragment ();
  Bundle Bundle = new Bundle ();
  Bundle.putstring (Tabfragment.title,mtitle);
  Tabfragment.setarguments (bundle);
  Mtabs.add (tabfragment); } adapter = new Fragmentpageradapter (Getsupportfragmentmanager ()) {@Override public android.support.v4.app.Fragmen
  T GetItem (int position) {return mtabs.get (position);
  @Override public int GetCount () {return mtabs.size ();
 }
  }; //Initialize all view private void Initview () {Viewpager= (Viewpager) Findviewbyid (R.id.id_viewpager);
  Changecoloriconwithtext one = (changecoloriconwithtext) Findviewbyid (R.id.id_indicator_one);
  Changecoloriconwithtext two = (changecoloriconwithtext) Findviewbyid (r.id.id_indicator_two);
  Changecoloriconwithtext three = (changecoloriconwithtext) Findviewbyid (R.id.id_indicator_three);
  Changecoloriconwithtext four = (changecoloriconwithtext) Findviewbyid (R.id.id_indicator_four);
  Mtabindicators.add (one);
  Mtabindicators.add (two);
  Mtabindicators.add (three);
  Mtabindicators.add (four);
  One.setonclicklistener (this);
  Two.setonclicklistener (this);
  Three.setonclicklistener (this);
  Four.setonclicklistener (this);
 One.seticonalpha (1.0f);
  @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu);
 return true; //Apply reflection to change Overflowbutton icon private void Setoverflowbuttonalways () {try {viewconfiguration config = Viewconfigura
  Tion.get (this); Field Menukey = viewconfiguration.clAss.getdeclaredfield ("Shaspermanentmenukey");
  Menukey.setaccessible (TRUE);
  Menukey.setboolean (config, false);
  catch (Exception e) {e.printstacktrace (); }//Set Menu Display icon @Override public boolean onmenuopened (int featureid, menu menu) {if featureid== window.feature  _action_bar&&menu!=null) {if (Menu.getclass (). Getsimplename (). Equals ("Menubuilder")) {try {method method =
   Menu.getclass (). Getdeclaredmethod ("setoptionaliconsvisible", Boolean.type);
   Method.setaccessible (TRUE);
   Method.invoke (menu,true);
   catch (Exception e) {e.printstacktrace ();
 }} return Super.onmenuopened (Featureid, menu);
  @Override public void OnClick (View v) {resetothertabs ();
   Switch (V.getid ()) {case R.id.id_indicator_one:mtabindicators.get (0). Seticonalpha (1.0f);
   Viewpager.setcurrentitem (0,false);
  Break
   Case R.id.id_indicator_two:mtabindicators.get (1). Seticonalpha (1.0f);
   Viewpager.setcurrentitem (1, false);
  Break Case R . Id.id_indicator_three:mTabIndicators.get (2). Seticonalpha (1.0f);
   Viewpager.setcurrentitem (2, false);
  Break
   Case R.id.id_indicator_four:mtabindicators.get (3). Seticonalpha (1.0f);
   Viewpager.setcurrentitem (3, false);
  Break } private void Resetothertabs () {for (int i=0;i<mtabindicators.size (); i++) {Mtabindicators.get (i). Seticonalpha
  (0); @Override public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {if (position
  offset>0) {Changecoloriconwithtext left = Mtabindicators.get (position);
  Changecoloriconwithtext right = Mtabindicators.get (position+1);
  Left.seticonalpha (1-positionoffset);
  Right.seticonalpha (Positionoffset); @Override public void onpageselected (int position) {} @Override public void onpagescrollstatechanged (int sta

 TE) {}}

Changecoloriconwithtext.java Code:

Package viewhelper;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Bitmap;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.graphics.PorterDuff;
Import Android.graphics.PorterDuffXfermode;
Import Android.graphics.Rect;
Import android.graphics.drawable.BitmapDrawable;
Import Android.os.Bundle;
Import Android.os.Looper;
Import android.os.Parcelable;
Import Android.util.AttributeSet;
Import Android.util.TypedValue;

Import Android.view.View;


Import SISO.GEEKWORLD.R;
 public class Changecoloriconwithtext extends View {private int mcolor = 0xff45c01a;
 Private String Mtext = "micro-letter";
 Private Bitmap Miconbitmap; private int mtextsize = (int) typedvalue.applydimension (typedvalue.complex_unit_sp,12, Getresources ().

 Getdisplaymetrics ());
 Private Canvas Mcanvas;
 Private Bitmap Mbitmap;
 Private Paint Mpaint;
 Transparency 0.0~1.0 private float malpha;
 Private Rect Miconrect;

 Private Rect Mtextbound; Private PaintMtextpaint;
 private static final String Instance_status = "Instance_status";

 private static final String Instance_alpha = "Instance_alpha";
 Public Changecoloriconwithtext {This (context, NULL);
 Public Changecoloriconwithtext (context, AttributeSet attrs) {This (context, attrs, 0);  /** * Gets the value of the custom attribute * @param context * @param attrs * @param defstyleattr/Public Changecoloriconwithtext

 Context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);

 TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.changecoloriconwithtext);

 int n = a.getindexcount ();
  for (int i=0;i<n;i++) {int attr = A.getindex (i); Switch (attr) {case R.styleable.changecoloriconwithtext_micon:bitmapdrawable drawable = (bitmapdrawable) a.getDrawabl
   E (attr);
   Miconbitmap = Drawable.getbitmap ();

  Break
   Case R.styleable.changecoloriconwithtext_mcolor:mcolor = A.getcolor (attr,0xff45c01a);

  Break Case R.Styleable.
   Changecoloriconwithtext_text:mtext = a.getstring (attr);

  Break Case r.styleable.changecoloriconwithtext_text_size:mtextsize = (int) a.getdimension (attr,typedvalue.applydimension
   (typedvalue.complex_unit_sp,12, Getresources (). Getdisplaymetrics ()));

  Break

 } a.recycle ();
 Mtextbound = new Rect ();
 Mtextpaint = new Paint ();
 Mtextpaint.settextsize (mtextsize);

 Mtextpaint.setcolor (0xff555555);
 Mtextpaint.gettextbounds (mtext, 0, Mtext.length (), mtextbound); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeasurespec
 , Heightmeasurespec); int iconwidth = Math.min (Getmeasuredwidth ()-Getpaddingleft ()-Getpaddingright (), Getmeasuredheight ()-GetPaddingTop (

 )-Getpaddingbottom ()-mtextbound.height ());
 int left = Getmeasuredwidth ()/2-iconwidth/2;

 int top = Getmeasuredheight ()/2-(Mtextbound.height () +iconwidth)/2;

 Miconrect = new Rect (left,top,left+iconwidth,top+iconwidth); } @OvErride protected void OnDraw (Canvas Canvas) {canvas.drawbitmap (Miconbitmap, NULL, miconrect, NULL);
 int alpha = (int) math.ceil (255*malpha);

 Memory ready Bitmap,setalpha, solid color, Xfermode, Icon Setuptargetbitmap (Alpha);
 1. Draw the original text 2. Draw the discolored text Drawsourcetext (Canvas,alpha);

 Drawtargettext (Canvas,alpha);

 Canvas.drawbitmap (mbitmap, 0, 0, NULL);
 }//Draw discolored text private void Drawtargettext (Canvas Canvas, int alpha) {mtextpaint.setcolor (mcolor);
 Mtextpaint.setalpha (Alpha);
 int x = Miconrect.left+miconrect.width ()/2-mtextbound.width ()/2;
 int y = miconrect.bottom+mtextbound.height ();

 Canvas.drawtext (Mtext, x, Y, mtextpaint);
 //Draw the original text private void Drawsourcetext (Canvas Canvas, int alpha) {mtextpaint.setcolor (0xff333333);
 Mtextpaint.setalpha (255-alpha);
 int x = Miconrect.left+miconrect.width ()/2-mtextbound.width ()/2;
 int y = miconrect.bottom+mtextbound.height ();
 Canvas.drawtext (Mtext, x, Y, mtextpaint); ////In memory draw the color icon private void setuptargetbitmap (int alpha) {Mbitmap = BiTmap.createbitmap (Getmeasuredwidth (), Getmeasuredheight (), Bitmap.Config.ARGB_8888);
 Mcanvas = new Canvas (MBITMAP);
 Mpaint = new Paint ();
 Mpaint.setcolor (Mcolor);
 Anti-aliasing Mpaint.setantialias (true);
 Jitter processing Mpaint.setdither (TRUE);
 Mpaint.setalpha (Alpha);
 Mcanvas.drawrect (Miconrect, mpaint);
 Mpaint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_IN));
 Mpaint.setalpha (255);
 Mcanvas.drawbitmap (Miconbitmap, NULL, Miconrect, mpaint);
 public void Seticonalpha (float alpha) {this.malpha = Alpha;
 Redraw Invalidateview ();
 private void Invalidateview () {if (Looper.getmainlooper () ==looper.mylooper ()) {//Is UI thread invalidate ();
 }else{postinvalidate ();
 } @Override protected Parcelable onsaveinstancestate () {Bundle Bundle = new Bundle ();
 Bundle.putparcelable (Instance_status,super.onsaveinstancestate ());
 Bundle.putfloat (Instance_alpha,malpha);
 return bundle;
} @Override protected void Onrestoreinstancestate (parcelable state) {if (state instanceof Bundle) {  Bundle Bundle = (Bundle) state;
  Malpha = Bundle.getfloat (Instance_alpha);
 Super.onrestoreinstancestate (Bundle.getparcelable (instance_status));
 }else{super.onrestoreinstancestate (state);

 }

 }
}

Tabfragment.java Code:

Package viewhelper;

Import Android.graphics.Color;
Import Android.os.Bundle;
Import android.view.Gravity;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.TextView;

public class Tabfragment extends Android.support.v4.app.Fragment {

 private String mtitle = "DEFAULT";

 public static final String title = "title";

 @Override public
 View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {

 if (getarguments ()!=null) {
  mtitle = getarguments (). getString (TITLE);

 TextView TV = new TextView (getactivity ());
 Tv.settext (mtitle);
 Tv.settextsize (a);
 Tv.setgravity (gravity.center);
 Tv.setbackgroundcolor (Color.parsecolor ("#ffffffff"));
 return TV;
 }
}

Activity_wechat6.xml content:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:hymen= "http:// Schemas.android.com/apk/res-auto "android:orientation=" vertical "android:layout_width=" Match_parent "Android: layout_height= "Match_parent" > <android.support.v4.view.viewpager android:id= "@+id/id_viewpager" Android: Layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "1" > </ android.support.v4.view.viewpager> <linearlayout android:orientation= "Horizontal" android:background= "@ DRAWABLE/TAB_BG "android:layout_width=" "Match_parent" android:layout_height= "60DP" > <viewhelper. Changecoloriconwithtext android:id= "@+id/id_indicator_one" android:layout_width= "0DP" android:layout_height= " Match_parent "android:layout_weight=" 1 "android:padding=" 5DP "hymen:micon=" @drawable/ic_menu_start_conversation "H Ymen:mcolor= "#ff45c01a" hymen:text_size= "12SP" hymen:text= "@string/app_name"/> <viewhelper. ChangecoloriconwIthtext android:id= "@+id/id_indicator_two" android:layout_width= "0DP" android:layout_height= "Match_parent" Android : layout_weight= "1" android:padding= "5DP" hymen:micon= "@drawable/ic_menu_friendslist" hymen:mcolor= "#ff45c01a" Hyme N:text_size= "12SP" hymen:text= "@string/tab_contact"/> <viewhelper. Changecoloriconwithtext android:id= "@+id/id_indicator_three" android:layout_width= "0DP" android:layout_height= " Match_parent "android:layout_weight=" 1 "android:padding=" 5DP "hymen:mcolor=" #ff45c01a "hymen:micon=" @drawable/ic_m Enu_emoticons "hymen:text=" @string/tab_found "hymen:text_size=" 12sp "/> <viewhelper. Changecoloriconwithtext android:id= "@+id/id_indicator_four" android:layout_width= "0DP" android:layout_height= " Match_parent "android:layout_weight=" 1 "android:padding=" 5DP "hymen:mcolor=" #ff45c01a "hymen:micon=" @drawable/ic_m Enu_allfriends "hymen:text=" @string/tab_me "hymen:text_size=" 12sp "/> </LinearLayout> &Lt;/linearlayout>

 

Strings.xml content:

<resources>
 <string name= "app_name" > Micro-letter </string>
 <string name= "Action_search" > Find </string>
 <string name= "Action_add" > Add </string>
 <string name= "Menu_group_chat" > Launch Group chat </string>
 <string name= "Menu_feedback" > Feedback </string>
 <string name= "Menu_" Addfriend "> Add friends </string>
 <string name=" Menu_scan "> Sweep </string>
 <string name=" tab _contact "> Address Book </string>
 <string name=" Tab_found "> Discovery </string>
 <string name=" Tab_ Me "> I </string>

</resources>

Main.xml content:

<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 >

 <item
 android:id=" @+id/action_search "
 android:icon=" @drawable/actionbar_ Search_icon "
 android:actionviewclass=" Android.widget.SearchView "
 android:showasaction=" ifroom| Collapseactionview "
 android:title=" @string/action_search "/>

 <item android:id=
 " @+id/action_ Group_chat "
 android:icon=" @drawable/menu_group_chat_icon "
 android:title=" @string/menu_group_chat " >

 <item
 android:id= "@+id/action_add_friend"
 android:icon= "@drawable/menu_add_icon"
 android:title= "@string/menu_addfriend"/>

 <item
 android:id= "@+id/action_scan"
 Android:icon = "@drawable/men_scan_icon"
 android:title= "@string/menu_scan"/>

 <item android:id=
 "@+id/ Action_feedback "
 android:icon=" @drawable/menu_feedback_icon "
 android:title=" @string/menu_feedback " >
</menu>

Styles.xml content:

 <resources> <!--Base application theme.--> <style name= "Appbasetheme "Parent=" Theme.AppCompat.Light "> <!--Customize your Theme here. --> <item name= "colorprimary" > @color/colorprimary</item> <item name= "Colorprimarydark" > @color /colorprimarydark</item> <item name= "coloraccent" > @color/coloraccent</item> <item " Android:actionoverflowbuttonstyle "> @style/weixinoverflowbuttonstyle</item> </style> <style Name = "Weixinoverflowbuttonstyle" > <item name= "android:src" > @drawable/actionbar_add_icon</item> </ Style> <!--application theme. --> <style name= "Apptheme" parent= "Android:Theme.Holo.Light.DarkActionBar" > <!--<style name= " Apptheme "parent=" Appbasetheme ">--> <!--all customizations that are does specific to a particular api-level can Go here. --> </style> </resources> 

Drawable Resources:

Run the results as shown in figure:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.