setAnimation在LongClickListener中不起作用

时间:2016-07-23 05:30:51

标签: android animation cardview

为什么我的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>

我需要摇动的cardview布局是:

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

我只想在用户Longclicks cardview元素时,卡片应该开始摇动,以便在长时间触摸它时继续通知它。

1 个答案:

答案 0 :(得分:2)

尝试改变这一点:

breakfast.startAnimation(shake);

对此:

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Bootstrap</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>


<form closs="col-sm-3">
	
<div class="form-group">
<label for="email">Email </label> 
<input type="email"  placeholder="Enter email" class="form-control"></input>

</div>

<div class="form-group">
<label for="password">Password </label>	
<input type="password"  placeholder="Enter email" class="form-control"></input>

</div>


<button class="btn btn-primary">Login</button>
</form>


<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
</body>
</html>

您正在设置动画但未启动它。