5

我有这段代码来交叉淡入 ImageView 的背景:

private void setWithCrossfade(Bitmap bitmap) {
    //create drawable vector
    Drawable backgrounds[] = new Drawable[2];
    //getting first image
    backgrounds[0] = mImageView.getDrawable();
    backgrounds[1] = new BitmapDrawable(mImageView.getResources(), bitmap);

    TransitionDrawable transitionDrawable = new TransitionDrawable(backgrounds);
    transitionDrawable.setCrossFadeEnabled(true);

    mImageView.setAdjustViewBounds(false);
    mImageView.setImageDrawable(transitionDrawable);
    //it is needed to reset scale type
    mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    //start crossfading
    transitionDrawable.startTransition(250);
}

ImageView在 XML 布局scaleType中设置为。centerCrop但是,当交叉淡入淡出完成时,新位图设置为fitXY. 在其他线程中,他们说可以通过重置scaleType,但它也不起作用。就内存而言,调整位图大小并不是一个合适的解决方案。是否有任何解决方法来实现这一点?非常感谢。

PS:请不要建议交叉淡入淡出 2 ImageViews,或使用 ViewSwitcher 谢谢。

4

1 回答 1

3

我有同样的问题。问题是aTransitionDrawable需要两个相同大小的drawable。如果它不是相同的大小,它会延伸到第一个图像的大小。毕加索已经解决了这个问题。所以我从 Picasso 那里拿了PicassoDrawable,首先设置了占位符,然后是位图。

PicassoDrawable.setPlaceholder(imageView, placeholderDrawable);
PicassoDrawable.setBitmap(imageView, context, bitmap);

您可能需要将类和那些方法设置为公开。我还删除了一些仅与毕加索相关的方法。现在它应该使用不同的图像尺寸正确淡入淡出。

于 2015-06-22T09:19:58.627 回答