设置另一个动态壁纸

时间:2016-10-01 11:01:29

标签: android

我是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
};
}

如果有人知道这是什么问题。告诉我。 提前谢谢

1 个答案:

答案 0 :(得分:0)

为了摧毁以前的壁纸并设置新壁纸,你有清除之前的壁纸,

在您的setWallpaper按钮单击事件中使用此代码

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

        }
    });

这对我有用......

相关问题