我正在研究如何应用实时壁纸(GIF图片)。当我点击应用按钮时,默认的gif图像会被设置为壁纸。我从Firebase获取所有图像,所以我想将该图像设置为壁纸。我不知道如何将来自
我不知道如何在
LiveViewActivity
的gif图像传递给GIFWallpaperService
,以将该.gif图像设置为实时壁纸,而不是默认图像。(抱歉我的英语不好,希望你能理解)LiveWallpaperActivity.java //main activity(where I'm getting all the images from firebase)
|
| //pass the .gif image url by intent to next activity
|
LiveViewActivity.java
|
|
| //Here I receive the image by intent and load into imageview with glide
|
| //added a button to apply live wallpaper(.gif image)
| //pass .gif image to GIFWallpaperService class service (I don't know how to do)
|
GIFWallpaperService
实时视图活动
在这里我添加了一个按钮来应用动态壁纸
在这里,我想将.gif图像传递给GIFWallpaperService
public class LiveViewActivity extends AppCompatActivity {
ImageView imageView;
Button setLiveWallpaper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_view);
imageView = findViewById(R.id.viewImage);
Glide.with(this).load(getIntent().getStringExtra("images")).into(imageView);
setLiveWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
applyLiveWallpaper();
}
});
}
private void applyLiveWallpaper() {
Intent intent = new Intent(
WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(this, GIFWallpaperService.class));
startActivity(intent);
}
}
GIFWallpaperService
我希望从LiveViewActivity接收发送的.gif图片,并将其设置为动态壁纸。
public class GIFWallpaperService extends WallpaperService {
@Override
public WallpaperService.Engine onCreateEngine() {
try {
Movie movie = Movie.decodeStream(getResources().getAssets().open("sea_gif.gif")); //Here is the default gif image
return new GIFWallpaperEngine(movie);
} catch (IOException e) {
Log.d("GIFWallpaperService", "Could not loaded live wallpaper");
return null;
}
}
private class GIFWallpaperEngine extends WallpaperService.Engine {
private final int frameDuration = 20;
private SurfaceHolder holder;
private Movie movie;
private boolean visible;
private Handler handler;
public GIFWallpaperEngine(Movie movie) {
this.movie = movie;
handler = new Handler();
}
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
this.holder = surfaceHolder;
}
private Runnable drawGIF = new Runnable() {
@Override
public void run() {
draw();
}
};
private void draw() {
if (visible) {
Canvas canvas = holder.lockCanvas();
canvas.save();
canvas.scale(4f, 4f);
movie.draw(canvas, -100, 0);
canvas.restore();
holder.unlockCanvasAndPost(canvas);
movie.setTime((int) (System.currentTimeMillis() % movie.duration()));
handler.removeCallbacks(drawGIF);
handler.postDelayed(drawGIF, frameDuration);
}
}
@Override
public void onVisibilityChanged(boolean visible) {
//super.onVisibilityChanged(visible);
this.visible = visible;
if (visible) {
handler.post(drawGIF);
} else {
handler.removeCallbacks(drawGIF);
}
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(drawGIF);
}
}
}
我不知道如何在
LiveViewActivity
和GIFWallpaperService
之间发送和接收 .gif 图片。