2016-10-27 7 views
-3

指が特定の領域にあるときに、どのようにアクションを実行できますか? ButtonやTextViewなどを使用したくありません。私はAndroidのメーカーで働いています特定の領域でアクションを実行していますか?

public void onTouch { 
if [finger is in set x and y position]{ 
player.start 
} else if[finger is not in a set positon] { 
player.release(); 
} 

: 私はこのような何かを作ることができます。
ご協力いただければ幸いです。

+3

は、あなたがそれをここに投稿する前にこれをGoogleで検索していますか?はいの場合は、試したコードを貼り付けてください。 – Manishika

+1

Androidデベロッパーガイドで詳しく説明しています。https://developer.android.com/guide/topics/ui/ui-events.html – vzsg

+0

あなたのやりたいことを説明してください。 「指が特定の領域にあるときにどのように行動を取ることができますか」という意味はどういう意味ですか?その領域にはレイアウトが含まれています。あなたは何をしたいのですか? – LoveAndroid

答えて

0

もちろん可能です。 ActivityonTouch()メソッドをオーバーライドします。

public void onTouch(MotionEvent event) { 
    int left, top, right, bottom; // Don't forget to initialize with your desired coordinates. 

    Rect rectangle = new Rect(left, top, right, bottom); 

    if (rectangle.contains(event.getRawX(), event.getRawY())) { 
     // Perform your action here. 
    } 

    super.onTouch(event); 
} 
0

任意のViewまたはViewGroupまたはそのサブクラスでonTouchListenerを使用します。

ここでは簡単な例では、タッチアクションを追跡する方法を示すことです。

MainActivity:

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.RelativeLayout; 

public class MainActivity extends AppCompatActivity { 
    private static final String TAG = MainActivity.class.getSimpleName(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     RelativeLayout layout = (RelativeLayout) findViewById(R.id.rootView); 
     layout.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       float x = event.getX(); 
       float y = event.getY(); 
       switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         // action when on touch action is down pressed 
         Log.d(TAG, "ACTION_DOWN, location:x" + x + ",y:" + y); 
         return true; 
        case MotionEvent.ACTION_MOVE: 
         // action when on touch action is moving 
         Log.d(TAG, "ACTION_MOVE, location:x" + x + ",y:" + y); 
         return true; 

        case MotionEvent.ACTION_UP: 
         // action when on touch action is up 
         Log.d(TAG, "ACTION_UP, location:x" + x + ",y:" + y); 
         return true; 
       } 
       return false; 
      } 
     }); 
    } 
} 

activity_main:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:id="@+id/rootView" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


</RelativeLayout> 
関連する問題