Android Custom View, ViewGroup, composite components (1)

Source: Internet
Author: User

Custom Red dot ImageView

Redtipimageview.java

import android.content.context;import android.content.res.typedarray;import  android.graphics.canvas;import android.graphics.color;import android.graphics.paint;import  android.util.attributeset;import android.widget.imageview;import com.youapp.app.r;/** *  created by moziqi on 2015/11/12. *  Little Red Dot  */public class  redtipimageview extends imageview implements generalview {     Private int tipvisibility = 0;    private tiptype mtiptype  = tiptype.red_tip_invisible;    public redtipimageview (Context  Context)  {        this (context, null, 0);     }    public redtipimageview (context context, attributeset  ATTRS)  {       &nbsP;this (context, attrs, 0);    }    public  Redtipimageview (context context, attributeset attrs, int defstyleattr)  {         super (context, attrs, defstyleattr);         init (context, attrs);     }    @ Override    public void init (Context context)  {     }    /**     *  using configuration files to manipulate      *      *  @param  context     *  @param  attrs      */     @Override     public void  init (context context, attributeset attrs)  {         if  (attrs != null) &NBSp {            typedarray typedarray =  getcontext (). Obtainstyledattributes (Attrs, r.styleable.redtipimageview);             tipvisibility = typedarray.getint ( r.styleable.redtipimageview_redtipsvisibility, 0);             typedarray.recycle ();        }     }     @Override     protected void onmeasure (int  WIDTHMEASURESPEC, INT HEIGHTMEASURESPEC)  {         Super.onmeasure (WIDTHMEASURESPEC, HEIGHTMEASURESPEC);     }    @ Override    protected void ondraw (Canvas canvas)  {         super.ondraw (Canvas);        if  (tipvisibility == tiptype.red_tip_ Visible.getcode ()  | |  mtiptype == tiptype.red_tip_visible)  {             int width = getwidth ();             int y = 10;             paint paint = new paint ();             paint.setcolor (color.red);             paint.setantialias (False);             Paint.setdither (True);             paint.setstyle ( Paint.Style.FILL_AND_STROKE);             canvas.drAwcircle (Width - y, y, y / 2, paint);         }    }    public void settipvisibility (int  Visibility)  {        tipVisibility = visibility;         invalidate ();     }    public  void settipvisibility (Tiptype tiptype)  {         if  (tiptype == null)  {             throw new nullpointerexception ();        }         this.mTipType = tipType;         invalidate ();     }    /**     *   Red dot type      */    public enum TipType {         red_tip_invisible (0),//Do not show         red_tip_visible (1) ,//Display         red_tip_gone (2);//Do not display          private int code;        tiptype (Int code)  {            this.code = code;         }        public  Int getcode ()  {            return  code;        }    }}

Attrs.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "Redtipimageview" > & Lt;attr name= "redtipsvisibility" > <enum name= "invisible" value= "0" ></enum> <enum N    Ame= "Visible" value= "1" ></enum> <enum name= "Gone" value= "2" ></enum> </attr> </declare-styleable></resources>

Define the item composite component at the bottom

Bottomitemview.java

import android.content.context;import android.graphics.bitmap;import  android.graphics.drawable.drawable;import android.util.attributeset;import android.view.gravity; import android.view.layoutinflater;import android.view.view;import android.widget.imageview; import android.widget.linearlayout;import android.widget.textview;import com.youapp.app.r;/**  * Created by moziqi on 2015/11/12. *  Define Bottom Menu item */public  class bottomitemview extends linearlayout implements generalview {     private Context mContext;    private RedTipImageView  mredtipimageview;    private textview mtextview;    // Determine if there is a current focus     private boolean isFocusable = false;     public bottomitemview (Context context) &NBSP;{&NBsp;       this (context, null, 0);    }     public bottomitemview (context context, attributeset attrs)  {         this (context, attrs, 0);    }     public bottomitemview (Context context, attributeset attrs, int  defstyleattr)  {        super (context, attrs,  DEFSTYLEATTR); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//2 init can be used to see personal likes          init (context, attrs);         //Picture Adaptive stretching         mredtipimageview.setscaletype (ImageView.ScaleType.FIT_XY);         //Canceling event triggering          Mredtipimageview.setfocusable (false);     }    /**     *  using XML configuration Files       *     *  @param  context     */      @Override     public void init (context context)  {        mContext = context;         LayoutInflater mLayoutInflater =  (Layoutinflater)   Context.getsystemservice (context.layout_inflater_service);         view  rootview = mlayoutinflater.inflate (r.layout.view_bottom_item, this);         mRedTipImageView =  (Redtipimageview)  rootview.findviewbyid ( R.id.iv_view_bottom_item);        mtextview =  (TextView)  rootview.findviewbyid (R.id.tv_view_bottom_item);    }    /**     *  Pure Code Implementation       *     *  @param  context     *   @param  attrs     */     @Override      public void init (context context, attributeset attrs)  {         mContext = context;         SetOrientation (VERTICAL);         mredtipimageview = new  redtipimageview (mcontext, attrs);         mtextview =  new textview (Mcontext);        layoutparams  Layoutparams = new layoutparams (layoutparams.wrap_content, layoutparams.wrap_content);        &nbsP;layoutparams.gravity = gravity.center;        addview ( Mredtipimageview, layoutparams);         addview (mTextView,  Layoutparams);     }    public void setisfocusable (Boolean  isfocusable)  {        this.isFocusable =  Isfocusable;    }    public void seticon (Drawable  focusabledrawable, drawable unfocusabledrawable)  {         if  (isfocusable)  {             SetIcon (focusabledrawable);        } else {             seticon (unfocusabledrawable);         }  &nbSp; }    public void seticon (int focusableresid, int  UNFOCUSABLERESIDM)  {        if  (isfocusable)  {             seticon (FOCUSABLERESID);         } else {             seticon (UNFOCUSABLERESIDM);        }     }    public void seticon (bitmap focusablebitmap, bitmap  Unfocusablebitmap)  {        if  (isfocusable)  {             seticon (Focusablebitmap);         } else {             seticon (Unfocusablebitmap);         }    }    public  void seticon (drawable drawable)  {         Mredtipimageview.setimagedrawable (drawable);     }    public void  seticon (INT&NBSP;RESID)  {         Mredtipimageview.setimageresource (resId);     }    public void  seticon (Bitmap bitmap)  {         Mredtipimageview.setimagebitmap (bitmap);    }    /**      *  set red dot tips      *     *  @param   Tiptype     */    public void settipvisibility ( Redtipimageview.tiptype tiptype) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;mredtipimageview.settipvisibility (TipType);    }    public  Void settext (String text)  {        mtextview.settext ( text);     }    public void settext (int resId)  {         mtextview.settext (resId);     }}

View_bottom_item.xml

<?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=" wrap_content "android:orientation=" vertical " ><com.zhuoheng.chaecha.ui.redtipimageview android:id= "@+id/iv_view_bottom_item" android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:layout_gravity=" center "/><textview android:id=" @+id /tv_view_bottom_item "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_gra vity= "Center"/></linearlayout>


Android Custom View, ViewGroup, composite components (1)

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.