简体   繁体   English

.2秒延迟,单击物理设备上的按钮,但不单击仿真器

[英].2 second delay clicking Button on physical device but not emulator

EDIT 2: This happens on my physical Samsung Galaxy S7 Edge running API 23 and not my emulator, Nexus 5X running API 23. I have another physical device which runs Android 5 which animates but not very smoothly. 编辑2:这发生在运行API 23的物理三星Galaxy S7 Edge上,而不是运行API 23的仿真器Nexus 5X上。我有另一台运行Android 5的物理设备,该动画具有动画效果,但不是很平稳。 The Nexus 5X API 22 has the same problem as my physical device running API 23. Any ideas why? Nexus 5X API 22与运行API 23的物理设备存在相同的问题。为什么有想法?

Not entirely sure what's going on but when I tap on my Button , I get a .2 second delay before I see the logging for it or the animation begins. 不能完全确定发生了什么,但是当我点击Button ,出现0.2秒的延迟,然后才能看到日志记录或动画开始。 If I hold click, I visually see the delay as the button background lags and animates. 如果按住Click,当按钮背景滞后并动画时,我会在视觉上看到延迟。

I get no problem when I don't set the style to Borderless or if I don't have the backgrounds set to ?android:attr/selectableItemBackground . 当我没有将样式设置为Borderless或没有将背景设置为?android:attr/selectableItemBackground时,我没有问题。 Any idea what's causing this issue? 知道是什么引起了这个问题吗?

<?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>

Fragment : 片段

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");
}
}

Logs: 日志:

As you can see, sometimes there is a 200ms delay particularly in the final 2 taps, sometimes there isn't. 如您所见,有时会有200ms的延迟,特别是在最后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

If I am unable to set these, what alternative can I use to emulate a button click? 如果无法设置这些值,可以使用什么替代方法来模拟按钮单击? A ripple is ideal as it's Android default. 涟漪是理想的选择,因为它是Android的默认设置。

EDIT: I have tried to set an animation on the buttons by using: btnYes.animate().scaleX(.85f).scaleY(.85f).setDuration(175); 编辑:我试图通过使用btnYes.animate().scaleX(.85f).scaleY(.85f).setDuration(175);在按钮上设置动画btnYes.animate().scaleX(.85f).scaleY(.85f).setDuration(175); and I don't see the animation. 我看不到动画。 I see a delay and then it changes. 我看到延迟,然后改变。

I have also tried changing the buttons to TextView s with the above change and I still see no animation, just lag. 我也尝试通过上述更改将按钮更改为TextView ,但仍然看不到动画,只是滞后。

The background that I was using was placed in res/drawable . 我正在使用的背景放置在res/drawable As soon as I separated it out into the respective drawable folders, all of the lag stopped. 一旦将其分离到各个可绘制的文件夹中,所有的滞后就停止了。

I added a ViewPager to see if that would lag on animation and it did, which then pointed me to this answer . 我添加了一个ViewPager来查看它是否会滞后于动画,并且确实如此,然后我将其指向该答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM