如何在recyclerview中使用imagegetter

时间:2017-08-22 12:04:38

标签: android bitmap android-recyclerview

您好我正在尝试在textview上显示html,所以我使用Imagegetter方法来显示它。现在它正在显示,但它使Recyclerview不那么顺畅。我使用过的代码。

    txtMsg.setText(Html.fromHtml(m.getMessage(),new ImageGetter(), null));

     public class ImageGetter implements Html.ImageGetter {
    Bitmap bitmap = null;
    public Drawable getDrawable(String source) {
        int id;
        Log.d("source",source);


       new LoadImage(new LoadImage.AsyncResponse(){

            @Override
            public void processFinish(Bitmap output){
                //Here you will receive the result fired from async class
                //of onPostExecute(result) method.

                bitmap=output;

        try {
            bitmap =  new LoadImage().execute(source).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        Drawable d =new BitmapDrawable(context.getResources(),bitmap);

        if(source.contains("emoji")){
            d.setBounds(0,0,40,40);
        }else{
            d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
        }

        return d;
    }

}

这是我的Asynictask班级

    class LoadImage extends AsyncTask<String, Void, Bitmap> {


   private Bitmap bitmap1;



    @Override
    protected Bitmap doInBackground(String... params) {

        try {

            URL url=new URL(params[0]);
            // url = new URL("http://www.gravatar.com/avatar/a9317e7f0a78bb10a980cadd9dd035c9?s=32&d=identicon&r=PG");
             bitmap1 = BitmapFactory.decodeStream(url.openConnection().getInputStream());


            // bitmap = Picasso.with(MainActivity.this).load("http://www.gravatar.com/avatar/a9317e7f0a78bb10a980cadd9dd035c9?s=32&d=identicon&r=PG").get();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap1;
    }


    @Override
    protected void onPostExecute(Bitmap bitmap) {

        Log.d(TAG, "onPostExecute bitmap " + bitmap);
        if (bitmap != null) {
            Log.d(TAG, "onPostExecuteafter" + bitmap);
            // myMethod(bitmap);

            CharSequence charSequence = txtMsg.getText();
            txtMsg.setText(charSequence);

        }

    }
}

然后我因为这一行getmothod而依赖它。

     bitmap =  new LoadImage().execute(source).get();

我也使用过这样的接口,但后来我无法在Textview中显示图像。

   public class LoadImage extends AsyncTask<String, Void, Bitmap> {

private Bitmap bitmap1;


public interface AsyncResponse {
    void processFinish(Bitmap output);
}

public LoadImage.AsyncResponse delegate = null;

public LoadImage(LoadImage.AsyncResponse delegate){
    this.delegate = delegate;
}

@Override
protected Bitmap doInBackground(String... params) {

    try {

        URL url=new URL(params[0]);
        // url = new URL("http://www.gravatar.com/avatar/a9317e7f0a78bb10a980cadd9dd035c9?s=32&d=identicon&r=PG");
        bitmap1 = BitmapFactory.decodeStream(url.openConnection().getInputStream());


        // bitmap = Picasso.with(MainActivity.this).load("http://www.gravatar.com/avatar/a9317e7f0a78bb10a980cadd9dd035c9?s=32&d=identicon&r=PG").get();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap1;
}


@Override
protected void onPostExecute(Bitmap bitmap) {

    Log.d(TAG, "onPostExecute bitmap " + bitmap);
    if (bitmap != null) {
        Log.d(TAG, "onPostExecuteafter" + bitmap);

        delegate.processFinish(bitmap);

    }

   }
   }

我会像这样修改我的图片getter

     public class ImageGetter implements Html.ImageGetter {
    Bitmap bitmap = null;
    public Drawable getDrawable(String source) {
        int id;
        Log.d("source",source);


       new LoadImage(new LoadImage.AsyncResponse(){

            @Override
            public void processFinish(Bitmap output){
                //Here you will receive the result fired from async class
                //of onPostExecute(result) method.
                Toast.makeText(context,output.toString(),Toast.LENGTH_LONG).show();
                bitmap=output;
               /* CharSequence charSequence = txtMsg.getText();
                txtMsg.setText(charSequence);*/
            }
        }).execute(source);
       /* try {
            bitmap =  new LoadImage().execute(source).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }




*/

        Drawable d =new BitmapDrawable(context.getResources(),bitmap);

        if(source.contains("emoji")){
            d.setBounds(0,0,40,40);
        }else{
            d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
        }

        return d;
    }

请帮助卡住。

0 个答案:

没有答案