3

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

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

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

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

这是我的代码::

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

1 回答 1

0

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

于 2012-06-26T09:11:16.787 回答