Android自定義View實(shí)現(xiàn)雪花特效
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)雪花特效展示的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
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;//風(fēng)向 >0 右邊飄 <0 左邊飄 private float vY = 5f;//下落速度 <0你的雪花要往上飄呀 private int snowCount = 50;//雪花個(gè)數(shù) 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; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android集成騰訊X5實(shí)現(xiàn)文檔瀏覽功能
Android內(nèi)部沒有控件來直接顯示文檔,跳轉(zhuǎn)WPS或其他第三方文檔App體驗(yàn)性不好,使用騰訊X5內(nèi)核能很好的解決的這一問題這篇文章主要介紹了Android集成騰訊X5實(shí)現(xiàn)文檔瀏覽功能,需要的朋友可以參考下2019-10-10Android應(yīng)用程序轉(zhuǎn)到后臺(tái)并回到前臺(tái)判斷方法
這篇文章主要介紹了Android應(yīng)用程序轉(zhuǎn)到后臺(tái)并回到前臺(tái)判斷方法的相關(guān)資料,需要的朋友可以參考下2016-11-11android 實(shí)現(xiàn)按鈕浮動(dòng)在鍵盤上方的實(shí)例代碼
這篇文章主要介紹了android 實(shí)現(xiàn)按鈕浮動(dòng)在鍵盤上方,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Android仿淘寶view滑動(dòng)至屏幕頂部會(huì)一直停留在頂部的位置
這篇文章主要介紹了Android仿淘寶view滑動(dòng)至屏幕頂部會(huì)一直停留在頂部的位置的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11Android實(shí)現(xiàn)qq列表式的分類懸浮提示
工作中遇到了一個(gè)需求,讓應(yīng)用中的一個(gè)列表按照分類顯示,并且能提示當(dāng)前是在哪個(gè)分類,度娘了一番,參考了前輩們的博客后實(shí)現(xiàn)了,現(xiàn)在分享給大家,有需要的可以參考借鑒。2016-09-09使用flutter的showModalBottomSheet遇到的坑及解決
這篇文章主要介紹了使用flutter的showModalBottomSheet遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09flutter實(shí)現(xiàn)appbar下選項(xiàng)卡切換
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)appbar下選項(xiàng)卡切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07