将图片从 Android 活动传递到类(动态壁纸)

4
我正在研究如何应用实时壁纸(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);

        }
    }

}



我不知道如何在LiveViewActivityGIFWallpaperService之间发送和接收 .gif 图片。

添加共享首选项监听器并将其作为选项传递到文件路径。 - Style-7
2个回答

1
你的问题对我来说有点模糊,但如果我理解正确的话,实际上你可以通过适配器和上下文轻松地在 LiveWallpaperActivity 中获取图片,然后从该活动中通过意图将您的图像传递到任何想要的活动。
要从活动发送数据到服务,您需要覆盖 onStartCommand,在那里您直接访问意图。
Override
public int onStartCommand(Intent intent, int flags, int startId) {

然后从LiveViewActivity中创建意图对象来启动服务,然后将您的图片名称放入意图对象中:

 Intent serviceIntent = new Intent(YourService.class.getName())
 serviceIntent.putExtra("IMAGE_KEY", "image");
 context.startService(serviceIntent);

当服务启动时,它的onStartCommand()方法将被调用,然后您可以获取图像:

public int onStartCommand (Intent intent, int flags, int startId) {
String image = intent.getStringExtra("IMAGE_KEY");
return START_STICKY;
}

首先,我从LiveWallpaperActivity将图像传递到LiveViewActivity。在LiveViewActivity中,我有一个按钮(设置动态壁纸)。当按钮被点击时,我想将图像从LiveViewActivity发送到GIF壁纸服务类。 - Marsad
如果您在LvieViewActivity中有图像并需要将其发送到您的服务,则可以通过意图来执行此操作,请再次检查我的答案。 - Parisa Baastani

0

尝试使用DomainEventBus,它可以帮助在所有应用程序之间传递对象、图像和实际上任何数据类型。 https://greenrobot.org/eventbus/

使用此功能,您需要在创建服务时注册“事件总线”,然后它将能够从应用程序的任何组件接收图像。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接