2012-10-09 39 views
1

我正在尝试创建我的第一个动态壁纸。它的工作原理和一切都会很好,除了速度问题。它减慢桌面 - 小部件和图标滚动显着慢于使用亲动态壁纸时(我在三星笔记上测试它,所以不应该有速度问题)。我开始思考,我都做错了way-在我的代码,所以来看看,请:Android动态壁纸 - 为什么这么慢 - 如何提高速度?

public class DemoWallpaperService extends WallpaperService { 


@Override 
public Engine onCreateEngine() { 
    return new DemoWallpaperEngine(); 

} 

private class DemoWallpaperEngine extends Engine { 
    private boolean mVisible = false; 
    private final Handler mHandler = new Handler(); 

    int x=0,y=0,a=255,i=-1, a1=255, i1=-1; 
    float r=0,rs=1; 
    float rx1=10, rxs=-1; 

    private Matrix mMatrix = new Matrix(); 
    private Matrix mMatrix1 = new Matrix(); 
    private Matrix mMatrixRotate1 = new Matrix(); 
    private Matrix mMatrixRotate2 = new Matrix(); 

    public Bitmap spaceShip = BitmapFactory.decodeResource(getResources(), R.drawable.spaceship); 
    public Bitmap background= BitmapFactory.decodeResource(getResources(), R.drawable.back2short2j); 
    public Bitmap wyspa= BitmapFactory.decodeResource(getResources(), R.drawable.wyspa22g); 
    public Bitmap ksiezyc = BitmapFactory.decodeResource(getResources(), R.drawable.ksiezyc); 
    public Bitmap reflektorfront= BitmapFactory.decodeResource(getResources(), R.drawable.reflektorwyspa); 

    private float mPixels; 
    private float mPixels1; 



    private final Runnable mUpdateDisplay = new Runnable() { 
    @Override 
    public void run() { 
     draw(); 
    }}; 



    private void draw() { 
     SurfaceHolder holder = getSurfaceHolder(); 
     Canvas c = null; 

     Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
     int wx= d.getWidth(); 
     int wy= d.getHeight(); 


     try { 
      Runtime.getRuntime().gc(); 
      c = holder.lockCanvas(); 
      c.save(); 

      if (c != null) { 

       Paint paintMoon = new Paint(); 

       if(a1<=15){ 
        i1=1; 
       } 
       else if(a1>=255){ 
        i1=-1; 
       } 
       a1+=5*i1; 
       paintMoon.setAlpha(a1); 

       c.translate((float)mPixels, 0f); 
       c.drawBitmap(background, mMatrix, null); 
       c.drawBitmap(ksiezyc, 1027*wx/480,15*wy/800, paintMoon); 

       if(rx1<=-15){ 
        rxs=1; 
       } 
       else if(rx1>=15){ 
        rxs=-1; 
       } 
       rx1+=rxs*0.7; 

       c.translate((float)mPixels1, 0f); 
       //reflektor wyspa back 
       mMatrixRotate2.setTranslate(340*wx/480,300*wy/800); 
       mMatrixRotate2.preRotate(rx1,reflektorfront.getWidth()/2,20); 
       c.drawBitmap(reflektorfront, mMatrixRotate2, null); 


       c.drawBitmap(wyspa, mMatrix1, null); 

       if(r<=-15){ 
        rs=1; 
       } 
       else if(r>=15){ 
        rs=-1; 
       } 
       r+=rs*0.5; 


       mMatrixRotate1.setTranslate(160*wx/480,380*wy/800); 

       mMatrixRotate1.preRotate(r,reflektorfront.getWidth()/2,20); 

       c.drawBitmap(reflektorfront, mMatrixRotate1, null); 


       if(x<c.getWidth()){ 
       x+=3;} 
       else{x=0;} 
       if(y<c.getHeight()){ 
       y+=3;} 
       else{y=0;} 
       Paint paint = new Paint(); 


       if(a<=5){ 
        i=1; 
       } 
       else if(a>=255){ 
        i=-1; 
       } 
       a+=10*i; 
       paint.setAlpha(a); 

       c.drawBitmap(spaceShip,x,y,paint); 


       c.restore(); 

      } 
     } finally { 
      if (c != null) 
      holder.unlockCanvasAndPost(c); 
     } 
     mHandler.removeCallbacks(mUpdateDisplay); 
     if (mVisible) { 
      mHandler.postDelayed(mUpdateDisplay, 10); 
     } 
    } 
    @Override 
    public void onOffsetsChanged(float xOffset, float yOffset, 
      float xStep, float yStep, int xPixels, int yPixels){ 

     super.onOffsetsChanged(xOffset, yOffset, xStep, yStep, xPixels, yPixels);     
     mPixels = xPixels*7/4; 

     mPixels1 = 500+xPixels; 

      draw(); 
    } 



    @Override 
    public void onVisibilityChanged(boolean visible) { 
     mVisible = visible; 
     if (visible) { 
      draw(); 
     } else { 
      mHandler.removeCallbacks(mUpdateDisplay); 
     } 
    } 

    @Override 
     public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { 

     super.onSurfaceChanged(holder, format, width, height); 

     float w = background.getWidth(); 
     float h = background.getHeight(); 
     float s = height/(float)h; 
     float z = height/(float)h; 
     mMatrix.reset(); 
     mMatrix1.reset(); 
     mMatrixRotate1.reset(); 
     mMatrixRotate2.reset(); 

     mMatrix.setScale(s, s); 
     mMatrix1.setScale(z, z); 
     mMatrixRotate1.setScale(s, s); 

     draw(); 
     } 

    @Override 
    public void onSurfaceDestroyed(SurfaceHolder holder) { 
     super.onSurfaceDestroyed(holder); 
     mVisible = false; 
     mHandler.removeCallbacks(mUpdateDisplay); 
    } 


    @Override 
    public void onCreate(SurfaceHolder surfaceHolder) { 
     super.onCreate(surfaceHolder); 

     setTouchEventsEnabled(false); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     mVisible = false; 
     mHandler.removeCallbacks(mUpdateDisplay); 
    } 
}  

其基本上在背景(850x480jpg 70KB)的位图,即滚动速度较慢,在位图前(350x400透明的gif 100 kb),可以更快地滚动(实现视差效果)和一些更小的png位图(3x 10-20 kb 50x50),这些位图在前景(灯光和太空飞船)上旋转和移动。

我的问题是 - 我是否错过了这一点 - 我通过刷新draw()函数并延迟设置为10来动画它。(这是我知道的唯一方法 - 是否应该以其他方式制作动画?是位图太大?或者为什么它太慢?我是否应该使用精灵为前面的小效果设置动画效果以使其更快?是否有其他方法可以使生活壁纸动画?我经历了很多教程,并且我来到目前为止,但现在我已经。求人

回答

0

我不知道它是完整的答案...但它是值得尝试此而不是引用使用Bitmap.decodeResources()方法的位图,试试这个:

pic1 = Bitmap.createScaledBitmap(background, (int)background.getWidth(), (int)background.getHeight(), true); 

我一直在建立视差壁纸一段时间,并发现缓慢通常是由于在屏幕上绘制了多少位图。