【问题标题】:setAnimation not working in LongClickListenersetAnimation 在 LongClickListener 中不起作用
【发布时间】:2016-11-27 01:40:14
【问题描述】:

为什么我的 setAnimation 在 LongClickListener 中无法正常工作,而在 LongClick 侦听器外部却可以正常工作?

这是我添加动画的java代码

final Animation shake = AnimationUtils.loadAnimation(NavyashActivity.context, R.anim.shake);
final CardView breakfast = (CardView) rootView.findViewById(R.id.breakfastcard);
breakfast.setOnLongClickListener(new View.OnLongClickListener() {
         @Override
        public boolean onLongClick(View v) {
            Log.e("shake long press","coming ");
            // TODO Auto-generated method stub
            breakfast.setAnimation(shake);
            return true;
        }
    });

我的shake.xml也是:

<?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <rotate
            android:duration="70"
            android:fromDegrees="-5"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="5"
            android:repeatMode="reverse"
            android:interpolator="@android:anim/linear_interpolator"
            android:toDegrees="5" />
        <translate
            android:fromXDelta="-10"
            android:toXDelta="10"
            android:repeatCount="5"
            android:repeatMode="reverse"
            android:interpolator="@android:anim/linear_interpolator"
            android:duration="70" />
    </set>

我需要摇动的卡片视图布局是:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:longClickable="true"
    android:clickable="true"    
    android:id="@+id/breakfastcard">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:background="@drawable/framebox">

       <TextView
            android:layout_width="wrap_content"
            android:id="@+id/breakfastheading"
            android:gravity="center_horizontal"
            android:layout_gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"/>

        <TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:id="@+id/breakfastdetails"
           android:layout_margin="2dp"/>
   </LinearLayout>
</android.support.v7.widget.CardView>

我只想当用户长按卡片视图元素时卡片应该开始晃动,以便通知长时间触摸卡片会持续移动。

【问题讨论】:

    标签: android animation cardview


    【解决方案1】:

    尝试改变这一点:

    breakfast.setAnimation(shake);
    

    到这里:

    breakfast.startAnimation(shake);
    

    您正在设置动画但没有启动它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      相关资源
      最近更新 更多