如何从左侧动画 ImageView 以填充屏幕?
How to animate an ImageView from left to fill the screen?
我在 ImageView 中设置了一个图像,我想对其进行动画处理,使屏幕上最初不显示任何内容,图像从左侧进入并向右移动,最后填满屏幕
在我检查的代码中,图像从左侧进入但没有填满屏幕并在其后面留有空白
ObjectAnimator transAnimation=
ObjectAnimator.ofFloat(imageView,"translationX",-100,100);
transAnimation.setDuration(3000);//set duration
transAnimation.start();//start animation
使用 TransistionManager 怎么样?
val slideRight= Slide()
slideRight.slideEdge = Gravity.RIGHT
slideRight.mode = Slide.MODE_IN
slideRight.addTarget(logoImageView)
slideRight.duration = ANIMATION_DURATION
TransitionManager.beginDelayedTransition(parentContainer, slideRight)
logoImageView.visibility = VISIBLE
这是我在 Kotlin 中的工作代码片段
也可以使用TransitionSet组合动画
第一步:在res[=28=的anim目录下创建left_to_right_anim文件]
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="800" />
</set>
第 2 步:
//your image view
imageView = (ImageView) findViewById(R.id.img);
//name of animation from anim directory
animSlide = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.left_to_right_anim);
// Start the animation like this
imageView.startAnimation(animSlide);
第 3 步:
现在计算动画时间,当您的图像视图在右侧 侧停止 动画时,将其设置到您的屏幕中。
为此,您可以在开始动画时使用用户处理程序。
Thread 的 运行() 方法用于获取动画的时间。
谢谢;)
您发布的动画代码似乎运行良好。您遇到的问题是 imageview 本身及其占据屏幕的位置。
你说你的图像视图没有完全 space 而在它后面留下空白 space,
所以要修复它,只需让您的 imageview width = match_parent。如果仍然不起作用,则添加 scaleType=centerCrop
更新:
将此代码添加到您的 onCreate()
imageView.post(new Runnable() {
@Override
public void run() {
startImageAnimation();
}
});
private void startImageAnimation() {
ObjectAnimator animation = ObjectAnimator.ofFloat(imageView, "translationX",-(imageView.getWidth()), 0);
animation.setDuration(1100);
animation.start();
}
我在 ImageView 中设置了一个图像,我想对其进行动画处理,使屏幕上最初不显示任何内容,图像从左侧进入并向右移动,最后填满屏幕
在我检查的代码中,图像从左侧进入但没有填满屏幕并在其后面留有空白
ObjectAnimator transAnimation=
ObjectAnimator.ofFloat(imageView,"translationX",-100,100);
transAnimation.setDuration(3000);//set duration
transAnimation.start();//start animation
使用 TransistionManager 怎么样?
val slideRight= Slide()
slideRight.slideEdge = Gravity.RIGHT
slideRight.mode = Slide.MODE_IN
slideRight.addTarget(logoImageView)
slideRight.duration = ANIMATION_DURATION
TransitionManager.beginDelayedTransition(parentContainer, slideRight)
logoImageView.visibility = VISIBLE
这是我在 Kotlin 中的工作代码片段
也可以使用TransitionSet组合动画
第一步:在res[=28=的anim目录下创建left_to_right_anim文件]
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="800" />
</set>
第 2 步:
//your image view
imageView = (ImageView) findViewById(R.id.img);
//name of animation from anim directory
animSlide = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.left_to_right_anim);
// Start the animation like this
imageView.startAnimation(animSlide);
第 3 步:
现在计算动画时间,当您的图像视图在右侧 侧停止 动画时,将其设置到您的屏幕中。 为此,您可以在开始动画时使用用户处理程序。 Thread 的 运行() 方法用于获取动画的时间。 谢谢;)
您发布的动画代码似乎运行良好。您遇到的问题是 imageview 本身及其占据屏幕的位置。
你说你的图像视图没有完全 space 而在它后面留下空白 space, 所以要修复它,只需让您的 imageview width = match_parent。如果仍然不起作用,则添加 scaleType=centerCrop
更新:
将此代码添加到您的 onCreate()
imageView.post(new Runnable() {
@Override
public void run() {
startImageAnimation();
}
});
private void startImageAnimation() {
ObjectAnimator animation = ObjectAnimator.ofFloat(imageView, "translationX",-(imageView.getWidth()), 0);
animation.setDuration(1100);
animation.start();
}