[英]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
您發布的動畫代碼似乎工作正常。 您遇到的問題是imageview本身及其在屏幕上的位置。
您說過,您的imageview不會占用全部空間並在其后留空白,因此要解決此問題,只需使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();
}
使用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組合動畫
步驟1:在res的 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:
現在計算動畫時間,並在右側的imageview設置動畫時停止動畫並將其設置到屏幕中。 為此,您可以在啟動動畫時使用用戶處理程序。 線程的run()方法用於獲取動畫的時間。 謝謝 ;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.