canvas教程

Android实战简易教程第六十九枪(自定义控件实现雪花飘落效果)

字号+ 作者:H5之家 来源:H5之家 2015-11-04 12:13 我要评论( )

现在APP要求越来越高了,不只是要求实现功能,颜值的要求也越来越高,下面我们通过自定义控件来实现雪花飘落的效果,可以作为界面背景哦。1 自定义控件:package

  • > 移动开发 > Android > 正文
  • 编程大巴
  • Android实战简易教程第六十九枪(自定义控件实现雪花飘落效果)

    作者:  发布日期:2015-10-29 21:00:03

    Tag标签:控件  简易  雪花  

  • 现在APP要求越来越高了,不只是要求实现功能,颜值的要求也越来越高,下面我们通过自定义控件来实现雪花飘落的效果,可以作为界面背景哦。

    1.自定义控件:

     

    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片雪花 private Integer[] offsetX ; private 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; this.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); } @Override protected 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); canvas.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 < - 20) { 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 - 80) + 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(105) + 1; this.g = offsetY[r.nextInt(4)]; } public MyFlower(){ super(); init(); } } }
    2.布局:

     

     

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="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; // 屏幕宽度 public static int screenWidth; // 屏幕高度 public static int screenHeight; Timer myTimer = null; TimerTask mTask = null; private static final int SNOW_BLOCK = 1; private Handler mHandler = new Handler() { public void dispatchMessage(Message msg) { mFlowerView.inva(); }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mFlowerView = (FlowerView) findViewById(R.id.flowerview); screenWidth = getWindow().getWindowManager().getDefaultDisplay() .getWidth();//获取屏幕宽度 screenHeight = getWindow().getWindowManager().getDefaultDisplay() .getHeight();//获取屏幕高度 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() { @Override public void run() { Message msg = new Message(); msg.what = SNOW_BLOCK; mHandler.sendMessage(msg); } }; myTimer.schedule(mTask, 3000, 10); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mFlowerView.recly(); } }
    运行实例如下:

     

    \

    喜欢的朋友请关注我!

     

  • 延伸阅读:

    返回到首页 返回到编程大巴

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • html5的canvas实现3d雪花飘舞效果

      html5的canvas实现3d雪花飘舞效果

      2016-02-06 21:33

    • DBGrid的实用技巧

      DBGrid的实用技巧

      2016-01-21 16:04

    • Canvas + JS 实现简易的时钟

      Canvas + JS 实现简易的时钟

      2016-01-21 10:03

    • 了解TabFolder、Canvas和其他多种控件

      了解TabFolder、Canvas和其他多种控件

      2016-01-19 08:06

    网友点评