2014-09-06 86 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和原始佈局不會相同。