通用图像加载器:为什么加载的图像限制了Textview的垂直大小

时间:2016-04-20 22:41:05

标签: android json universal-image-loader

我正在使用排球从JSON获取数据。在bookContent中显示的数据中,有< img>不同位置的标签。

我使用通用图像加载程序在< img>中加载图像标签

这是我的活动。

BookDetails

public class BookDetails extends AppCompatActivity{

    private final String TAG = "BookDetails";

    private JSONObject bookData;

    protected com.nostra13.universalimageloader.core.ImageLoader mImageLoader;


    TextView bookTitle, bookAuthorDate, bookContent;
    View firstView, secView;
    CircularNetworkImageView authorImg;
    ImageLoader AuthImgLoader;


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

        showDialog();

        bookTitle = (TextView) findViewById(R.id.dbook_title);
        bookAuthorDate = (TextView) findViewById(R.id.author_date);
        bookContent = (TextView) findViewById(R.id.dbook_content);
        authorImg = (CircularNetworkImageView) findViewById(R.id.author_img);
        firstView =  findViewById(R.id.dviewtop);
        secView =  findViewById(R.id.dviewbottom);

        DisplayImageOptions defaultoptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultoptions)
                .writeDebugLogs()
                .build();


        mImageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
        mImageLoader.init(config);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }



        if (savedInstanceState != null) {

            try {
                String bookDataStr = savedInstanceState.getString("bookData");
                bookData = new JSONObject(bookDataStr);

                parseBook(bookData);
            } catch (JSONException e) {
                e.printStackTrace();
            }


        } else {
            if (NetworkCheck.isAvailableAndConnected(this)) {
                //Calling method to load books
                loadBook();
            } else {
                internetDialog.show();
            }
        }
    }


    private void loadBook() {
        Log.d(TAG, "loadBook called");

        final ProgressBar progressBar;
        progressBar = (ProgressBar) findViewById(R.id.progress_circle);
        progressBar.setVisibility(View.VISIBLE);


        int news_id = getIntent().getIntExtra("BookId", -1);
        Log.d(TAG, "You clicked book id " + book_id);

        final JsonObjectRequest jsonObjReq = new JsonObjectRequest( DetailConfig.GET_DURL + book_id, null,


                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("Debug", response.toString());

                        //Dismissing progressbar;
                        if (progressBar != null) {
                            progressBar.setVisibility(View.GONE);
                        }

                        bookData = response;

                        //Calling method to parse json array
                        parseBook(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d("", "Error: " + error.getMessage());

                        if (progressBar != null) {
                            progressBar.setVisibility(View.GONE);
                        }


                    }
                });

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to queue
        requestQueue.add(jsonObjReq);
    }

    //This method will parse json data of book
    private void parseBook(JSONObject jsonObject) {

        Log.d(TAG, "Parsing book array");


            try {
                String title = jsonObject.getString(DetailConfig.TAG_DPOST_TITLE);
                bookTitle.setText(Html.fromHtml(title));

                JSONObject pAuthor = jsonObject.getJSONObject("author");
                String author = pAuthor.getString("name");
                String authorimg = pAuthor.getString("avatar");

                AuthImgLoader = VolleyRequest.getInstance(getApplicationContext()).getImageLoader();
                AuthImgLoader.get(authorimg, ImageLoader.getImageListener(authorImg, R.drawable.ic_author, R.drawable.ic_author));
                authorImg.setImageUrl(authorimg, AuthImgLoader);

                String content = jsonObject.getString(DetailConfig.TAG_DPOST_CONTENT);
                Spanned spanned = Html.fromHtml(content, new UILImageGetter(bookContent, this), null);
                bookContent.setText(spanned);

            } catch (JSONException w) {
                w.printStackTrace();
            }

        //Unhiding views
        bookTitle.setVisibility(View.VISIBLE);
        bookAuthorDate.setVisibility(View.VISIBLE);
        bookContent.setVisibility(View.VISIBLE);
        authorImg.setVisibility(View.VISIBLE);
        firstView.setVisibility(View.VISIBLE);
        secView.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("bookData", bookData.toString());
    }


}

下面,我使用从the accepted answer in this question获得的这段代码来加载bookContent中的图片。

本课程使用Universal Image Loader。

UILImageGetter

public class UILImageGetter implements Html.ImageGetter{
    Context c;
    TextView conatiner;
    UrlImageDownloader urlDrawable;

    public UILImageGetter(View textView, Context context) {
        this.c = context;
        this.conatiner = (TextView) textView;
    }

    @Override
    public Drawable getDrawable(String source) {
        urlDrawable = new UrlImageDownloader(c.getResources(), source);
        if (Build.VERSION.SDK_INT >= 21) {
        urlDrawable.mDrawable = c.getResources().getDrawable(R.drawable.default_thumb,null);
        } else {
            urlDrawable.mDrawable = c.getResources().getDrawable(R.drawable.default_thumb);
        }
        ImageLoader.getInstance().loadImage(source, new SimpleListener(urlDrawable));
        return urlDrawable;
    }

    private class SimpleListener extends SimpleImageLoadingListener {
        UrlImageDownloader mUrlImageDownloader;

        public SimpleListener(UrlImageDownloader downloader) {
            super();
            mUrlImageDownloader= downloader;
        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            int width = loadedImage.getWidth();
            int height = loadedImage.getHeight();

            int newWidth = width;
            int newHeight = height;

            if (width > conatiner.getWidth()) {
                newWidth = conatiner.getWidth();
                newHeight = (newWidth * height) / width;
            }

            if (view != null) {
                view.getLayoutParams().width = newWidth;
                view.getLayoutParams().height = newHeight;
            }

            Drawable result = new BitmapDrawable(c.getResources(), loadedImage);
            result.setBounds(0, 0, newWidth, newHeight);

            mUrlImageDownloader.setBounds(0, 0, newWidth, newHeight);
            mUrlImageDownloader.mDrawable = result;

            conatiner.setHeight((conatiner.getHeight() + result.getIntrinsicHeight()));
            conatiner.invalidate();
        }

    }

    private class UrlImageDownloader extends BitmapDrawable {
        public  Drawable mDrawable;

        public UrlImageDownloader(Resources resources, String filepath) {
            super(resources, filepath);
            mDrawable = new BitmapDrawable(resources, filepath);
        }

        @Override
        public void draw(Canvas canvas) {
            if (mDrawable != null) {
                mDrawable.draw(canvas);
            }
        }
    }
}

一切正常,JSON已正确解析并显示,图片已加载但存在问题。

加载的图像会影响bookContent中显示的垂直线条。如果有许多垂直线,则它的某些部分被切断。 如果bookContent的垂直线很少,则TextView底部会留下一个大的空白区域。 但是,如果我没有加载图片,bookContent显示正常,没有截止,没有额外的空间。

请问,我该如何解决?

1 个答案:

答案 0 :(得分:0)

我从dcowthis question评论中回答了这个问题。我做的是我删除了

conatiner.setHeight((conatiner.getHeight() + result.getIntrinsicHeight()));

并写了

下的

container.setText(container.getText());

container.setText(container.getText());