编辑2:这发生在运行API 23的物理三星Galaxy S7 Edge上,而不是我的模拟器,运行API 23的Nexus 5X。我有另一个运行Android 5的物理设备,动画效果不是很顺利。 Nexus 5X API 22与运行API 23的物理设备存在同样的问题。任何想法为什么?
不完全确定发生了什么,但是当我点击我的Button
时,在看到日志记录或动画开始之前,我得到了0.2秒的延迟。如果我按住了,我会在视觉上看到延迟,因为按钮背景会滞后并动画。
当我没有将样式设置为无边框或者我没有将背景设置为?android:attr/selectableItemBackground
时,我没有遇到任何问题。知道是什么导致了这个问题吗?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="112dp"
android:maxLines="3"
android:text="Hello World"
android:textColor="@android:color/white"
android:textSize="30sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/btnYes"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_below="@+id/tv1"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="Yes"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="@android:color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@+id/tvAreYouAStudent"
/>
<Button
android:id="@+id/btnNo"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_below="@+id/tv1"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="No"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="@android:color/white"
app:layout_constraintLeft_toLeftOf="@+id/guideline"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv1"/>
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5026042"
tools:layout_editor_absoluteX="193dp"
tools:layout_editor_absoluteY="0dp"/>
</android.support.constraint.ConstraintLayout>
片段:
public class StudentFragment extends Fragment
{
public final String TAG = "StudentFragment";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment, container, false);
ButterKnife.bind(this, rootView);
Application.component().inject(this);
return rootView;
}
@OnClick(R.id.tvYes)
public void yesTapped()
{
tvYes.animate().scaleX(.85f).scaleY(.85f).setDuration(175);
tvNo.setScaleX(1);
tvNo.setScaleY(1);
Log.e(TAG, "Yes tapped");
}
@OnClick(R.id.tvNo)
public void noTapped()
{
tvNo.animate().scaleX(.85f).scaleY(.85f).setDuration(175);
tvYes.setScaleX(1);
tvYes.setScaleY(1);
Log.e(TAG, "No tapped");
}
}
日志:
正如您所看到的,有时会有200毫秒的延迟,特别是在最后的2个水龙头中,有时候没有。
12-20 22:57:52.266 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-20 22:57:52.366 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 1
12-20 22:57:52.576 20549-20549/com E/StudentFragment: Yes tapped
12-20 22:57:53.296 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-20 22:57:53.446 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 1
12-20 22:57:53.606 20549-20549/com E/StudentFragment: Yes tapped
12-20 22:57:54.656 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-20 22:57:54.716 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 1
12-20 22:57:54.856 20549-20549/com E/StudentFragment: Yes tapped
12-20 22:57:55.706 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-20 22:57:55.766 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 1
12-20 22:57:55.796 20549-20549/com E/StudentFragment: Yes tapped
12-20 22:57:57.116 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-20 22:57:57.166 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 1
12-20 22:58:00.496 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-20 22:58:00.616 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 1
12-20 22:58:00.826 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-20 22:58:00.826 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 1
12-20 22:58:00.826 20549-20549/com E/StudentFragment: Yes tapped
12-20 22:58:01.656 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-20 22:58:01.746 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 1
12-20 22:58:01.766 20549-20549/com E/StudentFragment: Yes tapped
12-20 22:58:02.756 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-20 22:58:02.836 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 1
12-20 22:58:03.166 20549-20549/com E/StudentFragment: Yes tapped
12-20 22:58:04.296 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 0
12-20 22:58:04.396 20549-20549/com D/ViewRootImpl: ViewPostImeInputStage processPointer 1
12-20 22:58:04.606 20549-20549/com E/StudentFragment: Yes tapped
如果我无法设置这些,我可以使用哪种方法来模拟按钮点击?纹波是理想的,因为它是Android的默认设置。
编辑:我尝试使用:btnYes.animate().scaleX(.85f).scaleY(.85f).setDuration(175);
在按钮上设置动画,但我看不到动画。我看到了延迟,然后又发生了变化。
我也尝试使用上述更改将按钮更改为TextView
,我仍然看不到动画,只是滞后。
答案 0 :(得分:0)
我使用的背景放在res/drawable
中。一旦我将它分成相应的可绘制文件夹,所有滞后都会停止。
我添加了一个ViewPager,看看它是否会落后于动画,而且确实是this answer。