我正在创建我的第一个动态壁纸,其中雪将在随机位置产生并开始动画。但它滞后。 这是我的代码。
代码中没有错误但是滞后。如果我增加了雪的数量,它就会滞后。 如何解决滞后问题?
public class CybWallpaper extends WallpaperService {
@Override
public Engine onCreateEngine() {
return new Mywall();
}
public class Mywall extends Engine
{
int x[]=new int[30],y[]=new int[30];
int isr=0;
int scw,sch;
Canvas dra;
int temps[]=new int[30];
boolean isft=true;
Handler hand=new Handler();
boolean isrun;
Random ran=new Random();
Runnable iterate=new Runnable()
{
@Override
public void run() {
slower();
drawframe();
}
};
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
super.onSurfaceCreated(holder);
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
super.onSurfaceChanged(holder, format, width, height);
slower();
drawframe();
}
protected void drawframe() {
// TODO Auto-generated method stub
SurfaceHolder sh=getSurfaceHolder();
dra = null;
try {
dra = sh.lockCanvas();
if (dra != null) {
for(int i=0;i<30&&isft;i++)
{
x[i]=ran.nextInt(dra.getWidth());
y[i]=ran.nextInt(dra.getHeight());
temps[i]=30+ran.nextInt(100);
}
isft=false;
dra.drawARGB(255, 0,0, 0);
drawsnow(dra,30);
}
} finally {
if (dra != null)
sh.unlockCanvasAndPost(dra);
}
}
private void drawsnow(Canvas dra,int n) {
// TODO Auto-generated method stub
Bitmap snow=BitmapFactory.decodeResource(getResources(), R.drawable.snow80);
Bitmap te[]=new Bitmap[30];
for(int i=0;i<n;i++)
{
te[i]=Bitmap.createScaledBitmap(snow, temps[i], temps[i], false);
dra.drawBitmap(te[i], x[i]-(te[i].getWidth()/2), y[i]-(te[i].getHeight()/2), null);
//Snow to and fro
if(isr==0&&x[i]!=140)
x[i]+=1;
else
isr=1;
if(isr==1&&x[i]!=120)
x[i]-=1;
else
isr=0;
if(y[i]<dra.getHeight())
y[i]+=6;
else
{
y[i]=0;
x[i]=ran.nextInt(dra.getWidth());
temps[i]=30+ran.nextInt(100);
}
}
//Toast.makeText(getApplicationContext(), Float.toString(cx), Toast.LENGTH_SHORT).show();
}
protected void slower() {
// TODO Auto-generated method stub
hand.removeCallbacks(iterate);
if (isrun) {
hand.postDelayed(iterate, 1000/30);
}
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
super.onSurfaceDestroyed(holder);
isrun=false;
hand.removeCallbacks(iterate);
Toast.makeText(getApplicationContext(), "Destroyed", Toast.LENGTH_SHORT).show();
}
@Override
public void onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
super.onTouchEvent(event);
}
@Override
public void onVisibilityChanged(boolean visible) {
// TODO Auto-generated method stub
super.onVisibilityChanged(visible);
isrun=visible;
if(isrun)
{
slower();
drawframe();
}
else
{
hand.removeCallbacks(iterate);
}
//Toast.makeText(getApplicationContext(), "Visi", Toast.LENGTH_SHORT).show();
}
}
}
我的编码是否正确?
答案 0 :(得分:3)
Bitmap snow=BitmapFactory.decodeResource(getResources(), R.drawable.snow80);
Bitmap te[]=new Bitmap[30];
我认为你在这里做错了什么。你的代码不断解码资源==&gt;浪费了大量内存和CPU ==&gt;使你的程序变慢,而不是这样做,你应该声明一个全局“雪”变量。
答案 1 :(得分:1)
你的滞后在解码资源中。
Bitmap snow=BitmapFactory.decodeResource(getResources(), R.drawable.snow80);
Bitmap te[]=new Bitmap[30];
我会将其移至onSurfaceCreated
函数并使用成员对象。