我是Android开发的新手,目前正在实现动态壁纸应用程序,因此,我的问题是从应用程序中选择新的动态壁纸时如何更改动态壁纸。当前我仅从应用程序中设置一个动态壁纸,但问题是当我从应用程序中选择墙纸设置为墙纸时,它不会更改并显示先前选择的墙纸。当我重新启动设备时,它将显示。我正在使用Glide库显示Gif图像。

这是我的WallpaperService类

public class GifPaperService extends WallpaperService {
static final String TAG = "gifService";
static final Handler gifHandler = new Handler();
int position;
boolean visible;

ImageAdapter img = new ImageAdapter();

Integer[] mThumb = img.mThumbIds;

@Override
public void onCreate() {
    super.onCreate();
    Log.v("Helllo", "...");
}

@Override
public Engine onCreateEngine() {
    try {
        return new GifEngine();
    } catch (IOException e) {
        Log.w(TAG, "Error creating engine", e);
        stopSelf();
        return null;
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.v("Hello..", ".....");
    return super.onStartCommand(intent, flags, startId);
}

class GifEngine extends Engine {
    private final Movie gif;
    private final int duration;
    private final Runnable runnable;

    float scaleX;
    float scaleY;
    int when;
    long start;

    GifEngine() throws IOException {

        MyPreferenceActivity myPref = new MyPreferenceActivity(getApplicationContext());
        Log.i("Imageis... ", "Position.." + myPref.getGifImage());

        position = myPref.getGifImage();

        InputStream is = getResources().openRawResource(mThumb[position]);
        Log.i("Imageposition...", "...." + mThumb[position]);

        if (is == null) {
            throw new IOException("Unable to open whoa.gif");
        }

        try {
            gif = Movie.decodeStream(is);
            duration = gif.duration();
        } finally {
            is.close();
        }
        when = -1;
        runnable = new Runnable() {
            @Override
            public void run() {
                animateGif();
            }
        };
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        gifHandler.removeCallbacks(runnable);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        super.onVisibilityChanged(visible);
        if (visible) {
            animateGif();
        } else {
            gifHandler.removeCallbacks(runnable);
        }
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);
        scaleX = width / (1f * gif.width());
        scaleY = height / (1f * gif.height());
        animateGif();
    }

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

    void animateGif() {
        tick();

        SurfaceHolder surfaceHolder = getSurfaceHolder();
        Canvas canvas = null;

        try {
            canvas = surfaceHolder.lockCanvas();

            if (canvas != null) {
                gifCanvas(canvas);
            }
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

        gifHandler.removeCallbacks(runnable);

        if (isVisible()) {
            gifHandler.postDelayed(runnable, 1000L / 25L);
        }
    }

    void tick() {
        if (when == -1L) {
            when = 0;
            start = SystemClock.uptimeMillis();
        } else {
            long diff = SystemClock.uptimeMillis() - start;
            when = (int) (diff % duration);
        }

    }

    void gifCanvas(Canvas canvas) {
        canvas.save();
        canvas.scale(scaleX, scaleY);
        gif.setTime(when);
        gif.draw(canvas, 0, 0);
        canvas.restore();
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        super.onSurfaceDestroyed(holder);
        stopSelf();
        gifHandler.removeCallbacks(runnable);
    }
}
}


设置墙纸的活动班

 setWallpaper.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT > 15) {
                Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
                intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(mContext, GifPaperService.class));
                startActivity(intent);
            }
        }

    });


ImageAdapter:

public class ImageAdapter extends BaseAdapter {

static WallpaperInfo info;
private Context mContext;

public ImageAdapter() {

}

public int getCount() {
    return mThumbIds.length;
}
public Object getItem(int position) {
    return mThumbIds[position];
}
public long getItemId(int position) {
    return 0;
}
public ImageAdapter(Context c) {
    mContext = c;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null){
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(3, 3, 3, 3);
        imageView.setMaxHeight(300);
        imageView.setMaxWidth(300);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MyPreferenceActivity myPref = new MyPreferenceActivity(mContext);
                myPref.setGifImage(position);


                Intent intent = new Intent(mContext, FullScreenImage.class);
                intent.putExtra("imageID", mThumbIds[position]);
                /*intent.putExtra(EXTRA_LIVE_WALLPAPER_INTENT, intent);
                intent.putExtra(EXTRA_LIVE_WALLPAPER_SETTINGS, info.getSettingsActivity());
                intent.putExtra(EXTRA_LIVE_WALLPAPER_PACKAGE, info.getPackageName());*/
                mContext.startActivity(intent);
            }
        });

        Animation anim = AnimationUtils.loadAnimation(mContext.getApplicationContext(), R.anim.fly);
        imageView.setAnimation(anim);
        anim.start();

    }
    else{
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}
public Integer[] mThumbIds = {
        R.drawable.gpp1, R.drawable.gpp2,
        R.drawable.gpp3,R.drawable.gpp4,
        R.drawable.gpp5,R.drawable.gpp6,
        R.drawable.gpp7,R.mipmap.h8,
        R.mipmap.h9,R.mipmap.h10,
        R.mipmap.h11,R.drawable.gp3,
        R.drawable.gp2,R.drawable.gp,
        R.drawable.onehalloween
};
}


如果有人知道问题出在哪里,请告诉我。
预先感谢

最佳答案

要销毁先前的墙纸并设置新墙纸,您必须像这样清除先前的墙纸,

在您的setWallpaper按钮click事件中使用此代码,

setWallpaper.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT > 16) {

                WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                try {
                    wallpaperManager.clear();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
                intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(mContext, GifPaperService.class));

                Log.i("Intent....", "...." + intent);
                startActivity(intent);
            }

        }
    });


这对我有用

10-08 18:51