Android實現(xiàn)花瓣飄落效果的步驟
1.定義變量將變量初始化
private SurfaceHolder mHolder; private boolean mFlag = true;//繪制小花線程的開關(guān)標志 private ArrayList<PointF> mFlowers;//小花點的坐標集合 private Random mRandom;//負責(zé)隨機數(shù)生成 private Bitmap mBitmap;//小花的圖案 public FlowerView(Context context) {super(context);init(); } public FlowerView(Context context, AttributeSet attrs) {super(context, attrs);init(); } public FlowerView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(); } private void init(){mHolder = getHolder();mHolder.addCallback(this);//設(shè)置背景透明this.setZOrderOnTop(true);mHolder.setFormat(PixelFormat.TRANSLUCENT);mFlowers = new ArrayList<>();mRandom = new Random();mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_hua); }
2.實現(xiàn)添加花朵坐標點的方法
/** * 添加花朵 */ private void addFlower(){PointF point = new PointF();point.x=mRandom.nextInt(getWidth());//根據(jù)控件寬度隨機生成X軸坐標point.y=-mBitmap.getHeight();//縱坐標設(shè)置為小花圖像的負值(產(chǎn)生從屏幕外進入的效果)mFlowers.add(point);//將坐標點添加進集合 }
3.實現(xiàn)SurfaceHolder.Callback及Runnable接口
public class FlowerView extends SurfaceView implements SurfaceHolder.Callback,Runnable
4.在run方法中實現(xiàn)繪制邏輯
@Override public void run() {while (mFlag){ try {Thread.sleep(80);//控制小花的下落速度Canvas canvas = mHolder.lockCanvas();PointF pointF = null;//清屏操作(否則會殘留一些無用圖像)if(canvas!=null){ canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);}else { continue;}for(PointF point: mFlowers){ pointF = point; canvas.drawBitmap(mBitmap,pointF.x,pointF.y,null); int i = mRandom.nextInt(getHeight()/50)+getHeight()/50;//修改雨滴線的縱坐標,使其看起來在下雨 pointF.y=pointF.y+i;}mHolder.unlockCanvasAndPost(canvas);addFlower();//當繪制點的縱坐標大于控件高度時,將該點移除if(mFlowers.size()>0&&pointF!=null&&pointF.y>=getHeight()){ mFlowers.remove(pointF);} }catch (Exception e){}} }
5.在SurfaceHolder.Callback的回調(diào)方法中開啟繪制線程
@Override public void surfaceCreated(SurfaceHolder holder) {mFlag = true;//surface創(chuàng)建時將線程開關(guān)打開new Thread(this).start();//開啟線程繪制 } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {mFlowers.clear();//當控件發(fā)生改變時清除之前的繪制點 } @Override public void surfaceDestroyed(SurfaceHolder holder) {mFlag = false;//當surface銷毀時關(guān)掉繪制線程 }完整代碼展示
public class FlowerView extends SurfaceView implements SurfaceHolder.Callback,Runnable{ private SurfaceHolder mHolder; private boolean mFlag = true;//繪制小花線程的開關(guān)標志 private ArrayList<PointF> mFlowers;//小花點的坐標集合 private Random mRandom;//負責(zé)隨機數(shù)生成 private Bitmap mBitmap;//小花的圖案 public FlowerView(Context context) {super(context);init(); } public FlowerView(Context context, AttributeSet attrs) {super(context, attrs);init(); } public FlowerView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(); } private void init(){mHolder = getHolder();mHolder.addCallback(this);//設(shè)置背景透明this.setZOrderOnTop(true);mHolder.setFormat(PixelFormat.TRANSLUCENT);mFlowers = new ArrayList<>();mRandom = new Random();mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_hua); } @Override public void surfaceCreated(SurfaceHolder holder) {mFlag = true;new Thread(this).start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {mFlowers.clear(); } @Override public void surfaceDestroyed(SurfaceHolder holder) {mFlag = false; } @Override public void run() {while (mFlag){ try {Thread.sleep(80);Canvas canvas = mHolder.lockCanvas();PointF pointF = null;//清屏操作if(canvas!=null){ canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);}else { continue;}for(PointF point: mFlowers){ pointF = point; canvas.drawBitmap(mBitmap,pointF.x,pointF.y,null); int i = mRandom.nextInt(getHeight()/50)+getHeight()/50;//修改雨滴線的縱坐標,使其看起來在下雨 pointF.y=pointF.y+i;}mHolder.unlockCanvasAndPost(canvas);addFlower();if(mFlowers.size()>0&&pointF!=null&&pointF.y>=getHeight()){ mFlowers.remove(pointF);} }catch (Exception e){}} } /** * 添加花朵 */ private void addFlower(){PointF point = new PointF();point.x=mRandom.nextInt(getWidth());point.y=-mBitmap.getHeight();mFlowers.add(point); }}
以上就是Android實現(xiàn)花瓣飄落效果的步驟的詳細內(nèi)容,更多關(guān)于Android實現(xiàn)花瓣飄落效果的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Xml簡介_動力節(jié)點Java學(xué)院整理2. UDDI FAQs3. 低版本IE正常運行HTML5+CSS3網(wǎng)站的3種解決方案4. ASP常用日期格式化函數(shù) FormatDate()5. 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法6. CSS可以做的幾個令你嘆為觀止的實例分享7. JSP+Servlet實現(xiàn)文件上傳到服務(wù)器功能8. jsp+servlet實現(xiàn)猜數(shù)字游戲9. jsp文件下載功能實現(xiàn)代碼10. JSP之表單提交get和post的區(qū)別詳解及實例
