简体   繁体   English

Android动态壁纸-为什么这么慢-如何提高速度?

[英]Android Live Wallpaper - why so slow - how to improve speed?

I'm attempting to create my first Live Wallpaper. 我正在尝试创建我的第一个动态壁纸。 It works and everything would be just fine except of the speed problem. 它可以工作,除了速度问题之外,其他一切都很好。 It slows down desktop - widgets and icons are scrolling noticeably slower than when using pro live wallpapers (I'm testing it on Samsung note so there shouldn't be a speed problem). 它降低了桌面的速度-与使用专业动态壁纸时相比,小部件和图标的滚动速度明显慢(我正在Samsung笔记本上对其进行测试,因此不应该存在速度问题)。 I start thinking that I do it all wrong way- so take a look at my code please: 我开始以为我做错了所有方法,所以请看一下我的代码:

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);
    }
}    

Basically its a bitmap at background (850x480jpg 70kb), that scrolls slower, bitmap at front (350x400 transparent gif 100 kb) that scrolls faster (to achieve parallax effect) and some smaller png bitmaps (3x 10-20 kb 50x50) that are rotating and moving at the foreground (lights and space ships). 基本上是背景位图(850x480jpg 70kb),滚动速度较慢;正面位图(350x400透明gif 100 kb),滚动速度更快(以实现视差效果),以​​及一些较小的png位图(3x 10-20 kb 50x50)并在前景移动(灯光和太空飞船)。

My question is - am I missing the point - I'm animating it by refreshing draw() function with a delay set to 10. (that's the only way I know - should I animate it some other way? Are the bitmaps too big? Or why is it so slow? Should I use sprites to animate front small effects to make it faster? Are there other ways of animating life wallpaper? I went through many tutorials and I've come so far but now I have to ask for help. 我的问题是-我是否错过了要点-我正在通过将draw()函数的延迟设置为10来进行动画处理(这是我知道的唯一方法-我应该以其他方式对其进行动画处理吗?位图太大吗?还是为什么这么慢?我应该使用精灵为前面的小效果制作动画以使其更快吗?是否有其他方法可以为动态壁纸设置动画?我经历了很多教程,但到目前为止,我不得不寻求帮助。

I'm not sure it is the complete answer... But it is worth attempting this. 我不确定这是否是完整的答案...但是值得尝试一下。 Rather than referencing the bitmap using Bitmap.decodeResources() method, try this: 而不是使用Bitmap.decodeResources()方法引用位图,请尝试以下操作:

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

I've been building parallax wallpapers for a while and have found that the slowness normally is due to how many bitmaps are drawn on the screen. 我已经构建视差墙纸了一段时间了,发现缓慢通常是由于在屏幕上绘制了多少位图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM