时间:2021-05-20
使用SurfaceView实现简单的红包雨动画,供大家参考,具体内容如下
具体代码:SurfaceViewDemo
public class TranslateSurfaceView extends SurfaceView implements DrawInterface { private DrawHandler drawHandler; private int width; private int height; private Bitmap bitmap; private int bitmapWidth; private int bitmapHeight; private Toast toast; private int count = 0; private ArrayList<MoveModel> moveList = new ArrayList<>(); public TranslateSurfaceView(Context context) { this(context, null); } public TranslateSurfaceView(Context context, AttributeSet attrs) { this(context, attrs, -1); } public TranslateSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { SurfaceHolder holder = getHolder(); setZOrderOnTop(true); holder.setFormat(PixelFormat.TRANSLUCENT); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); width = getMeasuredWidth(); height = getMeasuredHeight(); } public void prepare() { DrawThread drawThread = new DrawThread(); drawThread.start(); drawHandler = new DrawHandler(drawThread.getLooper(), this); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_redenvelope2); bitmapWidth = bitmap.getWidth(); bitmapHeight = bitmap.getHeight(); } public void addMoveModel(MoveModel moveModel) { moveList.add(moveModel); } public void start() { count = 0; moveList.clear(); for (int i = 0; i < 10; i++) { generateModel(); } drawHandler.sendEmptyMessage(DrawHandler.START_DRAW_KEY); } public void resume() { drawHandler.sendEmptyMessage(DrawHandler.START_DRAW_KEY); } public void pause() { drawHandler.sendEmptyMessage(DrawHandler.STOP_DRAW_KEY); } public void quit() { if (null != bitmap) { bitmap.recycle(); } if (null != drawHandler) { drawHandler.removeCallbacksAndMessages(null); drawHandler.getLooper().quit(); } } @Override public void startDraw() { SurfaceHolder holder = getHolder(); Canvas canvas = holder.lockCanvas(); if (null == canvas) { return; } Paint paint = new Paint(); paint.setAntiAlias(true); canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); for (MoveModel moveModel : moveList) { canvas.drawBitmap(bitmap, moveModel.x, moveModel.y, paint); if (moveModel.x > width || moveModel.y > height) { resetMoveModel(moveModel); } else { moveModel.y += moveModel.randomY; } } holder.unlockCanvasAndPost(canvas); drawHandler.sendEmptyMessage(DrawHandler.START_DRAW_KEY); } @Override public void stopDraw() { drawHandler.removeMessages(DrawHandler.START_DRAW_KEY); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_DOWN: checkInRect((int) event.getX(), (int) event.getY()); break; } return true; } /** * 是否点击在红包区域 * @param x * @param y */ private void checkInRect(int x, int y) { int length = moveList.size(); for (int i = 0; i < length; i++) { MoveModel moveModel = moveList.get(i); Rect rect = new Rect((int) moveModel.x, (int) moveModel.y, (int) moveModel.x + bitmapWidth, (int) moveModel.y + bitmapHeight); if (rect.contains(x, y)) { count++; resetMoveModel(moveModel); if (toast == null) { toast = Toast.makeText(getContext(), "", Toast.LENGTH_SHORT); } toast.setText("抢到红包" + count); toast.show(); break; } } } private void resetMoveModel(MoveModel moveModel) { Random random = new Random(); moveModel.x = random.nextInt(11) * (width / 10); moveModel.y = 0; moveModel.randomY = (random.nextInt(5) + 2) * getResources().getDisplayMetrics().density * 1.4f; } private void generateModel() { Random random = new Random(); MoveModel moveModel = new MoveModel(); moveModel.moveId = moveList.size() + 1; moveModel.x = random.nextInt(11) * (width / 10); moveModel.y = 0; moveModel.randomY = (random.nextInt(5) + 2) * getResources().getDisplayMetrics().density * 1.4f; moveList.add(moveModel); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实现如下几个界面之间的平移动画实现分析:导航界面移动过程中,平移动画上一页移入动画(-屏幕宽度,y)------>(0,y)上一页移出动画(0,y)----
本文介绍了Android实现红包雨动画效果,分享给大家,希望对大家有帮助红包雨关于实现上面红包雨效果步骤如下:1.创建一个红包实体类publicclassRed
本文实例为大家分享了Html实现移动端红包雨功能页面的具体代码,供大家参考,具体内容如下实现效果如下:具体代码如下html部分:红包雨demo.css为初始化c
简介Android动画主要包括视图动画和属性动画,视图动画包括Tween动画和Frame动画,Tween动画又包括渐变动画、平移动画、缩放动画、旋转动画。Twe
本文实例为大家分享了js实现移动端红包雨页面的具体代码,供大家参考,具体内容如下实习的效果如下:具体代码如下html部分:红包雨demo.css为初始化css,