如何在下载图像时以最佳方式显示图像

时间:2015-06-19 11:05:32

标签: android download imageview inflate

我想显示一些文字和图片,但是当应用程序下载图片时(我第一次运行活动),就会发生这种情况(这不好)

It's now Ok

但当应用程序下载图像时会发生这种情况(我第二次运行活动)(这很好) It's Ok

这是我的代码:

          LinearLayout layoutRoot = (LinearLayout) findViewById(R.id.layoutRoots);
      String text = "This is An Image File [LoadImage:addlike.png] this is Another Image [LoadImage:adddislike.png] this is Another Image ";
      processText(text, layoutRoot, R.layout.image_style, R.layout.text_style);
}
private void processText(String text, LinearLayout layoutRoot,int imageLayout, int textLayout) {
    Pattern pattern = Pattern.compile("\\[LoadImage:(.*?)\\]");
    Matcher matcher = pattern.matcher(text);
    int offset = 0;
    while (matcher.find()) {
        try {
            String textBeforImage = text.substring(offset, matcher.start());
            offset += textBeforImage.length();
            textBeforImage = textBeforImage.trim();
            if (textBeforImage.length() != 0) {
                addTextView(textBeforImage);
            }
            String ImageName = matcher.group(1).toString();
            if (ImageName.length() != 0) {
                addImageView(ImageName);
                offset += text.substring(matcher.start(), matcher.end()).length();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    try {
        if (offset != text.length()) {
            String content = text.substring(offset).trim();
            if (content.length() != 0) {
                addTextView(content);
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
private void addTextView(String text){
     View custom = G.layoutInflater.inflate(R.layout.text_style, null);
     TextView tv = (TextView) custom.findViewById(R.id.myTxt);
        tv.setText(text);
        parent.addView(custom);
        setContentView(parent);
}
private void addImageView(String imageName){
     View custom = G.layoutInflater.inflate(R.layout.image_style, null);
     parent.addView(custom); 
     setContentView(parent);
      ImageView img = (ImageView) custom.findViewById(R.id.MyImg);
          final File imageFile = new File(G.DIR_FINAL + "/" + imageName);
          if ( !imageFile.exists()) {
              img.setImageBitmap(null);
              String link = "http://example.com/Images/" + imageName;
              DownloadImage.addToDownloadList(link, img);
          }
          BitmapFactory.Options options = new BitmapFactory.Options();
          //options.inSampleSize = 8;
          Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
          img.setImageBitmap(bitmap);
}

如果你需要image_style或text_style或其他任何东西,请告诉我 抱歉我的英文

0 个答案:

没有答案