Android Custom Surfaceview for running male animations

Source: Internet
Author: User

First look at the effect:



Code implementation:

Import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.Rect; Import Android.util.attributeset;import android.view.surfaceholder;import android.view.surfaceview;/** * Run the villain * @ Author Shaoshuai * */public class Runmanview extends Surfaceview {private Surfaceholder holder;/** canvas */private Canvas MC anvas;/** Brush */private Paint mpaint;/** View width */private int mwidth;/** view height */private int mheight;/** background */private Bitmap m bg1;/** run the villain */private Bitmap mplay1;/** animation time of each frame */private int times = 50;/** animation frame of the current picture */private int mpicposition = 0;// Animation Total 10 frames, starting to perform action for 0/** */private Runnable Runnable = new Runnable () {@Overridepublic void run () {Mydraw ()}; Public Runmanview (context context, AttributeSet Attrs) {Super (context, attrs); holder = This.getholder (); Holder.addcallback (callBack); mpaint = new Paint (); Mpaint.setcolor (Color.yellow); BitmapfActory. Options Ops = new bitmapfactory.options (); mBg1 = Bitmapfactory.decoderesource (This.getresources (), R.DRAWABLE.BG1, Ops ); mPlay1 = Bitmapfactory.decoderesource (Getresources (), R.drawable.dartman, OPS);} Surfaceholder.callback Callback = new Surfaceholder.callback () {@Overridepublic void surfacecreated (Surfaceholder Holder) {} @Overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {//Get screen width height mwidth = Width;mheight = height;//Mrect = new Rect (0, 0, mwidth, mheight); new Thread (runnable). Start ();  @Overridepublic void surfacedestroyed (Surfaceholder holder) {}};p rivate void Mydraw () {int toleft = 300;//distance left width int totop = mheight-100;//distance from top height if (ToLeft > Mwidth) {toleft = 0;} while (true) {try {rect mcliprect = new Rect (ToLeft, totop, ToLeft + mplay1.getwidth ()/Ten, Totop-mplay1.getheight ()); m Canvas = Holder.lockcanvas (), if (Mcanvas! = null) {//Draw background mcanvas.drawbitmap (MBG1, NULL, new Rect (0, 0, Mwidth, mheight), M Paint); Mcanvas.save ();//Save current status MCanvas.cliprect (mcliprect);//crop area//Draw villain Mcanvas.drawbitmap (MPlay1, Toleft-mpicposition * mplay1.getwidth ()/10,toTop -Mplay1.getheight (), mpaint); Mcanvas.restore ();//Remove the saved state mpicposition++;//steps plus 1//infinite loop run if (Mpicposition > 9) { mpicposition = 0;}}} catch (Exception e) {e.printstacktrace ();} finally {if (Mcanvas! = null) {holder.unlockcanvasandpost (Mcanvas);}} try {thread.sleep (time);} catch (Interruptedexception e) {e.printstacktrace ();}}}}



The corresponding resource file:

Dartman.png




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

Android Custom Surfaceview for running male animations

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.