onTouchEvent如何获取手指下的当前视图

时间:2010-11-19 06:07:50

标签: android

这是我的代码

public class Main extends Activity {  

    @Override  
    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);   
        LinearLayout rLinear=new LinearLayout(this);  
        rLinear.setId(200);  

        for (int c=0;c<5;c++)  
        {  
            ImageView imEdit = new ImageView(this);  
            imEdit.setId(300+c);  
            imEdit.setImageResource(R.drawable.a);  
            imEdit.setLayoutParams(new LayoutParams(36, 36));  

            rLinear.addView(imEdit);
        }
    }  
}  

好的,我有这个代码,我在运行时添加5个ImageView到线性布局,到目前为止一直很好,但我想做的是,当用户将手指滑过其中一个时,我会喜欢改变图像,所以我的问题是,我怎么知道用户手指下的当前图像视图是什么。

感谢。

5 个答案:

答案 0 :(得分:8)

这是我用来在finger下找到第一个非ViewGroup视图。它是递归的并使用getGlobalVisibleRect

private View findViewAtPosition(View parent, int x, int y) {
    if (parent instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup)parent;
        for (int i=0; i<viewGroup.getChildCount(); i++) {
            View child = viewGroup.getChildAt(i);
            View viewAtPosition = findViewAtPosition(child, x, y);
            if (viewAtPosition != null) {
                return viewAtPosition;
            }
        }
        return null;
    } else {
        Rect rect = new Rect();
        parent.getGlobalVisibleRect(rect);
        if (rect.contains(x, y)) {
            return parent;
        } else {
            return null;
        }
    }
}

用法示例:

public boolean onTouchEvent(MotionEvent ev) {
    Log.i(TAG, "View under finger: " + findViewAtPosition(getRootView(), (int)ev.getRawX(), (int)ev.getRawY()));
}

答案 1 :(得分:3)

申请

    rLinear.setOnTouchListener(new OnTouchListener() {@Override
    public boolean onTouch(View v, MotionEvent event) {
        String eventName = "";

        LinearLayout layout = (LinearLayout)v;

        for(int i =0; i< layout.getChildCount(); i++)
        {
            View view = layout.getChildAt(i);


            Rect outRect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());

            if(outRect.contains((int)event.getX(), (int)event.getY()))
            {
                                  Log.d("", view.getId());
            }
        }
    }});

ps:这个代码按钮很乱

答案 2 :(得分:1)

在处理onTouchEvent时调用performClick()以将click事件调度到目标视图

答案 3 :(得分:0)

如果要在触摸图像时设置不同的图形,可以使用StateListDrawables并将ImageView源设置为xml文件,该文件描述在哪些状态下显示哪个图像

http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

e.g。
&lt; selector android =“http://schemas.android.com/apk/res/android”&gt;
&lt; item state_pressed =“true”drawable =“@ drawable / redbutton”&gt;
&lt; item state_focused =“true”drawable =“@ drawable / redbutton”&gt;
&lt; item drawable =“@ drawable / bluebutton”&gt;
&LT; /选择&GT;

答案 4 :(得分:0)

我认为你应该使用Layout inflater ..看看下面给出的代码......

LinearLayout lin_layt = (LinearLayout) findViewById(R.id.list_layout);

final FrameLayout fl[]=new FrameLayout[size];
View ResultList=new View(Result.this);
             ResultList=layoutInflater.inflate(getResources().getLayout(R.layout.result_list), null);
             fl[i] = new FrameLayout(this);
             fl[i] = (FrameLayout) ResultList.findViewById(R.id.FrameLayout_middle);

//                              
                fl[i].setOnTouchListener(new OnTouchListener(){

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch(event.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            fl[x].setBackgroundResource(R.drawable.middlestripselect);
                            return true;
                        case MotionEvent.ACTION_UP:
                            fl[x].setBackgroundResource(R.drawable.middlestrip);




                            return true;
                        }
                        return false;
                    }

                });



             lin_layt.addView(ResultList);