2012-05-22 24 views

回答

0

這樣做的一種方法就是在您的活動中使用OnTouchListener。您可以準確檢測屏幕上觸摸的位置。

import android.app.Activity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.LinearLayout; 
import android.widget.Toast; 

public class AndroidTestActivity extends Activity implements OnTouchListener { 
    LinearLayout main; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    main = (LinearLayout) findViewById(R.id.main_layout); 
    main.setOnTouchListener(this); // you need to set the touch listener for your view. And every element around the detection area. 
    } 

    public boolean onTouch(View v, MotionEvent e) { 
    if(e.getX() <= main.getWidth()/5) { 
     Toast.makeText(this, "In the %20..", Toast.LENGTH_SHORT).show(); 
     return true;  
    } 

    return false; 
    } 
} 

您還需要標識主佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
+0

謝謝您的回答,但它似乎並沒有爲我工作。即使我沒有設置任何條件,但我點擊佈局的任何區域動畫沒有開始 –

+0

20%區域我仍然有其他組件,但是我點擊20%區域內的組件,它會做組件自己的功能,而不是開始動畫 –

+0

如果您有任何其他佈局,圖像視圖或任何其他地方,你需要添加偵聽器與setOnTouchListener他們.. – Jan

相關問題