在动态壁纸的画布上旋转和翻译位图是滞后的

时间:2014-06-13 02:46:00

标签: android canvas bitmap live-wallpaper lag

请帮我解决这个滞后的问题。也许通过找到我如何将我的位图绘制到画布上的缺陷,帮助我的初学者动态壁纸更有效率。我的image1是png,我不确定图像类型是否重要,但我把它做得很小。我对这一切都很陌生,所以帮助我优化这些代码,这样我可以添加更多的壁纸将非常有帮助。谢谢。

package com.shamu11.abstractneonlivewallpaper;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;

public class LiveWallpaperService extends WallpaperService 
{
            int x,y,angle;
            float dx = 0.0f; 

            private float deviceWidth;
            private float deviceHeight;
            private float imageWidth;
            private float xxOffset;

            public void onCreate()
            {
                    super.onCreate();
            }

            public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height)
            {
                deviceWidth = width;
                deviceHeight = height;
            }

            public void onDestroy() 
            {
                    super.onDestroy();
            }

            public Engine onCreateEngine() 
            {
                    return new MyWallpaperEngine();
            }

            class MyWallpaperEngine extends Engine 
            {

                    private final Handler handler = new Handler();
                    private final Runnable drawRunner = new Runnable() {
                        @Override
                        public void run() {
                            draw();
                        }
                    };
                    private boolean visible = true;
                    public Bitmap image1,backgroundImage;

                    MyWallpaperEngine() 
                    {
                             // get the fish and background image references
                            image1 = BitmapFactory.decodeResource(getResources(),R.drawable.matt2);
                            backgroundImage = BitmapFactory.decodeResource(getResources(),R.drawable.blackholeswide3);
                            x=-50; // initialize x position 
                            y=400;  // initialize y position 

                    }


                    public void onCreate(SurfaceHolder surfaceHolder)
                    {
                            super.onCreate(surfaceHolder);
                    }

                    @Override
                    public void onVisibilityChanged(boolean visible)
                    {
                            this.visible = visible;
                            // if screen wallpaper is visible then draw the image otherwise do not draw
                            if (visible) 
                            {
                                handler.post(drawRunner);
                            }
                            else 
                            {
                                handler.removeCallbacks(drawRunner);
                            }
                    }

                    @Override
                    public void onSurfaceDestroyed(SurfaceHolder holder)
                    {
                            super.onSurfaceDestroyed(holder);
                            this.visible = false;
                            handler.removeCallbacks(drawRunner);
                    }

                    public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) 
                    {
                            draw();
                            dx = (deviceWidth - image1.getWidth()) * (1 - (xOffset * 2));
                    }

                    void draw() 
                    {
                            final SurfaceHolder holder = getSurfaceHolder();

                            Canvas c = null;
                            try 
                            {
                                    c = holder.lockCanvas();
                                    // clear the canvas
                                    c.drawColor(Color.BLACK);
                                    if (c != null)
                                    {;
                                            c.save();   
                                            c.translate(-dx, 0);
                                            c.drawBitmap(backgroundImage, -2400, 0, null);
                                            c.rotate(angle, x + (image1.getWidth()/2), y + (image1.getHeight()/2));
                                            c.drawBitmap(image1, x, y, null);
                                            // get the width of canvas
                                            int width=c.getWidth();

                                            // if x crosses the width means  x has reached to right edge
                                            if(x>width+100)
                                            {   
                                                    // assign initial value to start with
                                                    x=-400;
                                            }
                                            x=x+5; // change the x position/value by 5 pixels
                                            angle=angle+1;
                                            c.restore();
                                    }
                             }
                            finally 
                            {
                                    if (c != null)
                                           holder.unlockCanvasAndPost(c);
                            }

                            handler.removeCallbacks(drawRunner);
                            if (visible) 
                            {
                                      handler.postDelayed(drawRunner, 10); // delay 10 mileseconds
                            }    

                    }
            }
 }

0 个答案:

没有答案