动画 alpha(在 SurfaceView 下)不工作
Animation alpha (under SurfaceView) not working
我想在用户点击捕获按钮时制作类似淡入淡出的动画。
当我在 animatedView 和 android:fromAlpha="1.0", android:toAlpha="0.0" 上设置 alpha 1.0 时,它起作用了,但我需要反转它。
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<SurfaceView
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<View
android:id="@+id/animated_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:alpha="0.0"/>
<Button
android:id="@+id/button_capture"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
这是我的动画xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toAlpha="1.0"
/>
</set>
我就是这样 运行 的:
animClick = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.click);
@OnClick(R.id.button_capture)
void onCaptureClick() {
camera.takePicture(null, null, jpegCallback);
animatedView.startAnimation(animClick);
}
什么也没发生,我做错了什么?
设置动画不是问题你没有正确安排你的布局
请查看布局,前提是它与您想要的不同,但您将能够理解如何修复它。
你为每件事都设置了高度 ==> match_parent 可能这就是你无法为视图设置动画的原因。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<SurfaceView
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<View
android:id="@+id/animated_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ic_launcher"
/>
<Button
android:id="@+id/button_capture"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
您可以使用更新后的 xml 在这里工作正常 !!!
我想在用户点击捕获按钮时制作类似淡入淡出的动画。 当我在 animatedView 和 android:fromAlpha="1.0", android:toAlpha="0.0" 上设置 alpha 1.0 时,它起作用了,但我需要反转它。 这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<SurfaceView
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<View
android:id="@+id/animated_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:alpha="0.0"/>
<Button
android:id="@+id/button_capture"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
这是我的动画xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toAlpha="1.0"
/>
</set>
我就是这样 运行 的:
animClick = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.click);
@OnClick(R.id.button_capture)
void onCaptureClick() {
camera.takePicture(null, null, jpegCallback);
animatedView.startAnimation(animClick);
}
什么也没发生,我做错了什么?
设置动画不是问题你没有正确安排你的布局 请查看布局,前提是它与您想要的不同,但您将能够理解如何修复它。
你为每件事都设置了高度 ==> match_parent 可能这就是你无法为视图设置动画的原因。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<SurfaceView
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<View
android:id="@+id/animated_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ic_launcher"
/>
<Button
android:id="@+id/button_capture"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
您可以使用更新后的 xml 在这里工作正常 !!!