如何使用rxjava和rxandroid在imageview上设置可绘制

2020-02-14 java android

我可以在使用Textview的代码中使用rxjava和rxandroid使用文本。现在,我正在考虑用imageview替换textview并在imageview上显示可绘制对象。但是我遇到了错误。因为我是rxjava的新手,所以我很可能确定自己缺少某些东西。一个帮助将是真正的gr8!

因为要显示文本,所以我们将参数用作String ...所以我认为对于可绘制对象,我认为我可能需要将参数用作Drawable ...但是它给定错误。

package com.prajwal.rxjava_rxandroid;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;


import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    ImageView textView;
    Observable<Integer> stringObservable;
    DisposableObserver<Drawable> disposableObserver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textview);

        stringObservable = Observable.just(R.drawable.ic_launcher_background);


disposableObserver = new DisposableObserver<Drawable>() {
    @Override
    public void onNext(Drawable drawable) {
        Glide.with(getApplicationContext()).load(drawable).into(textView);
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onComplete() {

    }
};

        stringObservable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableObserver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposableObserver.dispose();
    }
}

错误:无法解析方法subscribe(Drawable)

Answers

找出解决方案:

package com.prajwal.rxjava_rxandroid;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;


import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    ImageView textView;
    Observable<Integer> stringObservable;
    DisposableObserver<Integer> disposableObserver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textview);

        stringObservable = Observable.just(R.drawable.ic_launcher_background);


/*
        stringObserver = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
                textView.setText(s); //emitted data is set to textview.
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };
*/

    disposableObserver = new DisposableObserver<Integer>() {
    @Override
    public void onNext(Integer integer) {
        Glide.with(getApplicationContext()).load(integer).asBitmap().into(textView);
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onComplete() {

    }
};

        stringObservable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableObserver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposableObserver.dispose();
    }
}

我将参数传递为Drawable,而我需要传递 参数作为Integer,因为Glide的.load()不将Drawable用作 参数。

Related