动态壁纸使用一系列图像

时间:2012-06-25 00:03:13

标签: java android live-wallpaper image

大家好。这是我的第一个android项目。我正在尝试使用从视频中获取的一系列图像来创建动态壁纸。每张图片的尺寸为960 x 800.到目前为止,动态壁纸有效,但我仍然遇到无法解决的问题。

  1. 设置后,动态壁纸仅显示整个png的一部分。我设法让它滚动图像宽度,它滚动但它只显示整个宽度的一部分。它只显示整个图像高度的一部分,直到肖像上的图像的一半,并且只是在景观上从上到下的一部分。我希望动态壁纸看起来不错,并且能够在纵向和横向模式下适应手机的屏幕。

  2. 它有效,但它有点暂停。它只在我切换屏幕时才能继续工作。更具体地说,在这个问题上,当你设置它时会动画,但是在周期的一半时间突然停止,当你切换屏幕时它会继续工作一点点,这意味着它会在序列上稍微前进一点。并且为了完成序列并重新开始,你需要继续浏览屏幕。

  3. 我希望序列更快,就像视频一样。

  4. 这是我的代码::

    package com.livewallpaper.mw3lwp;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.os.Handler;
    import android.service.wallpaper.WallpaperService;
    import android.view.SurfaceHolder;
    
    public class ModernWarfare3LiveWallpaper extends WallpaperService {
        private final Handler handler = new Handler();
        public float mxOffset;
        public void onCreate() 
        {
            super.onCreate();
        }
    
        public void onDestroy() 
        {
            super.onDestroy();
        }
    
        public Engine onCreateEngine() 
        {
            return new CercleEngine();
        }
    
        class CercleEngine extends Engine {
    
            private final Runnable drawRunner = new Runnable(){
                @Override
                public void run() {
                    updateBG();
                    drawFrame();
    
                }
            };
    
            public Bitmap myBg;
            int bgcycle = 0;
    
            public void updateBG() {
                if(bgcycle==50){
                    bgcycle=0;
                }
    
                switch (bgcycle) {
                    case 0: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n01); break;
                    case 1: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n02); break;
                    case 2: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n03); break;
                    case 3: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n04); break;
                    case 4: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n05); break;
                    case 5: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n06); break;
                    case 6: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n07); break;
                    case 7: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n08); break;
                    case 8: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n09); break;
                    case 9: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n10); break;
                    case 10: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n11); break;
                    case 11: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n12); break;
                    case 12: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n13); break;
                    case 13: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n14); break;
                    case 14: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n15); break;
                    case 15: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n16); break;
                    case 16: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n17); break;
                    case 17: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n18); break;
                    case 18: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n19); break;
                    case 19: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n20); break;
                    case 20: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n21); break;
                    case 21: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n22); break;
                    case 22: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n23); break;
                    case 23: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n24); break;
                    case 24: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n25); break;
                    case 25: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n26); break;
                    case 26: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n27); break;
                    case 27: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n28); break;
                    case 28: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n29); break;
                    case 29: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n30); break;
                    case 30: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n31); break;
                    case 31: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n32); break;
                    case 32: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n33); break;
                    case 33: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n34); break;
                    case 34: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n35); break;
                    case 35: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n36); break;
                    case 36: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n37); break;
                    case 37: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n38); break;
                    case 38: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n39); break;
                    case 39: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n40); break;
                    case 40: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n41); break;
                    case 41: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n42); break;
                    case 42: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n43); break;
                    case 43: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n44); break;
                    case 44: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n45); break;
                    case 45: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n46); break;
                    case 46: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n47); break;
                    case 47: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n48); break;
                    case 48: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n49); break;
                    case 49: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n50); break;
                    case 50: myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n51); break;
                }
    
                bgcycle++;
            }
    
            CercleEngine() 
            {   
                updateBG(); 
            }
    
    
            public void onCreate(SurfaceHolder surfaceHolder) 
            {
                super.onCreate(surfaceHolder);
            }
    
            @Override
            public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) 
            {
                super.onOffsetsChanged(mxOffset, yOffset, xStep, yStep, xPixels, yPixels);
                mxOffset = xPixels;
                drawFrame();
            }
    
    
    
            void drawFrame() 
            {
                final SurfaceHolder holder = getSurfaceHolder();
    
                Canvas c = null;
                try 
                {
                    c = holder.lockCanvas();
                    if (c != null) 
                    {           
    
                        c.save();
                        c.translate((float) mxOffset, 0f);
    
                        if(myBg != null) {
                            updateBG();
                            c.drawBitmap(myBg, 0, 0, null);
                        }
    
                        c.restore();
                        //updateBG();
                        //c.drawBitmap(myBg, 0, 0, null);
    
                    }
                }finally{
                    if (c != null) holder.unlockCanvasAndPost(c);
                }
                handler.removeCallbacks(drawRunner);
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

问题是你每次都在updateBG()中创建位图。这让它变得迟钝。