首页 \ 问答 \ ImageView最初没有显示图像(ImageView not showing image initially)

ImageView最初没有显示图像(ImageView not showing image initially)

我有GridView显示几个图像和一个与TouchImageView集成的ViewPager。 除了一种情况外,似乎工作正常。

当您单击任何GridView的缩略图图像以启动ViewPager时,response.getBitMap将显示为NULL。 但是当我们继续向右滑动时,图像加载正常。 当我们向后滑动时,甚至会显示第一张图像。 每次从GridViewActivity点击GridView的缩略图时,都会发生这种情况。

在检查日志文件后,我可以看到ImageLoader一次请求2个图像(当前和下一个图像)但是对于初始加载它执行相同的操作两次(所以总共4个请求,检查下面的logcat)。

FullScreenImageAdapter.java代码:

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class FullScreenImageAdapter extends PagerAdapter {
//Log tag
private static final String TAG = FullImageActivity.class.getSimpleName();
private Activity _activity;
private String[] _imagePaths, _imageCaptions;
private LayoutInflater inflater;
private View mCurrentView;
TouchImageView imgDisplay;

// constructor
public FullScreenImageAdapter(Activity activity, String[] imagePaths, String[] imageCaptions) {
    this._activity = activity;
    this._imagePaths = imagePaths;
    this._imageCaptions = imageCaptions;
}

@Override
public int getCount() {
    return this._imagePaths.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

public String getImageCaption(int position) {
    return _imageCaptions[position];
}

@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    mCurrentView = (View)object;
}

public View getCurrentView() {
    return mCurrentView;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

    inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container, false);

    imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);

    // We download image into ImageView
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    String strFullImageURL = _imagePaths[position];

    imageLoader.get(strFullImageURL, new ImageLoader.ImageListener() {
        @Override
        public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {

            Log.d(TAG, "Outside IF response.getBitmap() = "+response.getBitmap()+"");
            if (response.getBitmap() != null) {
                Log.d(TAG, "Inside IF response.getBitmap() = "+response.getBitmap()+"");
                // load bitmap into imageview
                imgDisplay.setImageBitmap(response.getBitmap());
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            // unable to fetch full image
            Log.d(TAG, "VolleyError while downloading full image: "+error.getMessage());
        }
    });

    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((RelativeLayout) object);
}
}

logcat的

10-05 18:16:31.286  24976-24976/com.xyz D/GridViewActivity﹕ Array length: 32
10-05 18:16:31.626  24976-24976/com.xyz D/FullImageActivity﹕ Outside IF response.getBitmap() = null
10-05 18:16:32.277  24976-24976/com.xyz D/FullImageActivity﹕ Outside IF response.getBitmap() = android.graphics.Bitmap@42a4f4a0
10-05 18:16:32.277  24976-24976/com.xyz D/FullImageActivity﹕ Inside IF response.getBitmap() = android.graphics.Bitmap@42a4f4a0
10-05 18:16:32.477  24976-24976/com.xyz D/FullImageActivity﹕ Outside IF response.getBitmap() = android.graphics.Bitmap@42a737d0
10-05 18:16:32.477  24976-24976/com.xyz D/FullImageActivity﹕ Inside IF response.getBitmap() = android.graphics.Bitmap@42a737d0

不知道为什么会这样。 我能做些什么来解决它?


I have GridView which displays few images and a ViewPager integrated with TouchImageView. It seems to be working fine except for one scenario.

When you click on any GridView's thumbnail image to initiate the ViewPager, the response.getBitMap is coming up as NULL. But when we keep swiping to right, images are loading fine. Even the first image is displayed when we swipe back to it. It happens everytime when an GridView's thumbnail image is clicked initially from GridViewActivity.

Upon checking the log files, I can see ImageLoader is making request for 2 images at a time (current and the next image) but for initial load it doing the same thing twice (so total 4 requests, check logcat below).

FullScreenImageAdapter.java code:

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class FullScreenImageAdapter extends PagerAdapter {
//Log tag
private static final String TAG = FullImageActivity.class.getSimpleName();
private Activity _activity;
private String[] _imagePaths, _imageCaptions;
private LayoutInflater inflater;
private View mCurrentView;
TouchImageView imgDisplay;

// constructor
public FullScreenImageAdapter(Activity activity, String[] imagePaths, String[] imageCaptions) {
    this._activity = activity;
    this._imagePaths = imagePaths;
    this._imageCaptions = imageCaptions;
}

@Override
public int getCount() {
    return this._imagePaths.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

public String getImageCaption(int position) {
    return _imageCaptions[position];
}

@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    mCurrentView = (View)object;
}

public View getCurrentView() {
    return mCurrentView;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

    inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container, false);

    imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);

    // We download image into ImageView
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    String strFullImageURL = _imagePaths[position];

    imageLoader.get(strFullImageURL, new ImageLoader.ImageListener() {
        @Override
        public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {

            Log.d(TAG, "Outside IF response.getBitmap() = "+response.getBitmap()+"");
            if (response.getBitmap() != null) {
                Log.d(TAG, "Inside IF response.getBitmap() = "+response.getBitmap()+"");
                // load bitmap into imageview
                imgDisplay.setImageBitmap(response.getBitmap());
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            // unable to fetch full image
            Log.d(TAG, "VolleyError while downloading full image: "+error.getMessage());
        }
    });

    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((RelativeLayout) object);
}
}

Logcat

10-05 18:16:31.286  24976-24976/com.xyz D/GridViewActivity﹕ Array length: 32
10-05 18:16:31.626  24976-24976/com.xyz D/FullImageActivity﹕ Outside IF response.getBitmap() = null
10-05 18:16:32.277  24976-24976/com.xyz D/FullImageActivity﹕ Outside IF response.getBitmap() = android.graphics.Bitmap@42a4f4a0
10-05 18:16:32.277  24976-24976/com.xyz D/FullImageActivity﹕ Inside IF response.getBitmap() = android.graphics.Bitmap@42a4f4a0
10-05 18:16:32.477  24976-24976/com.xyz D/FullImageActivity﹕ Outside IF response.getBitmap() = android.graphics.Bitmap@42a737d0
10-05 18:16:32.477  24976-24976/com.xyz D/FullImageActivity﹕ Inside IF response.getBitmap() = android.graphics.Bitmap@42a737d0

Not sure why this is happening. Anything I could do to fix it?


原文:https://stackoverflow.com/questions/26207887
更新时间:2023-05-10 07:05

最满意答案

设置组的布局数据的宽度和高度提示:

GridData data = new GridData(SWT.FILL, SWT.TOP, true, false);
data.widthHint = 200;
data.heightHint = 200;
grpModelProperties1.setLayoutData(data);

要使用ScrolledComposite你需要这样的东西:

ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);
// You must set a layout on scrolled composite
scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);

// Additional inner composite is required
Composite innerComposite = new Composite(scrolledComposite, SWT.NONE);
innerComposite.setLayout(new GridLayout());

Group grpModelProperties1 = new Group(innerComposite, SWT.SHADOW_IN);
grpModelProperties1.setLayout(new GridLayout());
grpModelProperties1.setText("Test Model");

GridData data1 = new GridData(SWT.FILL, SWT.TOP, true, false);
data1.heightHint = 400;
data1.widthHint = 400;
grpModelProperties1.setLayoutData(data1);

scrolledComposite.setContent(innerComposite);
// No need to use a resize listener
scrolledComposite.setMinSize(innerComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

Set the width and height hints for the layout data of the group:

GridData data = new GridData(SWT.FILL, SWT.TOP, true, false);
data.widthHint = 200;
data.heightHint = 200;
grpModelProperties1.setLayoutData(data);

To use ScrolledComposite you need something like:

ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);
// You must set a layout on scrolled composite
scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);

// Additional inner composite is required
Composite innerComposite = new Composite(scrolledComposite, SWT.NONE);
innerComposite.setLayout(new GridLayout());

Group grpModelProperties1 = new Group(innerComposite, SWT.SHADOW_IN);
grpModelProperties1.setLayout(new GridLayout());
grpModelProperties1.setText("Test Model");

GridData data1 = new GridData(SWT.FILL, SWT.TOP, true, false);
data1.heightHint = 400;
data1.widthHint = 400;
grpModelProperties1.setLayoutData(data1);

scrolledComposite.setContent(innerComposite);
// No need to use a resize listener
scrolledComposite.setMinSize(innerComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。