gpt4 book ai didi

android - 设置另一个动态壁纸时,Android 设备中的动态壁纸不会更改

转载 作者:行者123 更新时间:2023-11-30 00:58:27 24 4
gpt4 key购买 nike

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

Activity 设置壁纸类

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

});

图像适配器:

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 按钮点击事件中使用此代码,

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

}
});

这对我有用...

关于android - 设置另一个动态壁纸时,Android 设备中的动态壁纸不会更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39805800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com