3

画廊活动:

==================
    TextView (tv)
------------------
 | tv | tv | tv |
------------------
    GalleryFragment: dynamically created; replaces FrameLayout
        FrameLayout: W&H: match_parent


    imageView: W&H are match_parent; scaleType: fitCenter; 
        layout_below: the tv's above;
        layout_above: the TextView below;
            result: imageView fits snuggly between.
        Parent: RelativeLayout W&H: match_parent        


------------------
    TextView
==================

图库活动说明:此活动一次显示一张属于特定收藏的图像。用户点击图片:新片段显示下一张图片。

我已经有好几天的问题来弄清楚如何:

  • A. 获取 imageView 的大小,以便我可以正确缩放位图以适应。
  • B. 现在我发生了一个无限循环。而且我想我知道为什么,从逻辑上讲,我的意思是当我将位图填充到可能onPreDraw()再次触发的 imageView 中,然后我的代码开始运行时。...但我不知道如何解决它。
  • C. 我认为synchronization可能是答案,但我以前从未使用过它,而且我认为我没有正确使用它。
  • D. 我想也许if(workerThread == null)检查一下可能会奏效……但它只会让速度减慢一小会儿。

在这一点上,我迷路了。我不知道如何获取隐藏在动态添加的片段内的 ImageView 的尺寸,同时使用这些尺寸缩小位图的大小,然后在调整大小后将该位图加载到片段的 ImageView 中。

如果您需要任何澄清,请询问。

下面是 GalleryFragment 的 onCreateView 和 BitmapWorkerTask 的代码。然后下面是我过滤的logcat的粘贴。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // inflate view
    View view = inflater.inflate(R.layout.frag_gallery_image, container, false);

    // get view handles
    imageView = (ImageView)view.findViewById(R.id.gallery_image);   

    // TESTING
    ViewTreeObserver vto = imageView.getViewTreeObserver();
    vto.addOnPreDrawListener(new OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            // TESTING
            viewWidth = imageView.getMeasuredWidth();
            viewHeight = imageView.getMeasuredHeight();

            Log.d(TAG, SCOPE +"onPreDraw viewWidth: " +viewWidth +", viewHeight: " +viewHeight);

            // TESTING: Added because "synchronization" attempt didn't work 
            if(bmwt == null){
                loadImage(viewWidth, viewHeight);                   
            }

            return true;
        }
    });

    // set view actions     
    imageView.setOnClickListener(GalleryFragment.this);     

    return view;
}

/**
 * A synchronized method for loading bitmaps in background thread.
 * Synchronization: http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
 * ...something isn't working as intended...
 */
private synchronized void loadImage(int viewWidth, int viewHeight){
    // TESTING
    Log.d(TAG, SCOPE +"loadImage viewWidth: " +viewWidth +", viewHeight: " +viewHeight);

    bmwt = new BitmapWorkerTask(imageView, viewWidth, viewHeight);
    bmwt.execute(imageUri);
}


/**
 * BitmapWorkerTask is a subclass of Asynctask for the purpose of loading
 * images off of the UI thread. 
 */
private class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap>{
    private String uri;
    private int imageViewWidth;
    private int imageViewHeight;

    // Constructor.
    public BitmapWorkerTask(ImageView imageView, int width, int height){
        imageViewWidth = width;
        imageViewHeight = height;
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(String... params) {
        uri = params[0];
        final Bitmap bitmap =  ImageUtils.decodeSampledBitmapFromUri(uri, imageViewWidth, imageViewHeight);

        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null; // use some default bitmap
        }
        if (bitmap != null) {
            Log.d(TAG, SCOPE +"bitmap kilobyte count: "+ bitmap.getByteCount() / 1024);

            imageView.setImageBitmap(bitmap);

            // make BitmapWorkerTask reference null again.
            bmwt = null;
        }// else do nothing.
    }       
}

日志猫。注意“位图千字节计数”在 asyncTask 的 onPostExecute 中被调用。...并且是我希望伐木停止的地方。

06-13 01:50:41.442: D/ROSS(12982): GalleryFragment: imageUri: /mnt/sdcard/So so so beautiful.jpg
06-13 01:50:41.522: D/ROSS(12982): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 01:50:41.522: D/ROSS(12982): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 01:50:41.632: D/ROSS(12982): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 01:50:41.827: D/ROSS(12982): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 01:50:43.053: D/ROSS(12982): GalleryFragment: bitmap kilobyte count: 1183
06-13 01:50:43.142: D/ROSS(12982): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 01:50:43.142: D/ROSS(12982): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 01:50:43.632: D/ROSS(12982): GalleryFragment: bitmap kilobyte count: 1183
06-13 01:50:43.713: D/ROSS(12982): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 01:50:43.713: D/ROSS(12982): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 01:50:44.023: D/ROSS(12982): GalleryFragment: bitmap kilobyte count: 1183
06-13 01:50:44.101: D/ROSS(12982): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 01:50:44.101: D/ROSS(12982): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 01:50:44.332: D/ROSS(12982): GalleryFragment: bitmap kilobyte count: 1183
06-13 01:50:44.414: D/ROSS(12982): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 01:50:44.414: D/ROSS(12982): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 01:50:44.681: D/ROSS(12982): GalleryFragment: bitmap kilobyte count: 1183
06-13 01:50:44.761: D/ROSS(12982): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 01:50:44.761: D/ROSS(12982): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 01:50:45.151: D/ROSS(12982): GalleryFragment: bitmap kilobyte count: 1183
06-13 01:50:45.227: D/ROSS(12982): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 01:50:45.231: D/ROSS(12982): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 01:50:45.521: D/ROSS(12982): GalleryFragment: bitmap kilobyte count: 1183
06-13 01:50:45.593: D/ROSS(12982): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 01:50:45.593: D/ROSS(12982): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 01:50:45.962: D/ROSS(12982): GalleryFragment: bitmap kilobyte count: 1183

编辑:

在实施 Nicholas 的建议后,这里的 logcat 总共是:

06-13 14:51:44.976: D/ROSS(15465): GalleryFragment: imageUri: /mnt/sdcard/RossAndClay - Copy (12).JPG
06-13 14:51:45.146: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:51:45.146: D/ROSS(15465): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 14:51:45.376: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:51:45.422: D/ROSS(15465): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 14:51:45.626: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:51:45.626: D/ROSS(15465): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 14:51:49.336: D/ROSS(15465): GalleryFragment: bitmap kilobyte count: 1183
06-13 14:51:49.356: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:51:51.327: D/ROSS(15465): GalleryFragment: bitmap kilobyte count: 1183
06-13 14:51:51.336: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:51:53.395: D/ROSS(15465): GalleryFragment: bitmap kilobyte count: 1183
06-13 14:51:53.469: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692

单击图像后,在片段中,这是该事件的总日志:

06-13 14:54:41.315: D/ROSS(15465): GalleryFragment: imageUri: /mnt/sdcard/RossAndClay - Copy (11).JPG
06-13 14:54:41.402: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:54:41.406: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:54:41.406: D/ROSS(15465): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 14:54:41.655: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:54:41.665: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:54:41.665: D/ROSS(15465): GalleryFragment: loadImage viewWidth: 480, viewHeight: 692
06-13 14:54:44.285: D/ROSS(15465): GalleryFragment: bitmap kilobyte count: 1183
06-13 14:54:44.305: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:54:44.305: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:54:46.965: D/ROSS(15465): GalleryFragment: bitmap kilobyte count: 1183
06-13 14:54:47.036: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692
06-13 14:54:47.036: D/ROSS(15465): GalleryFragment: onPreDraw viewWidth: 480, viewHeight: 692

每次单击图像时,logcat 中的条目数都会越来越长。我的片段交易看起来像:

/*
 * Create the fragment that holds the collection image.
 * @param imageUri
 */
private void createImageFragment(String imageUri) {
    // With each click wipe previous entry, ie: there's no going back.
    getFragmentManager().popBackStack();

    // create new fragment
    GalleryFragment galleryFrag = GalleryFragment.newInstance(imageUri);

    // programmatically add new fragment
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.gallery_imageFrame, galleryFrag, GALLERY_FRAG);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();        
}

在我写到这里的那一刻,我尝试更改popBackStack()popBackStackImmediate()并且停止了不断增长的 logcat 条目列表。现在,在完成了一些加载各种图像大小的测试之后,看起来图像的大小决定了调用 loadImage() 的次数,这是有道理的(尽管我想阻止这种情况发生),因为(例如)在实际设置 imageView 的图像之前,三个异步任务被触发。所以现在的任务是弄清楚如何确保loadImage()只调用一次。

编辑2:

有时一个特定的问题会冻干我们的大脑。简单地用类字段private boolean thisMethodCalled = false;和onPreDraw解决了对方法的多次调用:

            if(!thisMethodCalled){
                loadImage(viewWidth, viewHeight);
                thisMethodCalled = true;
            }

...尽管这并不能阻止 onPreDraw 在每次替换片段时被调用的次数越来越多,但不确定我能对此做些什么。

最终编辑 - 最佳解决方案:

有了从这个答案中的一个评论中收集到的概念,只需在结尾附近删除监听imageView.getViewTreeObserver().removeOnPreDrawListener(this);器,没有监听器,没有方法被多次调用;显然没有额外的 onPreDraw() 调用。最后,完全按照我的意图工作。

    ViewTreeObserver vto = imageView.getViewTreeObserver();
    vto.addOnPreDrawListener(new OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            // TESTING
            viewWidth = imageView.getMeasuredWidth();
            viewHeight = imageView.getMeasuredHeight();

            loadImage(viewWidth, viewHeight);

            imageView.getViewTreeObserver().removeOnPreDrawListener(this);
            return true;
        }
    });
4

1 回答 1

1

也许尝试将图像视图标签设置为 false 或 true。真正的意思是已经画好了。它可能会起作用,但不确定它会如何处理您的应用程序。

然后你可以有类似的东西

imageView = (ImageView)view.findViewById(R.id.gallery_image);
// we have not loaded the image yet
imageView.setTag(false);

然后在你的预抽

boolean hasLoaded = ((Boolean) imageView.getTag());
// if we have not loaded the image yet
// we want to load it
if(!hasLoaded){
            loadImage(viewWidth, viewHeight);                   
}

然后在后期执行

    if (bitmap != null) {
        Log.d(TAG, SCOPE +"bitmap kilobyte count: "+ bitmap.getByteCount() / 1024);
        // we succesfully loaded bitmap
        imageView.setTag(true);
        imageView.setImageBitmap(bitmap);

        // make BitmapWorkerTask reference null again.
        bmwt = null;
    }

编辑:刚刚修复了一些代码以使其更易于阅读

于 2013-06-13T05:52:00.787 回答