我正在创建动态壁纸。我不知道我的代码是否消耗更多内存。
我检查了 adb logcat,它经常释放更多的内存。
这对我的代码来说正常吗?
链接到我的应用程序:http ://www.cybapps.com/temp/test.apk
这是我的代码:
public class CybWallpaper extends WallpaperService {
int noofsnows=30;
int x[]=new int[100],y[]=new int[100],speed[]=new int[100],nt=0;
int isr=0;
int scw,sch,snowsize=20;
float cx[]=new float[100],cy[]=new float[100];
Canvas dra;
int temps[]=new int[100],tempcs[]=new int[100];
boolean isft=true,isclicked=false;
Bitmap snow,bg,ts,gift;
SharedPreferences settings;
Bitmap te;
boolean isrun;
@Override
public Engine onCreateEngine() {
return new Mywall();
}
public class Mywall extends Engine
{
Handler hand=new Handler();
Random ran=new Random();
Runnable iterate=new Runnable()
{
@Override
public void run() {
slower();
drawframe();
}
};
public void update()
{
//Background image
settings=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
if(settings.getBoolean("cusimage", false))
{
File newImg=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/.cyb/livewallpaper.jpg");
if(newImg.exists())
{
bg=BitmapFactory.decodeFile("/storage/sdcard0/cyb/livewallpaper.jpg");
}
else
bg=BitmapFactory.decodeResource(getResources(), R.drawable.b1);
}
else
bg=BitmapFactory.decodeResource(getResources(), R.drawable.b1);
//Snows
noofsnows=Integer.parseInt(settings.getString("noofsnow", "30"));
for(int i=0;i<noofsnows&&isft;i++)
{
x[i]=ran.nextInt(scw);
y[i]=ran.nextInt(sch);
temps[i]=(10+ran.nextInt(snowsize));
tempcs[i]=(50+ran.nextInt(30));
speed[i]=(4+ran.nextInt(4));
cx[i]=-1;
cy[i]=-1;
}
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
super.onSurfaceCreated(holder);
snow=BitmapFactory.decodeResource(getResources(), R.drawable.s1);
gift=BitmapFactory.decodeResource(getResources(), R.drawable.g1);
isft=true;
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
super.onSurfaceChanged(holder, format, width, height);
isft=true;
slower();
drawframe();
}
protected void drawframe() {
// TODO Auto-generated method stub
SurfaceHolder sh=getSurfaceHolder();
dra = null;
try {
dra = sh.lockCanvas();
if (dra != null) {
if(isft)
{
scw=dra.getWidth();
sch=dra.getHeight();
update();
}
isft=false;
dra.drawBitmap(bg, 0, 0, null);
drawsnow(dra,noofsnows);
}
} finally {
if (dra != null)
sh.unlockCanvasAndPost(dra);
}
}
private void drawsnow(Canvas dra,int n) {
// TODO Auto-generated method stub
for(int i=0;i<n;i++)
{
te=Bitmap.createScaledBitmap(snow, temps[i], temps[i], false);
dra.drawBitmap(te, x[i]-(te.getWidth()/2), y[i]-(te.getHeight()/2), null);
//If Clicked
if(cx[i]!=-1&&cx[i]!=-1)
{
ts=Bitmap.createScaledBitmap(gift, tempcs[i], tempcs[i], false);
dra.drawBitmap(ts, cx[i]-(ts.getWidth()/2), cy[i]-(ts.getHeight()/2), null);
if(cy[i]<sch)
cy[i]+=speed[i];
else
{
cx[i]=-1;
cy[i]=-1;
}
}
//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]+=speed[i];
else
{
y[i]=0;
x[i]=ran.nextInt(dra.getWidth());
temps[i]=10+ran.nextInt(snowsize);
speed[i]=4+ran.nextInt(2);
}
}
}
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);
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
isclicked=true;
cx[nt]=event.getX();
cy[nt]=event.getY();
if(nt<=20)
nt++;
else
nt=0;
}
}
@Override
public void onVisibilityChanged(boolean visible) {
// TODO Auto-generated method stub
super.onVisibilityChanged(visible);
isrun=visible;
if(isrun)
{
isft=true;
slower();
drawframe();
}
else
{
hand.removeCallbacks(iterate);
}
}
}
}