我正在尝试为ImageView设置动画,以便从左到右慢慢显示.当我在解释我想要的东西之前问这个问题时,所以这次我使用HTML/JS创建了所需的效果:
在Android中获得此效果的最佳方法是什么?
我尝试更改scaleType,然后将ScaleAnimation直接应用于该ImageView:
布局:
<ImageView
android:id="@+id/graphImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:background="@android:color/transparent"
android:contentDescription="@string/stroom_grafiek"
/>
Run Code Online (Sandbox Code Playgroud)
Java的:
scale = new ScaleAnimation((float)0,
(float)1, (float)1, (float)1,
Animation.RELATIVE_TO_SELF, (float)0,
Animation.RELATIVE_TO_SELF, (float)1);
scale.setDuration(1000);
graphImage.startAnimation(scale);
Run Code Online (Sandbox Code Playgroud)
但这个stil缩放图像.
我也尝试在FrameLayout中包装ImageView,希望我能为FrameLayout设置动画:
<FrameLayout
android:layout_width="70dp"
android:layout_height="fill_parent"
android:clipChildren="true"">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left|clip_horizontal" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
这仍然会尝试扩展我的ImageView以适应FrameLayout.
所以我的布局中有一个ImageView,当用户滑过后者时我想向右或向左滑动它.我使用TranslateAnimation来翻译ImageView,如下所示.
ImageView logoFocus = (ImageView) findViewById(R.id.logoFocus);
Animation animSurprise2Movement = new TranslateAnimation(logoFocus.getLeft(), logoFocus.getLeft()+150, logoFocus.getTop(), logoFocus.getTop());
animSurprise2Movement.setDuration(1000);
animSurprise2Movement.setFillAfter(true);
animSurprise2Movement.setFillEnabled(true);
logoFocus.startAnimation(animSurprise2Movement);
Run Code Online (Sandbox Code Playgroud)
我已将此代码放在我的"向右滑动"部分,并使用相同的代码但使用getLeft() - 150用于"向左滑动"部分.当我第一次滑动时,它按预期工作,但当我向另一个方向滑动时,ImageView返回到其原始位置,然后向另一个方向滑动,而不是仅滑动到原始位置.
我已经尝试在AnimationListener的onAnimationEnd方法中添加以下内容,我将其设置为动画,但是徒劳无功.
MarginLayoutParams params = (MarginLayoutParams) logoFocus.getLayoutParams();
params.setMargins(logoFocus.getLeft()+150, logoFocus.getTop(), logoFocus.getRight(), logoFocus.getBottom());
logoFocus.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
我也尝试过以下相同的方法,但是这两种方法都没有按预期工作.
((RelativeLayout.LayoutParams) logoFocus.getLayoutParams()).leftMargin += 150;
logoFocus.requestLayout();
Run Code Online (Sandbox Code Playgroud)
有人请帮帮我吗?即使使用了setFillAfter(true)和setFillEnabled(true),动画后的位置似乎也没有变化.有没有使用TranslateAnimation的替代方案?
谢谢你的帮助.:)
android position imageview android-animation translate-animation