- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有 ImageView。在 onPostExecute 中加载位图后,我调用 setImageDrawable 来用图像填充 imageView(我使用 CircleImageView)。但是 TransitionDrawable 只显示第一张图片并且没有执行任何交易。我如何让这个工作?提前致谢。
private void setImageDrawable(ImageView imageView, Bitmap bitmap) {
if (mFadeInBitmap) {
BitmapDrawable drawable = null, placeholder = null;
if (bitmap != null) {
drawable = new BitmapDrawable(mResources, bitmap);
placeholder = new BitmapDrawable(mResources, mPlaceHolderBitmap);
}
final TransitionDrawable td =
new TransitionDrawable(new Drawable[] {
placeholder,
drawable,
});
imageView.setImageDrawable(td);
td.startTransition(200);
} else {
imageView.setImageBitmap(bitmap);
}
}
更新:我在 CircleImageView 文档中找到了为什么它对我不起作用的解决方案:“将 TransitionDrawable 与 CircleImageView 一起使用无法正常工作并导致图像困惑。”(https://github.com/hdodenhof/CircleImageView ).
那么有人可以建议我如何使动画淡入淡出的圆形图像正常工作吗?
更新 2:我发现在两个圆形图像之间实现平滑过渡的解决方法是淡出第一个,然后使用动画淡入第二个并延迟运行。这是代码示例。
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="100"
android:interpolator="@android:anim/accelerate_interpolator"
/>
</set>
fade_in.xml 几乎相同,只是您按位置更改了 fromAlpha 和 toAlpha。之后将这些动画应用到你的圆形 ImageView 中,就像这样:
Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.fade_out);
imageView.startAnimation(anim);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(value);//changing to different image ,here you will set image that you have loaded
Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
imageView.startAnimation(anim);
}
}, 100);
希望对您有所帮助。
最佳答案
我自己尝试了以下代码,可以说它有效并且转换非常明显:
private void setImageDrawable(ImageView imageView) {
Bitmap bitmap1 = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Bitmap bitmap2 = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
new Canvas(bitmap1).drawRect(0, 0, 100, 100, mWhitePaint);
new Canvas(bitmap2).drawRect(0, 0, 100, 100, mBlackPaint);
BitmapDrawable drawable1 = new BitmapDrawable(getResources(), bitmap1);
BitmapDrawable drawable2 = new BitmapDrawable(getResources(), bitmap2);
TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[] {drawable1, drawable2});
imageView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(3000);
}
请检查您的 bitmap
不是 null
并且它与 mPlaceHolderBitmap
不同(它也不应该是 null
)。也可以尝试将过渡持续时间设置为 3 秒,例如,因为 200 毫秒可能太快以至于看不到过渡本身。
关于android - 是否有支持 TransitionAnimation 的 CircleImageView 替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31479532/
我有 ImageView。在 onPostExecute 中加载位图后,我调用 setImageDrawable 来用图像填充 imageView(我使用 CircleImageView)。但是 Tr
我是一名优秀的程序员,十分优秀!