如何应用点击监听器?

时间:2012-12-13 12:01:16

标签: android page-curl

我使用此链接使用页面卷曲来创建应用程序: -

    https://github.com/harism/android_page_curl.

在该链接中使用bitmap实现页面卷曲。但我的要求是卷曲到View,为此我转换        查看(LinearLayout)到位图并在子视图上设置onclicklistener,有关详细说明,请参阅        附图        page curl work  properly unfortunatly onclick listener is not working please anyone suggest me

我的代码如下: -

public static LinearLayout createProgrammeView(final Context context,
        int width, int height, String title, String time) {


    // Upper layout of screen
    LinearLayout objmainlayout = new LinearLayout(context);

    if (height >= 320) {
        objmainlayout.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT, (height -100)));
        Log.e("chectttttttttttlayout",""+(height-71));
    } else {
        objmainlayout.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT, (height - 90)));
    }
    objmainlayout.setBackgroundColor(Color.WHITE);
    objmainlayout.setOrientation(LinearLayout.VERTICAL);
    objmainlayout.setPadding(10, 0, 10,0);


    for (int mindex = 0; mindex <3; mindex++)
    {
        RelativeLayout.LayoutParams objparams;
        // Layout Root View (RelativeLayout)

        RelativeLayout objrelativeinnerlayout = new RelativeLayout(context);
        if (height >= 320) 
        {
            objparams = new RelativeLayout.LayoutParams(width-20,
                    ((height - 71) / 3) - 10);
            Log.e("chectt33333tlayout",""+(((height - 71) / 3) - 10));
        } else {
            objparams = new RelativeLayout.LayoutParams(width,
                    ((height - 90) / 3) - 10);
        }
        //objparams.topMargin=10;
        objrelativeinnerlayout.setLayoutParams(objparams);
        // rlv.setBackgroundResource(R.drawable.sss);
        // rlv.setBackgroundResource(R.drawable.sss);


        ImageView objrow1img1 = new ImageView(context);
        if (height >= 320) {
            objrow1img1.setLayoutParams(new RelativeLayout.LayoutParams(
                    (width - 30) / 2,((height - 71) / 3) - 10));
        }
        objrow1img1.setScaleType(ScaleType.FIT_XY);

        RelativeLayout.LayoutParams objlp = (RelativeLayout.LayoutParams) objrow1img1.getLayoutParams();

        objlp.topMargin=10;
        objlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        objrow1img1.setId(1);
        objrow1img1.setBackgroundColor(Color.RED);

        if (mindex == 0) {
            objrow1img1.setImageResource(R.drawable.fblogin);
            objrow1img1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(context,"onclick",Toast.LENGTH_LONG).show();
                    Log.e("check click","good");
                    //Intent objIntent = new Intent(context,
                            //FacebookAlbumList.class);
                    //objcoContext.startActivity(objIntent);
                }
            });
        }
        else {/*
            if (data != null
                    && data.size() > saveindex
                    && data.get(saveindex) != null
                    && data.get(saveindex).get(0) != null
                    && data.get(saveindex).get(0).getImagepath() != null) {
                objrow1img1.setVisibility(View.VISIBLE);
                System.gc();
                decodeBitMap(data.get(index).get(0).getImagepath(),
                        objrow1img1);
            } else {
                objrow1img1.setVisibility(View.INVISIBLE);
            }
        */}
        objrelativeinnerlayout.addView(objrow1img1);

        ImageView objrow1img2 = new ImageView(context);
        objrow1img2.setLayoutParams(new RelativeLayout.LayoutParams(
                (width - 30) / 2,((height - 71) / 3) - 10));
        objrow1img2.setScaleType(ScaleType.FIT_XY);
        objrow1img2.setBackgroundColor(Color.RED);

        RelativeLayout.LayoutParams objlrelativelayoutparam = (RelativeLayout.LayoutParams) objrow1img2
                .getLayoutParams();
        objlrelativelayoutparam.setMargins(10, 10, 0, 0);
        objlrelativelayoutparam.addRule(RelativeLayout.RIGHT_OF,1);

        objrelativeinnerlayout.addView(objrow1img2);

        objmainlayout.addView(objrelativeinnerlayout);

    }
    return objmainlayout;
}

public static Bitmap loadBitmapFromView(LinearLayout v) {
    v.measure(MeasureSpec.makeMeasureSpec(v.getLayoutParams().width,
            MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
            v.getLayoutParams().height, MeasureSpec.EXACTLY));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
            Bitmap.Config.RGB_565);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;
}

在这里,我将Myview代码转换为bitmap.Problem是我如何获得onclickListener? 请有人建议我提前谢谢。

1 个答案:

答案 0 :(得分:4)

我遇到了类似的问题。为了使它工作,我有一个父布局(contentContainer),其中包含2个子视图:

  • 卷曲时转换为位图的内容视图
  • CurlView(OpenGL Surface)

我必须将onClickListener设置为父布局以捕获事件并发送或不将它们发送到CurlView。当事件发送到CurlView时,CurlView被带到前面(使用bringToFront()方法)。否则,内容视图将被置于最前面。

要确定是否将事件发送到CurlView,我使用一种方法,如果点击位于页面卷曲区域内(aprox。屏幕的1/6,两侧),则返回true。另外,我有一个手势和比例检测器来检测singleTap和longPress,这是我们用户交互所需要的。当捕获singleTap或longPress时,位置(x,y)告诉我用户触摸的位置,这样我就可以启动一些活动,显示上下文菜单等等。

一些示例代码:

    contentContainer.setOnClickListener(null); // DO NOT REMOVE THIS, IT'S A WORKAROUND
    contentContainer.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            mScaleDetector.onTouchEvent(event);

            int x = (int)event.getX();
            int y = (int)event.getY();

            boolean isMoving = event.getAction() == MotionEvent.ACTION_MOVE;
            boolean isTouchDown = event.getAction() == MotionEvent.ACTION_DOWN;
            boolean isTouchUp = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
            boolean pageCurlArea = isPageCurlArea(x, y);

            boolean sendEventToCurlView = false;

            if (isTouchDown && pageCurlArea) {
                downInPageArea = true;
                sendEventToCurlView = true;
            } else if (downInPageArea && isMoving) {
                sendEventToCurlView = true;
            } else if (downInPageArea && isTouchUp) {
                downInPageArea = false;
                sendEventToCurlView = true;
            } else if (!downInPageArea && isMoving) {
                return false;
            }

            if (downInPageArea && isMoving && !curlViewIsOnTop) {
                bringCurlViewToFront();
            }

            if (sendEventToCurlView) {
                MotionEvent mEvent = MotionEvent.obtain(event);
                mCurlView.onTouch(v, mEvent);
            }

            return !sendEventToCurlView;
        }
    });

这是一项正在进行中的工作,因为我需要将其修改为不使用“页面卷曲区域”来区分“卷曲事件”和其他事件(singleTap,longPress ...),我计划使用它如果捕获了事件,则返回手势和比例检测器,并根据结果继续将事件发送到CurlView。这对于在1/6页卷曲区域内执行“轻拍”和“长按”是必要的。

希望它有所帮助。