文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-04-02 19:55

关注

效果图

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<SnowBean> 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 < snowBeanList.size(); i++) {
 
                    snowBeanList.get(i).setX(snowBeanList.get(i).getX() + vX);
                    snowBeanList.get(i).setY(snowBeanList.get(i).getY() + vY);
 
                    if (snowBeanList.get(i).getX() < 0 || snowBeanList.get(i).getX() > 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 < snowCount; i++) {
            SnowBean bean = new SnowBean();
            bean.setX(getRandomX());
            bean.setY(getRandomY());
            bean.setSize((float) (Math.random() * 50) + 5);
            snowBeanList.add(bean);
        }
    }
 
    private float getRandomX() {
        return (float) (Math.random() * Tools.getWindowsWidth());
    }
 
    private float getRandomY() {
        return (float) (Math.random() * Tools.getWindowsHeight());
    }
 
 
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < snowBeanList.size(); i++) {
            SnowBean bean = snowBeanList.get(i);
            paint.setTextSize(bean.getSize());
            paint.setColor(bean.getColor());
            canvas.drawText(SNOW, bean.getX(), bean.getY(), paint);
        }
    }
 
 
    private GestureDetector  detector = new GestureDetector(getContext(),new MyGestureDetector());
    private boolean isPoint = false;
    private long pointTime = 0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                pointTime = 0;
//                int pCount = event.getPointerCount();
//                if (pCount >= 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雪花效果的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯