2014-09-06 71 views
1

我对我的四个TextView使用滑动向上/向下动画。以下是我的XML向上/向下滑动后的动画Textview onClickListener不起作用

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrollView1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/bg" > 

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.0" > 

     <TextView 
      android:id="@+id/textViewblue" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="25dp" 
      android:background="@drawable/blue" 
      android:gravity="center" 
      android:text="blue" /> 

     <TextView 
      android:id="@+id/textVieworange" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:layout_centerInParent="true" 
      android:layout_marginRight="25dp" 
      android:background="@drawable/orange" 
      android:gravity="center" 
      android:text="orange" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.0"> 

     <TextView 
      android:id="@+id/textViewpink" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerInParent="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="25dp" 
      android:background="@drawable/pink" 
      android:gravity="center" 
      android:text="pink" /> 

     <TextView 
      android:id="@+id/textViewgreen" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerInParent="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginRight="25dp" 
      android:background="@drawable/green" 
      android:gravity="center" 
      android:text="green" /> 
    </RelativeLayout> 
</LinearLayout> 

和以下是我的课

public class HomeScreenActivity extends Activity implements OnClickListener { 

    TextView textViewblue, textVieworange, textViewpink, 
      textViewgreen; 
    Animation mAnimation_up, mAnimation_down; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home_screen); 
     mAnimation_up = new TranslateAnimation(
       TranslateAnimation.RELATIVE_TO_SELF, 0f, 
       TranslateAnimation.RELATIVE_TO_SELF, 0f, 
       TranslateAnimation.RELATIVE_TO_SELF, 1.6f, 
       TranslateAnimation.RELATIVE_TO_SELF, 0f); 
     mAnimation_up.setDuration(1000); 
     mAnimation_up.setRepeatCount(-1); 
     mAnimation_up.setRepeatMode(Animation.REVERSE); 
     mAnimation_up.setInterpolator(new LinearInterpolator()); 

     mAnimation_down = new TranslateAnimation(
       TranslateAnimation.RELATIVE_TO_SELF, 0f, 
       TranslateAnimation.RELATIVE_TO_SELF, 0f, 
       TranslateAnimation.RELATIVE_TO_SELF, 0f, 
       TranslateAnimation.RELATIVE_TO_SELF, 1.6f); 
     mAnimation_down.setDuration(1000); 
     mAnimation_down.setRepeatCount(-1); 
     mAnimation_down.setRepeatMode(Animation.REVERSE); 
     mAnimation_down.setInterpolator(new LinearInterpolator()); 
     initialize(); 
    } 

    private void initialize() { 

     textViewblue = (TextView) findViewById(R.id.textViewblue); 
     textVieworange = (TextView) findViewById(R.id.textVieworange); 
     textViewpink = (TextView) findViewById(R.id.textViewpink); 
     textViewgreen = (TextView) findViewById(R.id.textViewgreen); 

     textViewblue .setOnClickListener(this); 
     textVieworange .setOnClickListener(this); 
     textViewpink .setOnClickListener(this); 
     textViewgreen .setOnClickListener(this); 

     textViewblue .setAnimation(mAnimation_down); 
     textViewpink .setAnimation(mAnimation_down); 
     textVieworange .setAnimation(mAnimation_up); 
     textViewgreen .setAnimation(mAnimation_up); 
    } 



    @Override 
    public void onClick(View v) { 
     Intent intentmenu; 
     switch (v.getId()) { 
     case R.id.textViewblue: 
      Toast.makeText(this, "blue", Toast.LENGTH_LONG).show(); 
      break; 
     case R.id.textVieworange: 
      Toast.makeText(this, "orange", Toast.LENGTH_LONG).show(); 
      break; 
     case R.id.textViewpink: 
      Toast.makeText(this, "pink", Toast.LENGTH_LONG).show(); 
      break; 
     case R.id.textViewgreen: 
      Toast.makeText(this, "reeng", Toast.LENGTH_LONG).show(); 
      break; 
     } 
    } 

} 

的问题是,当在动画仅在其XML位置开始onClickListener作品INT位置任意TextView滑动装置而不是更新的位置。 请给我建议解决方案,我如何检测更新后的位置以检测TextView上的onClickListener。

谢谢。

回答

1

也许这样做有点复杂。正如你使用View animation system,这种行为是在预期之下。虽然视图被翻译到其他地方,但他们只是在 绘制那里。从系统的角度来看,它们实际上始终处于您在xml文件中定义的原始位置。如果你想在新地点点击事件,你应该使用新的property animation system

+0

感谢您的建议,你可以给我任何简单的参考链接 – Akshay 2014-09-09 06:25:17

+0

看到http://android-developers.blogspot.com/2011/05/introducing-viewpropertyanimator.html – suitianshi 2014-09-09 06:30:40

+0

再次感谢... – Akshay 2014-09-09 06:32:51

0

onClickListener不工作,因为视图由于动画而从原始位置移动。为了解决这个问题,在动画下面创建一个相同的布局,并最初将其Gravity设置为GONE,当动画完成时,将其重力视为可见,并将动画布局可见性视为已过。 也要小心,动画的所有id和原始布局不会相同。