您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在Android中實(shí)現(xiàn)一個(gè)花瓣飄落效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
首先需要生成繪制小花的坐標(biāo)點(diǎn),坐標(biāo)點(diǎn)的橫坐標(biāo)是根據(jù)控件的寬度隨機(jī)生成的,而縱坐標(biāo)則設(shè)置為小花圖片高度的負(fù)值(這樣可以實(shí)現(xiàn)小花從屏幕外進(jìn)入)。
將這些點(diǎn)存儲(chǔ)到集合當(dāng)中。
遍歷集合根據(jù)點(diǎn)的位置繪制小花
繪制完后不斷增加各個(gè)點(diǎn)的縱坐標(biāo)
1.定義變量將變量初始化
private SurfaceHolder mHolder; private boolean mFlag = true;//繪制小花線程的開關(guān)標(biāo)志 private ArrayList<PointF> mFlowers;//小花點(diǎn)的坐標(biāo)集合 private Random mRandom;//負(fù)責(zé)隨機(jī)數(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.實(shí)現(xiàn)添加花朵坐標(biāo)點(diǎn)的方法
/** * 添加花朵 */ private void addFlower(){ PointF point = new PointF(); point.x=mRandom.nextInt(getWidth());//根據(jù)控件寬度隨機(jī)生成X軸坐標(biāo) point.y=-mBitmap.getHeight();//縱坐標(biāo)設(shè)置為小花圖像的負(fù)值(產(chǎn)生從屏幕外進(jìn)入的效果) mFlowers.add(point);//將坐標(biāo)點(diǎn)添加進(jìn)集合 }
3.實(shí)現(xiàn)SurfaceHolder.Callback及Runnable接口
public class FlowerView extends SurfaceView implements SurfaceHolder.Callback,Runnable
4.在run方法中實(shí)現(xiàn)繪制邏輯
@Override public void run() { while (mFlag){ try { Thread.sleep(80);//控制小花的下落速度 Canvas canvas = mHolder.lockCanvas(); PointF pointF = null; //清屏操作(否則會(huì)殘留一些無用圖像) 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;//修改雨滴線的縱坐標(biāo),使其看起來在下雨 pointF.y=pointF.y+i; } mHolder.unlockCanvasAndPost(canvas); addFlower(); //當(dāng)繪制點(diǎn)的縱坐標(biāo)大于控件高度時(shí),將該點(diǎn)移除 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)建時(shí)將線程開關(guān)打開 new Thread(this).start();//開啟線程繪制 } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { mFlowers.clear();//當(dāng)控件發(fā)生改變時(shí)清除之前的繪制點(diǎn) } @Override public void surfaceDestroyed(SurfaceHolder holder) { mFlag = false;//當(dāng)surface銷毀時(shí)關(guān)掉繪制線程 }
public class FlowerView extends SurfaceView implements SurfaceHolder.Callback,Runnable{ private SurfaceHolder mHolder; private boolean mFlag = true;//繪制小花線程的開關(guān)標(biāo)志 private ArrayList<PointF> mFlowers;//小花點(diǎn)的坐標(biāo)集合 private Random mRandom;//負(fù)責(zé)隨機(jī)數(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;//修改雨滴線的縱坐標(biāo),使其看起來在下雨 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); } }
關(guān)于怎么在Android中實(shí)現(xiàn)一個(gè)花瓣飄落效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。