Android Custom View_gdi Drawing _2d Drawing _canvas Drawing

Source: Internet
Author: User

A lot of things happened from 2014 to 2016, and now it's a foregone conclusion to forget about the troubles, to find something to do before, to fill the brain and not to think about it.
before, always want to do a hanging machine game, a variety of platforms and development language have been selected, from HTML5 to c,c++,c#,unity3d a variety of small demo write a lot, and finally chose the Android platform to do ....

Online read a lot of instances, about the custom view is similar to win under the SDK development form, found that the implementation of the android is very around, those instances are to inherit that what Surfaceview a lot of messy things.

In win under the development of many years accustomed to win under the thinking is not used to do so with the following code



Packagecom.example.test;Importjava.io.IOException;ImportAndroid.os.Bundle;ImportAndroid.os.Message;Importandroid.app.Activity;ImportAndroid.content.pm.ActivityInfo;ImportAndroid.view.Menu;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;ImportAndroid.widget.Button;ImportAndroid.os.Handler; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) { This. GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE _not_fullscreen); //hide the battery icon and all cosmetic parts (status bar section) This. Requestwindowfeature (Window.feature_no_title); Super. OnCreate (savedinstancestate); FinalMyView my =NewMyView ( This); Setcontentview (my); FinalHandler Handler =NewHandler () { Public voidhandlemessage (Message message) {if(Message.what = = 0x123) {my.invalidate (); } } }; Thread R=NewThread () { Public voidrun () { while(true) {handler.sendemptymessage (0x123); } } }; //thread t = new Thread (r);R.start (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
 Packagecom.example.test;Importjava.io.IOException;ImportJava.io.InputStream;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PorterDuff.Mode;ImportAndroid.graphics.Rect;ImportAndroid.graphics.RectF;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;Importandroid.content.res.Resources;ImportAndroid.content.res.Resources.Theme; Public classMyViewextendsView {PrivateBitmap bmp; PrivateBitmap bmp2; Private intOFS = 5; PrivatePaint paint;  PublicMyView (Context context) {Super(context); Resources Res= This. Getresources (); InputStream is=NULL; Try{ is= Getresources (). Getassets (). Open ("Bg.png"); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } BMP=Bitmapfactory.decodestream (IS); BMP2=Bitmapfactory.decoderesource (res, r.drawable.ic_launcher); }     Public voidDraw (canvas canvas) {Try{canvas.drawcolor (color.transparent, mode.clear); //canvas.drawpaint (null);Canvas.drawbitmap (BMP, 0, 0,NULL); Canvas.drawbitmap (BMP2, OFS,0,NULL); //Canvas.drawtext (OFS, N, +, paint);//Drawing text TextOFS++; } Catch(Exception e) {log.i (E.getmessage (). toString (),NULL); //Todo:handle Exception        }    }}

Update::

The previous idea is to take the class that inherits the view as the canvas, what is the problem? You cannot add a button control to this view canvas because the button is also integrated in the view

Change the idea

Activity Stage

Classes that inherit from view are sprites

According to this idea Baidu under the view of the properties and methods. We can do a lot of things.


Android Custom View_gdi Drawing _2d Drawing _canvas Drawing

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.