自定义控件之飘落动画
来源:互联网 发布:微商推广软件 编辑:程序博客网 时间:2024/06/05 15:10
不少人会遇到飘落动画的需求,在这里我大致写了一个,但是不可能满足所有需求,仍有很多地方需要修改,我直接贴代码,效果请自行黏贴到demo中尝试
public class FlowerView extends View { public int alalpha = 300; Bitmap[] flos; int[] drawables; // Bitmap mFlowers = null; MyFlower flowers[]; private Integer[] offsetX; private Integer[] offsetY; private int drawableSize = 50; /** * 定一个全局随机 */ Random newRandom = new Random(); /** * 图片处理类 */ Matrix imMatrix = new Matrix(); /** * 画笔 */ Paint p = new Paint(); /** * 画布宽度 */ int mW = 480; /** * 画布高度 */ int mH = 800; int animalTime = 10000;//默认10秒动画 private long startTime; /** * 设置动画时长 毫秒 * * @param animalTime */ public void setAnimalTime(int animalTime) { this.animalTime = animalTime; } private boolean isDrawing = false; boolean HIDE_MODE = false; private boolean fileAfter = false; //是否保留最后状态 public void setFileAfter(boolean fileAfter) { this.fileAfter = fileAfter; } //是否渐隐 public void setHIDE_MODE(boolean HIDE_MODE) { this.HIDE_MODE = HIDE_MODE; } //设置漂浮物 public void setDrawables(int[] drawables) { this.drawables = drawables; } //设置漂浮物密度 public void setDrawableSize(int drawableSize) { this.drawableSize = drawableSize; } /** * 设置画布大小 * * @param pW 宽度 * @param pH 高度 */ private void setWH(int pW, int pH) { this.mW = pW; this.mH = pH; offsetX = new Integer[]{2, -2, -1, 0, 1, 2, 1}; offsetY = new Integer[]{3, 5, 5, 3, 4}; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); setWH(w, h); init(); } public FlowerView(Context context) { this(context, null); } public FlowerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public FlowerView(Context context, AttributeSet attrs) { this(context, attrs, 0); } private void init() { if (drawables == null) drawables = new int[]{}; if (flowers == null) flowers = new MyFlower[drawableSize]; loadFlower(); addRect(); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); if (isDrawing) { if (HIDE_MODE) { alalpha--; p.setAlpha(alalpha); } for (int i = 0; i < flowers.length; i++) { MyFlower rect = flowers[i]; Bitmap bi = rect.bi; int t = rect.t; t--; if (t <= 0) { rect.y += rect.g; rect.x += rect.h; canvas.save(); imMatrix.reset(); imMatrix.setScale(rect.s, rect.s); canvas.setMatrix(imMatrix); canvas.drawBitmap(bi, rect.x, rect.y, p); canvas.restore(); } rect.t = t; if (rect.y >= mH) { rect.init(bi); } if (rect.x >= mW || rect.x < -20) { rect.init(bi); } flowers[i] = rect; } postInvalidateDelayed(10); if (HIDE_MODE) { if (alalpha <= 0) isDrawing = false; } else { if (System.currentTimeMillis() - startTime > animalTime) { isDrawing = false; } } } else { if (fileAfter) { for (int i = 0; i < flowers.length; i++) { MyFlower rect = flowers[i]; Bitmap bi = rect.bi; int t = rect.t; t--; if (t <= 0) { rect.y += rect.g; rect.x += rect.h; canvas.save(); imMatrix.reset(); imMatrix.setScale(rect.s, rect.s); canvas.setMatrix(imMatrix); canvas.drawBitmap(bi, rect.x, rect.y, p); canvas.restore(); } rect.t = t; if (rect.y >= mH) { rect.init(bi); } if (rect.x >= mW || rect.x < -20) { rect.init(bi); } flowers[i] = rect; } } } } public void loadFlower() { Resources r = this.getContext().getResources(); if (flos != null) { for (int i = 0; i < flos.length; i++) { flos[i].recycle(); } flos = null; } flos = new Bitmap[drawables.length]; for (int i = 0; i < drawables.length; i++) { flos[i] = ((BitmapDrawable) r.getDrawable(drawables[i])).getBitmap(); } } public void addRect() { for (int i = 0; i < flowers.length; i++) { flowers[i] = new MyFlower(flos[(int) (Math.random() * flos.length)]); } } /** * 开始动画 */ public void start() { if (isDrawing) { return; } alalpha = 300; startTime = System.currentTimeMillis(); isDrawing = true; postInvalidateDelayed(100); } class MyFlower { Bitmap bi; int x; int y; /** * 缩放尺寸 */ float s; /** * 透明度 */ int alpha; int t = -10; /** * y轴偏移 */ int g; /** * x轴偏移 */ int h; public void init(Bitmap bi) { float aa = newRandom.nextFloat(); this.x = newRandom.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.alpha = alalpha; this.t = -1; this.g = offsetY[newRandom.nextInt(4)]; this.h = offsetX[newRandom.nextInt(7)]; } public MyFlower(Bitmap bi) { super(); this.bi = bi; init(bi); } }}
有关初始化和调用的代码如下
private void initFlower() { flower = (FlowerView) findViewById(R.id.flower);// flower.setFileAfter(true); flower.setDrawableSize(50); flower.setDrawables(new int[]{R.drawable.sn1, R.drawable.sn2, R.drawable.sn3, R.drawable.sn4}); } public void startFlow(View v) { flower.start(); }
就会有飘落动画的出现,设置密度
flower.setDrawableSize(50);
一定要设置需要飘落的图片
flower.setDrawables(new int[]{R.drawable.sn1, R.drawable.sn2, R.drawable.sn3, R.drawable.sn4});调用start()查看效果
阅读全文
0 0
- 自定义控件之飘落动画
- Android之雪花飘落的动画特效
- 自定义控件之帧动画
- Android自定义控件之扫描动画UI
- 自定义动画控件 AnimationView
- 自定义控件动画TextView
- Android实战简易教程-第六十九枪(自定义控件实现雪花飘落效果)
- Android实战简易教程-第六十九枪(自定义控件实现雪花飘落效果)
- Swift-自定义控件之IndicatorButton(带动画的按钮)
- 安卓自定义控件之带有折叠动画的ExpandableListView
- Android学习之动画效果的实现、自定义控件皮肤
- 安卓开发之自定义粒子旋转动画加载控件
- 安卓开发之使用PathMeasure自定义加载动画控件
- android自定义控件之圆形进度条(带动画)
- 雪花飘落动画-帧动画系列
- Silverlight & Blend动画设计系列十三:三角函数(Trigonometry)动画之飘落的雪花(Falling Snow)
- 飘落
- 自定义控件 播放GIF动画
- java中 Object转换成 int 类型。
- makefile下$^,$@,$?,$<,$(@D),$(@F)定义使用详解
- NOIP模拟考试
- Unity 纹理格式
- Servlet Mapping 中/ 和 /*的区别
- 自定义控件之飘落动画
- Android 生命周期封装销毁
- asp.net core 使用session
- JSP中servlet中的路径配置问题
- Mysql 高可用方案 InnoDB Cluster
- oracle中如何判断某个日期是星期几
- java -Xms -Xmx设置java应用程序运行的内存大小,Xms Xmx PermSize MaxPermSize 区别
- iOS Protocol 协议
- 数据挖掘经典算法--朴素贝叶斯分类