Display images on Android using TextView and Html.ImageGetter asynchronously?

前端 未结 3 1065
孤独总比滥情好
孤独总比滥情好 2020-12-25 09:25

I want to set a TextView with SpannableString which is from the method below:

Html.fromHtml(String source, Html.ImageGetter imageGe         


        
3条回答
  •  青春惊慌失措
    2020-12-25 09:56

    These guys did a great job, this is my solution using Square's Picasso library:

    //...
    final TextView textView = (TextView) findViewById(R.id.description);
            Spanned spanned = Html.fromHtml(getIntent().getStringExtra(EXTRA_DESCRIPTION),
                    new Html.ImageGetter() {
                        @Override
                        public Drawable getDrawable(String source) {
                            LevelListDrawable d = new LevelListDrawable();
                            Drawable empty = getResources().getDrawable(R.drawable.abc_btn_check_material);;
                            d.addLevel(0, 0, empty);
                            d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
                            new ImageGetterAsyncTask(DetailActivity.this, source, d).execute(textView);
    
                            return d;
                        }
                    }, null);
            textView.setText(spanned);
    //...
    
    
    class ImageGetterAsyncTask extends AsyncTask {
    
    
        private LevelListDrawable levelListDrawable;
        private Context context;
        private String source;
        private TextView t;
    
        public ImageGetterAsyncTask(Context context, String source, LevelListDrawable levelListDrawable) {
            this.context = context;
            this.source = source;
            this.levelListDrawable = levelListDrawable;
        }
    
        @Override
        protected Bitmap doInBackground(TextView... params) {
            t = params[0];
            try {
                Log.d(LOG_CAT, "Downloading the image from: " + source);
                return Picasso.with(context).load(source).get();
            } catch (Exception e) {
                return null;
            }
        }
    
        @Override
        protected void onPostExecute(final Bitmap bitmap) {
            try {
                Drawable d = new BitmapDrawable(context.getResources(), bitmap);
                Point size = new Point();
                ((Activity) context).getWindowManager().getDefaultDisplay().getSize(size);
                // Lets calculate the ratio according to the screen width in px
                int multiplier = size.x / bitmap.getWidth();
                Log.d(LOG_CAT, "multiplier: " + multiplier);
                levelListDrawable.addLevel(1, 1, d);
                // Set bounds width  and height according to the bitmap resized size
                levelListDrawable.setBounds(0, 0, bitmap.getWidth() * multiplier, bitmap.getHeight() * multiplier);
                levelListDrawable.setLevel(1);
                t.setText(t.getText()); // invalidate() doesn't work correctly...
            } catch (Exception e) { /* Like a null bitmap, etc. */ }
        }
    }
    

    My 2 cents... Peace!

提交回复
热议问题