热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android如何用自定义View实现雪花效果

这篇文章主要介绍了Android如何用自定义View实现雪花效果,对特效感兴趣的同学可以参考下

效果图

1.SnowView 类

package com.ilz.rocketapplication.handaccount.view;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
 
import com.ilz.rocketapplication.handaccount.R;
import com.ilz.rocketapplication.handaccount.bean.SnowBean;
import com.ilz.rocketapplication.handaccount.utils.ColorUtils;
import com.ilz.rocketapplication.handaccount.utils.Tools;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
 
public class SnowView extends RelativeLayout {
//    private final String SNOW = "❄";
//    private final String SNOW = "☀❆★❉❈❀✿❃❁";
    private final String SNOW = "❄";
    private float vX = 2.5f;//风向 >0 右边飘 <0 左边飘
    private float vY = 5f;//下落速度 <0你的雪花要往上飘呀
    private int snowCount = 50;//雪花个数
    private List snowBeanList = new ArrayList<>();
 
    private int XB = Tools.getWindowsWidth();
    private int YB = Tools.getWindowsHeight();
 
    private Paint paint = new Paint();
    private Timer timer;
 
    private boolean isStart = false;
 
    public SnowView(Context context) {
        this(context, null);
    }
 
    public SnowView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public SnowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
 
    private void initView() {
        paint.setAntiAlias(true);
        initSnowData();
    }
 
    public void start() {
        if (timer == null) {
            timer = new Timer();
        }
        isStart = true;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
 
                if (!isStart) return;
                for (int i = 0; i  XB) {
                        snowBeanList.get(i).setX(getRandomX());
                    }
                    if (snowBeanList.get(i).getY() <0 || snowBeanList.get(i).getY() > YB) {
                        snowBeanList.get(i).setY(0f);
                    }
                }
 
                postInvalidate();
            }
        }, 0, 15);
    }
 
    public void resume() {
        if (timer == null) {
            start();
        }
        isStart = true;
    }
 
    public void pause(){
        isStart = false;
    }
 
    public void destroy() {
        isStart = false;
        if (snowBeanList != null) {
            snowBeanList.clear();
        }
        invalidate();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
 
    private void initSnowData() {
        for (int i = 0; i = 2) {
//                    isPoint = true;
//                    pointTime = System.currentTimeMillis();
//                }
//                break;
//            case MotionEvent.ACTION_MOVE:
//                break;
//            case MotionEvent.ACTION_UP:
//                isPoint = false;
//                pointTime = 0;
//                break;
//        }
 
//        return super.onTouchEvent(event);
        return detector.onTouchEvent(event);
    }
 
    private class MyGestureDetector implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
 
        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }
 
        @Override
        public void onShowPress(MotionEvent e) {
 
        }
 
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }
 
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }
 
        @Override
        public void onLongPress(MotionEvent e) {
 
        }
 
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
 
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return false;
        }
 
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return false;
        }
 
        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }
    }
}

2.SnowBean

package com.ilz.rocketapplication.handaccount.bean;
 
import android.graphics.Color;
 
import com.ilz.rocketapplication.handaccount.utils.ColorUtils;
 
public class SnowBean {
    float x;
    float y;
    float size;
    int color = Color.WHITE;
 
    public float getX() {
        return x;
    }
 
    public void setX(float x) {
        this.x = x;
    }
 
    public float getY() {
        return y;
    }
 
    public void setY(float y) {
        this.y = y;
    }
 
    public float getSize() {
        return size;
    }
 
    public void setSize(float size) {
        this.size = size;
    }
 
    public int getColor() {
        return color;
    }
 
    public void setColor(int color) {
        this.color = color;
    }
}

3.Tools

/**
 * 获取屏幕的宽度
 */
public static int getWindowsWidth() {
    WindowManager wm = (WindowManager) (MyApplication.getInstance().getSystemService(Context.WINDOW_SERVICE));
    DisplayMetrics dm = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(dm);
    int mScreenWidth = dm.widthPixels;
    return mScreenWidth;
}

/**
 * 获取屏幕的高度
 */
public static int getWindowsHeight() {
    WindowManager wm = (WindowManager) (MyApplication.getInstance().getSystemService(Context.WINDOW_SERVICE));
    DisplayMetrics dm = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(dm);
    int mScreenHeigh = dm.heightPixels;
    return mScreenHeigh;
}

以上就是Android如何用自定义View实现雪花效果的详细内容,更多关于Android自定义View雪花效果的资料请关注其它相关文章!


推荐阅读
author-avatar
另df外一个自我_198
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有