読者です 読者をやめる 読者になる 読者になる

そーす

福岡在住のプログラマ

Webから引っ張ってきた画像をRepeatさせる

Android Java
        String imageSource = "http://.....";
        Single
                .create(new Single.OnSubscribe<BitmapDrawable>() {
                    @Override
                    public void call(SingleSubscriber<? super BitmapDrawable> singleSubscriber) {
                        try {
                            //Webから画像読み込み
                            URL url = new URL(imageSource);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setDoInput(true);
                            connection.connect();
                            InputStream input = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(input);
                            BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
                            //リピート設定
          bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
                            singleSubscriber.onSuccess(bitmapDrawable);
                        } catch (IOException e) {
                            singleSubscriber.onError(e);
                        }
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribe(new SingleSubscriber<BitmapDrawable>() {
                    @Override
                    public void onSuccess(BitmapDrawable value) {
                        //Viewに反映
                    }

                    @Override
                    public void onError(Throwable error) {

                    }
                });