Loading custom view classes in Layout.xml files in Android

Source: Internet
Author: User

      <COM.BN.SUMMER.GGVIEW3      android:layout_width= "100dip"       android:layout_height= "114dip"         android: layout_marginleft= "11dip"   />



Implementation of the View class:

Package Com.bn.summer;import Android.content.context;import Android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.paint;import Android.util.attributeset;import Android.view.view;public class GGVIEW3 extends View{ int Component_width;int Component_height;boolean Initflag=false; Bitmap[] BMA; Paint paint;int[] Drawablesid;int currindex=0;boolean workflag=true;public GGView3 (Context Father,attributeset as) { Super (Father,as); This.drawablesid=new int[]{r.drawable.room1,r.drawable.room2,r.drawable.room3,};bma=new Bitmap[ Drawablesid.length];initbitmaps ();p aint=new paint ();p aint.setflags (Paint.anti_alias_flag);//anti-aliasing new Thread () { public void Run () {while (Workflag) {currindex= (currindex+1)%drawablesid.length; GGView3.this.postInvalidate (); try {thread.sleep (2500);} catch (Interruptedexception e) {e.printstacktrace ();}}}}. Start ();} public void Initbitmaps () {Resources res=this.getresources (); for (int i=0;i<drawablesid.length;i++) {Bma[i]=bitmapfactory.decoderesource (res, drawablesid[i]);}} public void OnDraw (canvas canvas) {if (!initflag) {component_width=this.getwidth ();//Gets the width of the view component_height= This.getheight ();//Get the height of view initflag=true;} int picwidth=bma[currindex].getwidth (); int picheight=bma[currindex].getheight (); int startx= (Component_ Width-picwidth)/2;int starty= (component_height-picheight)/2;//Draw background color//canvas.drawargb (255, 244, 214, 116); Canvas.drawargb (255, N, +,), Canvas.drawbitmap (Bma[currindex], startx,starty, paint);}}


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.