Android Combat Simple tutorial-69th gun (custom control for snow falling effect)

Source: Internet
Author: User

Now the app is demanding more and more, not only the requirement to realize the function, the requirement of Yan value is also more and more high, the following we through the custom control to realize the snow falling effect, can be used as the interface background OH.

1. Custom CONTROLS:

Package Com.test.a;import Java.util.random;import Android.content.context;import android.content.res.Resources; Import Android.graphics.bitmap;import Android.graphics.canvas;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.drawable.bitmapdrawable;import Android.util.AttributeSet;import Android.view.view;public class Flowerview extends View {Bitmap mflowers = null; Myflower flowers [] = new myflower[50];//50 flakes Private integer[] OffsetX;p rivate integer[] OffsetY; Random r = new Random (); Matrix M = new Matrix (); Paint p = new paint (); int MW = 480;int MH = 800;float de = 0f;public void Setwh (int pW, int pH, float de) {THIS.MW = Pw;thi S.MH = Ph;this.de = de; System.out.println ("de---->" + de); OffsetX = new integer[]{(int) (2*DE), (int) ( -2*DE), (int) ( -1*DE), 0, (int) (1*DE), ( int) (2*DE), (int) (1*DE)};offsety = new integer[]{(int) (3*DE), (int) (5*DE), (int) (5*DE), (int) (3*DE), (int) (4*DE)}; Public Flowerview (Context context) {super (context);} Public Flowerview (Context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} Public Flowerview (context context, AttributeSet Attrs) {Super (context, attrs);} @Overrideprotected void OnDraw (canvas canvas) {Super.ondraw (canvas), for (int i = 0; i < flowers.length; i++) {Myflower Rect = Flowers[i];int T = rect.t;t--;if (t <= 0) {Rect.y + = Rect.g;canvas.save (); M.reset (); M.setscale (Rect.s, RECT.S); c Anvas.setmatrix (M);p. Setalpha (RECT.A); Canvas.drawbitmap (Mflowers, Rect.x, Rect.y, p); Canvas.restore ();} rect.t = t;if (rect.y >= MH) {rect.init ();} if (rect.x >= MW | | Rect.x <-) {rect.init ();} Flowers[i] = rect;}} public void Loadflower () {Resources r = This.getcontext (). Getresources (); mflowers = ((bitmapdrawable) r.getdrawable ( R.drawable.snow)). Getbitmap ();} public void recly () {if (mflowers! = null &&!mflowers.isrecycled ()) {mflowers.recycle ()}} public void Addrect () {for (int i = 0; i < flowers.length; i++) {flowers[i] = new Myflower ();}} public void Inva () {InvalidatE ();} Class Myflower{int X;int y;float s;int a;int t;int g;public void init () {Float AA = r.nextfloat (); this.x = R.nextint (mW-8 0) + 80;this.y = 0;if (aa >= 1) {this.s = 1.1f;} else if (AA <= 0.2) {this.s = 0.4f;} ELSE{THIS.S = AA;} THIS.A = R.nextint (155) + 100;THIS.T = R.nextint (+) + 1;THIS.G = Offsety[r.nextint (4)];} Public Myflower () {super (); init ();}}}

2. Layout:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <com.test.a.flowerview        android:id= "@+id/flowerview"        android:layout_ Width= "Fill_parent"        android:layout_height= "fill_parent" >    </com.test.a.flowerview></ Linearlayout>


3.java:

Package Com.test.a;import Java.util.timer;import Java.util.timertask;import android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.util.displaymetrics;public Class Mainactivity extends activity {/** Called when the activity is first created. */private flowerview mflowerview;//Screen width public static int screenwidth;//screen height public static int screenheight; Timer MyTimer = null; TimerTask mtask = null;private static final int snow_block = 1;private Handler Mhandler = new Handler () {public void Dispa Tchmessage (Message msg) {Mflowerview.inva ();};}; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); Mflowerview = (Flowerview) Findviewbyid (r.id.flowerview); screenwidth = GetWindow (). Getwindowmanager ( ). Getdefaultdisplay (). getwidth ();//Get screen width screenheight = GetWindow (). Getwindowmanager (). Getdefaultdisplay (). GetHeight ();//Get screen height displaymetrics dis = new Displaymetrics (); GetWindowManager (). Getdefaultdisplay (). getmetrics (dis); float de = Dis.density;mflowerview.setwh (ScreenWidth, ScreenHeight, DE); Mflowerview.loadflower (); Mflowerview.addrect (); mytimer = new Timer (); mtask = new TimerTask () {@Overridepublic void Run () {message msg = new Message (); msg.what = Snow_block;mhandler.sendmessage (msg);}}; Mytimer.schedule (Mtask, 3000, 10);} @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); mflowerview.recly ();}}

Run the following example:

Like friends please pay attention to me!

SOURCE download

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Combat Simple tutorial-69th gun (custom control for snow falling effect)

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.