首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓: ScrollView onTouch事件截获ImageVIew ontouch Drag

安卓: ScrollView onTouch事件截获ImageVIew ontouch Drag
EN

Stack Overflow用户
提问于 2015-02-04 16:01:13
回答 2查看 1.5K关注 0票数 3

我正在使用scrollview中的imageview控制器。当涉及到实现和测试时,我发现滚动总是重写onTouch事件而不是imageView (dragImage)。

我尝试使用它来启用滚动

代码语言:javascript
复制
scv.setOnTouchListener(null);    

并禁用此功能

代码语言:javascript
复制
scv.setOnTouchListener( new OnTouchListener(){ 
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true; 
    }
});

它显示dragImage只拖动一个很小的移动,而不是随意拖动到我想要的地方。你能告诉我当我触摸ImageView而不是scrollView时,关闭滚动的更好的方法吗?

以下是我的工作:

代码语言:javascript
复制
public static class PlaceholderFragment extends Fragment   {



private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);            
        return fragment;
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View  rootView = inflater.inflate(R.layout.frag_flight_bank, container, false);
        r1  = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt1);

        r2  = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt2);
        r3  = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt3);
        r4  = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt4);
        r5  = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt5);
        rTop  = (RelativeLayout) rootView.findViewById(R.id.rTop);

        scv = (ScrollView) rootView.findViewById(R.id.scrollView1);
        initialise(rootView);

        return rootView;
    }



    private void initialise(View rootView) {
        // TODO Auto-generated method stub

        tvCustom= (TextView) rootView.findViewById(R.id.ttvCustomize);
        tvRename= (TextView) rootView.findViewById(R.id.tvRename);
        tvNormal= (TextView) rootView.findViewById(R.id.tvNorma);
        tvExper= (TextView) rootView.findViewById(R.id.txExper);


    ...
            dragImage = (ImageView) rootView.findViewById(R.id.dragImage);
            //dragImage.setVisibility(View.GONE);

        dragImage.setOnTouchListener(new OnTouchListener(){
            private int _xDelta;
            private int _yDelta;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int X = (int) event.getRawX();
                final int Y = (int) event.getRawY();
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    RelativeLayout.LayoutParams lParams =      (RelativeLayout.LayoutParams) dragImage
                    .getLayoutParams();

                    _xDelta = X - lParams.leftMargin;
                    _yDelta = Y - lParams.topMargin;
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    RelativeLayout.LayoutParams ParamsA = (RelativeLayout.LayoutParams) dragImage
                    .getLayoutParams();
                    ParamsA.leftMargin = X - _xDelta;
                    ParamsA.topMargin = Y - _yDelta;
                    ParamsA.rightMargin = -250;
                    ParamsA.bottomMargin = -250;

                    dragImage.setLayoutParams(ParamsA);
                    break;
                }
                return true;

            }

        });

解决方案:在开头插入(onCreteView)

代码语言:javascript
复制
scv.setOnTouchListener(new View.OnTouchListener() 
        {
            public boolean onTouch(View p_v, MotionEvent p_event) 
            {
                dragImage.getParent().requestDisallowInterceptTouchEvent(false);
                //  We will have to follow above for all scrollable contents
                return false;
            }
        });

在setOnTouchListener中,onTouch方法:

代码语言:javascript
复制
v.getParent().requestDisallowInterceptTouchEvent(true);
EN

回答 2

Stack Overflow用户

发布于 2015-02-04 16:10:28

在您的MotionEvent.ACTION_DOWNMotionEvent.ACTION_MOVE案例中添加以下代码可以解决您的问题:

代码语言:javascript
复制
dragImage.getParent().requestDisallowInterceptTouchEvent(true);
票数 7
EN

Stack Overflow用户

发布于 2015-05-08 20:21:19

我终于让它正常工作了,你还需要在onTouch事件中返回true。下面的代码将缩放您的图像以填充imageview像素宽度。用户可以拖动封面图片来重新定位,您将需要跟踪Y矩阵以供以后显示。ImageView比例类型设置为矩阵。

代码语言:javascript
复制
//declare in activity
private float lastY = 0;

//Added to onCreate of Activity
            if (CoverPic!=null){
            CoverPic.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                     if (event.getAction() == MotionEvent.ACTION_DOWN) {
                         lastY = event.getY();
                         CoverPic.getParent().requestDisallowInterceptTouchEvent(true);
                         return true;
                     } 
                     else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                         float[] values = new float[9];
                         Matrix m = CoverPic.getImageMatrix();
                         m.getValues(values);
                         float y = values[Matrix.MTRANS_Y];
                         float deltaY = lastY - event.getY();                            
                         y -= deltaY;

                         //original height / original width x new width = new height
                         int newwidth = CoverPic.getWidth();
                         float curwidth = CoverPic.getMeasuredWidth();
                         curwidth = CoverPic.getDrawable().getBounds().width();
                         float newscale = newwidth / curwidth;

                         //We need to limit y Max and Min
                         int viewheight = CoverPic.getHeight();
                         float curheight = CoverPic.getDrawable().getBounds().height() * newscale;
                         float limitmove = curheight - viewheight;
                         limitmove = limitmove * -1;
                         if (y<limitmove) y = limitmove;
                         if (y>0) y = 0;                             

                         Matrix matrix = new Matrix();
                         matrix.postScale(newscale, newscale);
                         matrix.postTranslate(values[Matrix.MTRANS_X], y);

                         profile.setCoverOffY(y); //Update profile cover for saving                          
                         CoverPic.setImageMatrix(matrix);                            
                         CoverPic.invalidate();
                         CoverPic.getParent().requestDisallowInterceptTouchEvent(true);
                         return true;
                     } 
                     else if (event.getAction() == MotionEvent.ACTION_UP) {
                         CoverPic.getParent().requestDisallowInterceptTouchEvent(true);
                         return true;
                     }
                     return false;
                }
            }
            );                          
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28316292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档