500字范文,内容丰富有趣,生活中的好帮手!
500字范文 > Android实现雪花飘落特效

Android实现雪花飘落特效

时间:2021-06-08 13:08:41

相关推荐

Android实现雪花飘落特效

圣诞节各大电商平台app都会有节日的气息,越是优秀的app越会让你感觉到它是一个活生生的app,是有一个强大的运营团队营造的富有生气的app,就像淘宝,圣诞节当天会把app主题更换成圣诞主题一样,图标都换成圣诞节特有的一些物品,而且app界面上也是飘起了雪花,很漂亮,所以在此我也写了一下雪花飘落的demo,不废话了先上图看效果吧。

截图效果不咋好,大家可以下载文章末的demo试试。

自定义SnowView

雪花是通过画笔绘制上去的,然后关键在于onDraw(),实时绘制,改变雪花的x,y坐标实现。

@Overridepublic void onDraw(Canvas canvas) {super.onDraw(canvas);for (int x = 0; x < snowNum; x += 1) {if (snows[x].y >= view_height) {snows[x].y = 0;}// 雪花下落的速度snows[x].y += 5;// 雪花飘动的效果if (random.nextBoolean()) {// 随机产生一个数字,让雪花有水平移动的效果int ran = random.nextInt(10);snows[x].x += 2 - ran;if(snows[x].x < 0){snows[x].x = view_width;}else if(snows[x].x > view_width){snows[x].x = 0;}}Resources mResources = getResources();canvas.drawBitmap(((BitmapDrawable) mResources.getDrawable(R.drawable.snow0)).getBitmap(), ((float) snows[x].x),((float) snows[x].y), mPaint);}}

雪花初始位置是随机生成的,由于初始值应该让雪花从上往下飘落,所以y轴的随机我们应该都设为负值。

/*** 随机的生成雪花的位置* */public void addRandomSnow(int number) {snowNum = number;for (int i = 0; i < number; i++) {snows[i] = new Coordinate(random.nextInt(view_width), -random.nextInt(view_height));}}/* 内嵌坐标对象 */private class Coordinate {public int x;public int y;public Coordinate(int newX, int newY) {x = newX;y = newY;}}

使用方法

雪花数量可以自设

package com.maxi.snowtest;import java.util.Random;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.DisplayMetrics;import com.maxi.snowdemo.R;import com.maxi.snowtest.widget.SnowView;@SuppressLint("HandlerLeak")public class MainActivity extends Activity {private int SNOWNUMBER = 60; //雪花数量SnowView snow = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获得雪花视图,并加载雪花图片到内存snow = (SnowView) findViewById(R.id.snow);// 获取当前屏幕的高和宽DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);snow.SetView(dm.heightPixels, dm.widthPixels);// 更新当前雪花update();}/** 负责做界面更新工作 ,实现下雪*/private RefreshHandler mRedrawHandler = new RefreshHandler();class RefreshHandler extends Handler {@Overridepublic void handleMessage(Message msg) {//snow.addRandomSnow();snow.invalidate();sleep(100);}public void sleep(long delayMillis) {this.removeMessages(0);sendMessageDelayed(obtainMessage(0), delayMillis);}};/*** Handles the basic update loop, checking to see if we are in the running* state, determining if a move should be made, updating the snake's* location.*/// 随即生成器private static final Random RNG = new Random();public void update() {//int num = RNG.nextInt(80) == 0?50:RNG.nextInt(80);//snow.addRandomSnow(num);snow.addRandomSnow(SNOWNUMBER);mRedrawHandler.sleep(600);}}

界面布局:

<RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#686868"tools:context="${relativePackage}.${activityClass}" ><com.maxi.snowtest.widget.SnowViewandroid:id="@+id/snow"android:layout_width="match_parent"android:layout_height="match_parent"/></RelativeLayout>

代码比较简单不做详述,demo中关键地方都有注释,现把demo贴出:demo下载地址

最近发现一个更棒的实现效果(大家可以去看看):点击这里

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。