Android实现烟花效果.docx

上传人:b****3 文档编号:27437934 上传时间:2023-07-01 格式:DOCX 页数:21 大小:116.06KB
下载 相关 举报
Android实现烟花效果.docx_第1页
第1页 / 共21页
Android实现烟花效果.docx_第2页
第2页 / 共21页
Android实现烟花效果.docx_第3页
第3页 / 共21页
Android实现烟花效果.docx_第4页
第4页 / 共21页
Android实现烟花效果.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

Android实现烟花效果.docx

《Android实现烟花效果.docx》由会员分享,可在线阅读,更多相关《Android实现烟花效果.docx(21页珍藏版)》请在冰豆网上搜索。

Android实现烟花效果.docx

Android实现烟花效果

Android实现烟花效果

HolderSurfaceView.java

packagecom.lyc;

importandroid.graphics.PixelFormat;

importandroid.view.SurfaceView;

publicclassHolderSurfaceView{

privateHolderSurfaceView(){

 

}

privateSurfaceViewmSurfaceView;

privatestaticHolderSurfaceViewmHolderSurfaceView=null;

publicstaticHolderSurfaceViewgetInstance(){

if(mHolderSurfaceView==null)

mHolderSurfaceView=newHolderSurfaceView();

returnmHolderSurfaceView;

}

 

publicvoidsetSurfaceView(SurfaceViewview){

mSurfaceView=view;

mSurfaceView.setZOrderOnTop(true);

mSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);

}

publicSurfaceViewgetSurfaceView(){

returnmSurfaceView;

}

}

DrawTopBase.java

packagecom.lyc;

importandroid.content.Context;

importandroid.graphics.Canvas;

importandroid.graphics.Paint;

importandroid.graphics.PorterDuff.Mode;

importandroid.graphics.PorterDuffXfermode;

importandroid.graphics.Rect;

importandroid.view.MotionEvent;

importandroid.view.SurfaceHolder;

importandroid.view.SurfaceHolder.Callback;

importandroid.view.SurfaceView;

importandroid.view.View;

importandroid.view.View.OnTouchListener;

publicclassDrawTopBaseimplementsRunnable,Callback,OnTouchListener{

protectedDrawStatusmStatus=DrawStatus.NoWork;

protectedSurfaceViewmSurfaceView;

protectedSurfaceHoldermSurfaceHolder;

protectedContextmContext;

protectedRectmSurfaceRect=newRect(0,0,0,0);//前两个参数是左上角的坐标,后两个参数是右下角的坐标(不是宽度和高度)

 

publicDrawTopBase(){

setSurfaceView(HolderSurfaceView.getInstance().getSurfaceView());

}

publicvoidsetSurfaceView(SurfaceViewview){

mSurfaceView=view;

mContext=mSurfaceView.getContext();

mSurfaceHolder=mSurfaceView.getHolder();

mSurfaceHolder.addCallback(this);

//mSurfaceRect.set(newRect(0,0,mSurfaceView.getWidth(),mSurfaceView.getHeight()));

mSurfaceRect.set(newRect(100,100,250,250));

set();

}

 

publicvoidset(){

setRect(mSurfaceRect);

}

protectedThreadmThread=null;

 

publicvoidbegin(){

if(mThread==null){

mThread=newThread(this);

mThread.start();

}

}

publicvoidend(){

mStatus=DrawStatus.Ending;

}

 

protectedvoiddoWork(Canvascanvas){

}

 

protectedvoidendWork(){

}

protectedPaintmPaint=newPaint();

 

protectedvoidclear(Canvascanvas){

mPaint.setXfermode(newPorterDuffXfermode(Mode.CLEAR));

canvas.drawPaint(mPaint);

mPaint.setXfermode(newPorterDuffXfermode(Mode.SRC));

}

protectedvoidclear(){

synchronized(mSurfaceHolder){

Canvascanvas=this.mSurfaceHolder.lockCanvas();

try{

clear(canvas);

}catch(Exceptione){

e.printStackTrace();

}finally{

if(canvas!

=null)

mSurfaceHolder.unlockCanvasAndPost(canvas);

}

}

}

protectedvoiddoChange(){

change();

}

 

protectedvoidchange(){

}

//protectedRectmRect=newRect(0,0,0,0);

protectedRectmRect=newRect(50,50,250,250);

 

publicvoidsetRect(Rectr){

mRect.set(r);

}

publicRectgetRect(){

returnmRect;

}

 

protectedenumDrawStatus{

NoWork,Drawing,Ending,Destroyed

};

protectedlongmBegintime;

publicvoidrun(){

mStatus=DrawStatus.Drawing;

//获取当前时间

starttime=System.currentTimeMillis();

mBegintime=System.currentTimeMillis();

//建立两次缓存

clear();

clear();

while(mStatus==DrawStatus.Drawing){

synchronized(mSurfaceHolder){

Canvascanvas=this.mSurfaceHolder.lockCanvas(getRect());

try{

if(canvas!

=null){

clear(canvas);

doWork(canvas);

}

}catch(Exceptione){

e.printStackTrace();

}finally{

if(canvas!

=null)

mSurfaceHolder.unlockCanvasAndPost(canvas);

}

doChange();

}

calculatePerframe();

}

if(mStatus!

=DrawStatus.Destroyed)

clear();

mThread=null;

endWork();

}

 

protectedlongstarttime;

//每帧时间60帧附近第一次计算前使用毫秒

privatefloatperframe=6;//6=16

privateintcount=0;

//每隔多长时间测试一次帧时间

privateintmRefreshSpeed=30;//30=12

//设定要求多长时间每帧24帧每秒

privatefloatmFPS=1000/4;

privatefloatsleep=mFPS;

//设置刷新频率

publicvoidsetFPS(intfps){

mFPS=1000/fps;

}

 

protectedvoidcalculatePerframe(){

count++;

if(count==mRefreshSpeed)//由于每帧计算会降低游戏效率。

20到50差不多

{

longtimepass=System.currentTimeMillis();

longtime=timepass-starttime;

perframe=time/mRefreshSpeed;//每帧时间

sleep=perframe>mFPS?

mFPS-(perframe-mFPS)/mRefreshSpeed:

mFPS;

starttime=timepass;

count=0;

}

try{

Thread.sleep((long)(sleep));

//Thread.sleep(1000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

//newThread(){

//publicvoidrun(){

//try{

//

//sleep(2000);

//}catch(Exceptione){

////TODO:

handleexception

//e.printStackTrace();

//}finally{

////finish();

//}

//}

//}.start();

 

}

publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,

intheight){

mSurfaceRect.set(newRect(0,0,width,height));

//mSurfaceRect.set(newRect(0,0,200,500));

set();

}

 

publicvoidsurfaceCreated(SurfaceHolderholder){

}

publicvoidsurfaceDestroyed(SurfaceHolderholder){

mStatus=DrawStatus.Destroyed;

}

publicbooleanonTouch(Viewv,MotionEventevent){

//TODOAuto-generatedmethodstub

returnfalse;

}

}

packagecom.lyc;

importjava.util.Random;

importandroid.graphics.Bitmap;

importandroid.graphics.Canvas;

importandroid.graphics.Rect;

importandroid.view.MotionEvent;

importandroid.view.SurfaceHolder;

importandroid.view.View;

publicclassDrawYHextendsDrawTopBase{

privateintm_nAppX;

privateintm_nAppY;

privateintm_centerX;

privateintm_centerY;

privateintm_mouseX;

privateintm_mouseY;

privateintm_sleepTime;

privatebooleanisError;

privatebooleanm_isPaintFinished;

booleanisRunning;

booleanisInitialized;

Threadrunner;

intpix0[];

BitmapoffImage;

//ImagedbImg;

intpixls;

intpixls2;

Randomrand;

intbits;

doublebit_px[];

doublebit_py[];

doublebit_vx[];

doublebit_vy[];

intbit_sx[];

intbit_sy[];

intbit_l[];

intbit_f[];

intbit_p[];

intbit_c[];

intbit_max=100;

//intbit_sound=2;

intru;

intrv;

@Override

publicvoidsurfaceCreated(SurfaceHolderholder){

//TODOAuto-generatedmethodstub

super.surfaceCreated(holder);

this.begin();

//stopBGMedia();

//bgMedia=newGameSoundMedia(mContext);

//bgMedia.playInMediaPlayer(R.raw.bg,null);

}

//privatevoidstopBGMedia(){

//if(bgMedia!

=null){

//try{

//bgMedia.stopAll();

//bgMedia=null;

//}catch(Exceptione){

//e.printStackTrace();

//}

//

//}

//}

 

@Override

publicvoidsurfaceDestroyed(SurfaceHolderholder){

//stopBGMedia();

super.surfaceDestroyed(holder);

}

 

@Override

publicvoidset(){

super.set();

if(this.getRect().width()>10&&this.getRect().height()>10)

init();

}

 

Draw.java//重要类

 

publicDraw(){//画烟花

super();

m_mouseX=0;

m_mouseY=0;

m_sleepTime=5;//5

isError=false;

isInitialized=false;

rand=newRandom();

bits=10000;//控制烟花数量

bit_px=newdouble[bits];

bit_py=newdouble[bits];

bit_vx=newdouble[bits];

bit_vy=newdouble[bits];

bit_sx=newint[bits];

bit_sy=newint[bits];

bit_l=newint[bits];

bit_f=newint[bits];

bit_p=newint[bits];

bit_c=newint[bits];

ru=50;

rv=50;

//init();

}

CanvasmCanvas;

privateRectdrawRect=newRect(0,0,0,0);

publicvoidinit(){

 

m_nAppX=this.getRect().width()/2;

m_nAppY=this.getRect().height()/2;

drawRect=newRect(0,0,m_nAppX,m_nAppY);

m_centerX=m_nAppX/2;

m_centerY=m_nAppY/2;

m_mouseX=m_centerX;

m_mouseY=m_centerY;

//resize(m_nAppX,m_nAppY);

pixls=m_nAppX*m_nAppY;

pixls2=pixls-m_nAppX*3;

pix0=newint[pixls];

offImage=Bitmap.createBitmap(m_nAppX,m_nAppY,

Bitmap.Config.ARGB_8888);

mCanvas=newCanvas();

mCanvas.setBitmap(offImage);

for(inti=0;i

pix0[i]=0xff000000;

for(intj=0;j

bit_f[j]=0;

isInitialized=true;

}

@Override

protectedvoiddoWork(Canvascanvas){

//TODOAuto-generatedmethodstub

super.doWork(canvas);

if(offImage!

=null)

canvas.drawBitmap(offImage,drawRect,this.getRect(),mPaint);

}

@Override

protectedvoidchange(){

super.change();

if(isInitialized){

for(intj=0;j

intk=pix0[j];

intl=pix0[j+1];

inti1=pix0[j+m_nAppX];

intj1=pix0[j+m_nAppX+1];

inti=(k&0xff0000)>>16;

intk1=((((l&0xff0000)>>16)-i)*ru>>8)+i;

i=(k&0xff00)>>8;

intl1=((((l&0xff00)>>8)-i)*ru>>8)+i;

i=k&0xff;

inti2=(((l&0xff)-i)*ru>>8)+i;

i=(i1&0xff0000)>>16;

intj2=((((j1&0xff0000)>>16)-i)*ru>>8)+i;

i=(i1&0xff00)>>8;

intk2=((((j1&0xff00)>>8)-i)*ru>>8)+i;

i=i1&0xff;

intl2=(((j1&0xff)-i)*ru>>8)+i;

inti3=((j2-k1)*rv>>8)+k1;

intj3=((k2-l1)*rv>>8)+l1;

intk3=((l2-i2)*rv>>8)+i2;

pix0[j]=i3<<16|j3<<8|k3|0x77000000;

//if(pix0[j]==0x77000000)pix0[j]=0x55000000;

}

rend();

mCanvas.drawBitmap(pix0,0,m_nAppX,0,0,m_nAppX,m_nAppY,true,mPaint);

}

}

publicvoiddot(){

dot(rand.nextInt(m_nAppX),rand.nextInt(m_nAppY));

}

publicvoiddot(intx,inty){

m_mouseX=x;

m_mouseY=y;

intk=(int)(rand.nextDouble()*256D);

intl=(int)(rand.nextDouble()*256D);

inti1=(int)(rand.nextDouble()*256D);

intj1=k<<16|l<<8|i1|0xff000000;

intk1=0;

for(intl1=0;l1

if(bit_f[l1]!

=0)

continue;

bit_px[l1]=m_mouseX;

bit_py[l1]=m_mouseY;

doubled=rand.nextDouble()*6.2800000000000002D;

doubled1=rand.nextDouble();

bit_vx[l1]=Math.sin(d)*d1;

bit_vy[l1]=Math.cos(d)*d1;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 节日庆典

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1