Android Live壁纸在Kitkat 4.4.4上加载了一秒钟,但在4.4.2版本以下工作正常

时间:2015-01-06 10:20:32

标签: android

我正在制作动态壁纸,在HomeScreen上显示简单的gif动画,我有三个设备,一个有Kitkat 4.4.4,第二个有Kitkat 4.4.2,另一个我有Jellybean,它在Kitkat 4.4上工作正常。 2和Jellybean但没有进入kitkat 4.4.4,只需加载一秒钟和另一秒钟它就会在Kitkat 4.4.4上自我毁灭,

我没有收到任何错误或消息到Log-cat,我尝试使用自定义日志消息,它在所有设备上都具有相同的值,但壁纸的加载方式与其他设备上的相同。

任何帮助都会得到赞赏

这是我的代码

public class WallpaperServiceClass extends WallpaperService {
static final Handler mHandler = new Handler();
private int position;
private Canvas canvas = null;
public SharedPreferences mPreferences;
private InputStream is = null;

@Override
public void onCreate() {
    stopService(new Intent(WallpaperServiceClass.this,
            WallpaperServiceClass.class));
    mPreferences = getSharedPreferences("wallpapersettings", MODE_PRIVATE);
    new mEngine();
    super.onCreate();
}

@Override
public Engine onCreateEngine() {
    return new mEngine();
}

class mEngine extends Engine {
    private Movie mMovie;
    private int mMovieDuration;
    private Runnable mMovieRun;
    float mScaleX;
    float mScaleY;
    int mWhen;
    long mStart;

    mEngine() {
        try {
            loadGif(Integer.valueOf(mPreferences.getString("flakes", "0")));



        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void loadGif(int position) {
        try {
            if (position == 0) {
                is = getResources().openRawResource(R.raw.a);

            } else if (position == 1) {
                is = getResources().openRawResource(R.raw.b);

            } else if (position == 2) {
                is = getResources().openRawResource(R.raw.c);

            } else if (position == 3) {
                is = getResources().openRawResource(R.raw.d);

            } else if (position == 4) {
                is = getResources().openRawResource(R.raw.e);
            }
            if (is != null) {
                mMovie = Movie.decodeStream(is);
                mMovieDuration = mMovie.duration();
                is.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mWhen = -1;
        mMovieRun = new Runnable() {
            public void run() {
                nyan();
            }
        };
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(mMovieRun);
        android.os.Process.killProcess(android.os.Process.myPid());

    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        super.onVisibilityChanged(visible);
        if (visible) {
            nyan();
        } else {
            mHandler.removeCallbacks(mMovieRun);
        }
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);
        mScaleX = width / (1f * mMovie.width());
        mScaleY = height / (1f * mMovie.height());
        nyan();
    }

    @Override
    public void onOffsetsChanged(float xOffset, float yOffset,
            float xOffsetStep, float yOffsetStep, int xPixelOffset,
            int yPixelOffset) {
        super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
                xPixelOffset, yPixelOffset);
        nyan();
    }

    void nyan() {
        tick();
        SurfaceHolder surfaceHolder = getSurfaceHolder();
        try {
            canvas = surfaceHolder.lockCanvas();
            if (canvas != null) {
                nyanNyan(canvas);
            }
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
        mHandler.removeCallbacks(mMovieRun);
        if (isVisible()) {
            mHandler.postDelayed(mMovieRun, 1000L / 25L);
        }
        if (position != Integer.valueOf(mPreferences.getString("flakes",
                "0"))) {
            position = Integer.valueOf(mPreferences
                    .getString("flakes", "0"));

            loadGif(position);

        }

    }

    void tick() {
        if (mWhen == -1L) {
            mWhen = 0;
            mStart = SystemClock.uptimeMillis();
        } else {
            long mDiff = SystemClock.uptimeMillis() - mStart;
            if (mMovieDuration != 0)
                mWhen = (int) (mDiff % mMovieDuration);
        }
    }

    void nyanNyan(Canvas canvas) {
        canvas.save();
        canvas.scale(mScaleX, mScaleY);
        mMovie.setTime(mWhen);
        mMovie.draw(canvas, 0, 0);

        canvas.restore();

    }
}

}

感谢。

0 个答案:

没有答案