2010-08-25 65 views
1

我有一個ListView可以有一個或多個可點擊的項目。當我應用旋轉動畫時,單擊的座標對應於ListView項目的原始位置。例如,縱向模式中一個項目旋轉180度的列表將使項目在屏幕底部倒置,但當單擊屏幕頂部時,該項目會獲得單擊事件。 180度只是我想要移動任意角度的一個例子。 我已經查看了所有的listView屬性,但沒有一個對可點擊的座標沒有任何影響。我會假設willChangeTransformationMatrix會做的伎倆,但它不,也無效或invalidateViews。是否有我忽略的一個屬性,或者我會如何將座標移至正確的位置?安卓 - 點擊旋轉的ListView給出錯誤的座標

由於點擊時

樣本代碼 - 列表項正確突出,旋轉與dpad_center,之後點擊原來的位置時旋轉的項目突出。我試着動畫動畫,animationSet和layoutAnimationController所有相同的結果。

public class ToDoList extends Activity { 
    ListView myListView; 
    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    final ListView myListView = new ListView(this); 

    final ArrayList<String> todoItems = new ArrayList<String>(); 
    todoItems.add(0, "asdf"); 
    todoItems.add(0, "1234"); 

    // Create the array adapter to bind the array to the listview 
    final ArrayAdapter<String> aa; 
    aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,todoItems); 

    // Bind the array adapter to the listview. 
    myListView.setAdapter(aa); 
    setContentView(myListView); 
    myListView.setOnKeyListener(new OnKeyListener() { 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      if (event.getAction() == KeyEvent.ACTION_DOWN) 
      if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) 
      { 
       aa.notifyDataSetChanged(); 
       //myEditText.setText(""); 
       RotateAnimation ranim = new RotateAnimation(0f, 180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
       ranim.setDuration(1000); 
       ranim.setFillAfter(true); 
       ranim.willChangeBounds(); 
       ranim.willChangeTransformationMatrix(); 
       ranim.setInterpolator(new LinearInterpolator()); 
       myListView.startAnimation(ranim); 
       AnimationSet set = new AnimationSet(true); 
       set.addAnimation(ranim); 
       set.willChangeTransformationMatrix(); 
       set.setInterpolator(new LinearInterpolator()); 
       //set.setFillAfter(true); 
       //set.setFillEnabled(true); 
       LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f); 
       controller.setInterpolator(new LinearInterpolator()); 
       myListView.setLayoutAnimation(controller); 
       return true; 
      } 
      return false; 
     } 
     }); 
    } 
} 

回答

1

您正在獲取正確的座標。 RotateAnimation僅影響View的渲染,而不影響其佈局或觸摸事件。

+0

我想當我問到「我將如何將座標移到正確的位置?」通過「正確」我的意思是新近輪換的地方,幾乎任何人都希望他們是(在實際點擊的項目下)。任何想法? 謝謝 – JA1 2010-08-25 03:33:51

+0

@ JA1:第1步是讓你確定如何有一個橫向的ListView,沒有任何動畫。如果你不能在沒有動畫的情況下做到這一點,你也不能用動畫來完成它。由於我沒有意識到你可以有一個橫向的'ListView',我懷疑你需要修改你的UI計劃。 – CommonsWare 2010-08-25 04:36:21

+0

這是非常令人失望的,我曾經看過其他旋轉w/out動畫的方法,但只發現瞭如何去ImageViews。我也嘗試過動畫一個imageView,在屏幕上移動onTouch,並且座標也不跟着它,你必須重新觸摸原始位置重新動畫。這似乎是一個巨大的限制,但我仍然認爲我忽略了一些簡單的東西,或者可能有一些方法來調整座標。我的意思是一個帶有> 10個滾動瀏覽的ListView的ListView會在項目向外滾動時向位置做一些調整,類似於動畫 – JA1 2010-08-25 06:19:12

1

從honeycomb開始,您可以使用新的動畫API,允許您同時執行動畫和真實運動(或旋轉或縮放等)的視圖。

對於老版本的android,動畫只會改變視圖的顯示方式。