我正在开发动态壁纸应用程序。在其中,当我第一次尝试设置动态壁纸时,它不会被覆盖。如果我在它们之间放置图像,则完成。那么有人会帮助我知道如何在另一张动态壁纸上覆盖另一张动态壁纸吗?
我的代码在这里:
WallpaperManager wallpaperManager =
WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setResource(pos);
} catch (IOException e) {
e.printStackTrace();
}
mResourceId = R.raw.screen_1;
Intent intent = new Intent();
intent.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(SetwallpaperActivity.this, LiveWallpaperService.class));
startActivity(intent);
}
}).start();
服务等级:
public class LiveWallpaperService extends WallpaperService {
private AnimationEngine mEngine;
private int mDeviceWidth;
private int mDeviceHeight;
public static final Handler mDrawHandler = new Handler();
@Override
public void onCreate() {
super.onCreate();
mDeviceWidth = AppUtils.getDeviceWidth(LiveWallpaperService.this);
mDeviceHeight = AppUtils.getDeviceHeight(LiveWallpaperService.this);
}
@Override
public Engine onCreateEngine() {
try {
mEngine = new AnimationEngine();
return mEngine;
} catch (IOException e) {
stopSelf();
return null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void doProcessing() throws IOException {
if (mEngine != null) {
mEngine.process(mResourceId);
}
}
/**
* The class is used to start live wallpaper engine.
*
* @author vishalbodkhe
*/
class AnimationEngine extends Engine {
private Movie mMovie;
private int mMovieDuration;
public Runnable mAnimationRunnable;
float mScaleX;
float mScaleY;
int mWhen;
long mStart;
AnimationEngine() throws IOException {
process(mResourceId);
mWhen = -1;
/* mAnimationRunnable = new Runnable() {
public void run() {
drawAnimation();
}
};*/
}
public void process(int resId) throws IOException {
InputStream is = getResources().openRawResource(resId);
if (is != null) {
try {
mMovie = Movie.decodeStream(is);
mMovieDuration = mMovie.duration();
mScaleX = mDeviceWidth / (1f * mMovie.width());
mScaleY = mDeviceHeight / (1f * mMovie.height());
} catch (Exception e) {
} finally {
is.close();
}
} else {
throw new IOException("Unable to open input gif file");
}
mWhen = -1;
mAnimationRunnable = new Runnable() {
public void run() {
drawAnimation();
}
};
}
@Override
public void onDestroy() {
super.onDestroy();
mDrawHandler.removeCallbacks(mAnimationRunnable);
mAnimationRunnable = null;
}
@SuppressLint("ResourceType")
@Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
if (visible) {
drawAnimation();
} else {
mDrawHandler.removeCallbacks(mAnimationRunnable);
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
mDeviceHeight = height;
mDeviceWidth = width;
mScaleX = width / (1f * mMovie.width());
mScaleY = height / (1f * mMovie.height());
drawAnimation();
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xOffsetStep, float yOffsetStep, int xPixelOffset,
int yPixelOffset) {
super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
xPixelOffset, yPixelOffset);
drawAnimation();
}
/**
* Method to draw animation
*/
private void drawAnimation() {
findDuration();
SurfaceHolder surfaceHolder = getSurfaceHolder();
Canvas canvas = null;
try {
canvas = surfaceHolder.lockCanvas();
/* synchronized (surfaceHolder) {
drawPicture(canvas);
}*/
if (canvas != null) {
drawPicture(canvas);
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
mDrawHandler.removeCallbacks(mAnimationRunnable);
if (isVisible()) {
mDrawHandler.postDelayed(mAnimationRunnable, 1000L / 25L);
}
}
/**
* Method to get duration
*/
private void findDuration() {
if (mWhen == -1L) {
mWhen = 0;
mStart = SystemClock.uptimeMillis();
} else {
long mDiff = SystemClock.uptimeMillis() - mStart;
if (mMovieDuration > 0) {
mWhen = (int) (mDiff % mMovieDuration);
} else {
mMovieDuration = mMovie.duration();
}
}
}
/*
* Method to draw images on canvas
*/
private void drawPicture(Canvas canvas) {
canvas.save();
canvas.scale(mScaleX, mScaleY);
mMovie.setTime(mWhen);
mMovie.draw(canvas, 0, 0);
canvas.restore();
}
}
}