有关不规则图片的点击响应事件和手势滑动闪退的处理d

来源:互联网 发布:网络超市的前景 编辑:程序博客网 时间:2024/04/29 05:32

最近做过一个项目里面的图片效果是这样的

整个页面分成了7块,我的xml结构是这样


我是将图片做成了一部分是实体一部分透明的那种,将他们组合在一起,根据图片的透明与否来判断响应的点击事件。主要用到了setOnTouchListener和setOnClickListner两个监听方法。

废话不多说以下是我的代码:


public class YanQiLakeFragment extends HHYBaseFragment {    private static final java.lang.String TAG = "YanQiLakeFragment";    private ImageView centerImg; //中间    private ImageView top1Img; //1    private ImageView top2Img; //2    private ImageView top3Img; //3    private ImageView bottom1Img;//1    private ImageView bottom2Img;//2    private ImageView bottom3Img;//3    private Bitmap bitmapTop1;//top1的图片    private Bitmap bitmapTop2;//top2的图片    private Bitmap bitmapTop3;//top3的图片    private Bitmap bitmapBottom1;//bottom3的图片    private Bitmap bitmapBottom2;//bottom3的图片    private Bitmap bitmapBottom3;//bottom3的图片    private Bitmap bitmapCenter;//Center的图片    private boolean iv1TF; //是否透明    private boolean iv2TF;    private boolean iv3TF;    private boolean iv4TF ;    private boolean iv5TF;    private boolean iv6TF;    private boolean iv7TF;    private boolean isTop1;//判断是否点击上2左半部分    private boolean isBottom1;    @Nullable    @Override    public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {        rootView = inflater.inflate(R.layout.fragment_yanqilake, container, false);        //初始化控件        initView();        return rootView;    }    private void initView() {        centerImg = (ImageView) rootView.findViewById(R.id.center);        top1Img = (ImageView) rootView.findViewById(R.id.top1);        top2Img = (ImageView) rootView.findViewById(R.id.top2);        top3Img = (ImageView) rootView.findViewById(R.id.top3);        bottom1Img = (ImageView) rootView.findViewById(R.id.bottom1);        bottom2Img = (ImageView) rootView.findViewById(R.id.bottom2);        bottom3Img = (ImageView) rootView.findViewById(R.id.bottom3);        bitmapTop1 = ((BitmapDrawable) (top1Img.getDrawable())).getBitmap();        bitmapTop2 = ((BitmapDrawable) (top2Img.getDrawable())).getBitmap();        bitmapTop3 = ((BitmapDrawable) (top3Img.getDrawable())).getBitmap();        bitmapBottom1 = ((BitmapDrawable) (bottom1Img.getDrawable())).getBitmap();        bitmapBottom2 = ((BitmapDrawable) (bottom2Img.getDrawable())).getBitmap();        bitmapBottom3 = ((BitmapDrawable) (bottom3Img.getDrawable())).getBitmap();        bitmapCenter = ((BitmapDrawable) (centerImg.getDrawable())).getBitmap();        setClickTop1();        setClickTop2();        setClickTop3();        setClickBottom1();        setClickBottom2();        setClickBottom3();        setClickCenter();    }    @Override    public void onResume() {        super.onResume();        centerImg.setImageResource(R.mipmap.ico_youyanqi_center);        top1Img.setImageResource(R.mipmap.ico_youyanqi_top1);        top2Img.setImageResource(R.mipmap.ico_youyanqi_top2);        top3Img.setImageResource(R.mipmap.ico_youyanqi_top3);        bottom1Img.setImageResource(R.mipmap.ico_youyanqi_bottom1);        bottom2Img.setImageResource(R.mipmap.ico_youyanqi_bottom2);        bottom3Img.setImageResource(R.mipmap.ico_youyanqi_bottom3);    }    /**     * 1 门票     * 主要思想:通过onTouch拦截事件,如果当前位置下,imageview所在图片有像素,就传递事件,否则拦截     */    private void setClickTop1() {        top1Img.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                L.d(TAG, "我是雁栖湖上1");                if (iv1TF) {                    L.d(TAG, "我是透明区域");                } else {                    L.d(TAG, "我是实体区域");                    top1();//执行的事件                }            }        });        top1Img.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                int x = (int) event.getX();                int y = (int) event.getY();                //判断点击坐标是否在图片内部,避免 手势滑动屏幕导致闪退                if (x > 0&&x<bitmapTop1.getWidth()&&y>0&&y<bitmapTop1.getHeight()) {                    //判断图片是否透明,当透明设定iv1TFtrue 再点击时间里面处理                    if (bitmapTop1.getPixel(x, y) == 0) {                        L.i("透明区域");                        iv1TF = true;  //透明区域设置true//                        return true;//透明区域返回true  拦截点击事件                    } else {                        L.i("非透明 ");                        iv1TF = false;                    }                }                return false;            }        });    }    /**     * 2 游船     * 主要思想:通过onTouch拦截事件,如果当前位置下,imageview所在图片有像素,就传递事件,否则拦截     */    private void setClickTop2() {        top2Img.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                L.d(TAG, "我是雁栖湖上2");                if (iv2TF) {                    L.d(TAG, "我是透明区域2");                    /**                     *   当点击上2图透明部分时候需判断点击的是左半部分还是右半部分                     *   根据位置设置相应的点击事件                     */                    if (isTop1){                        top1();                    }else {                        top3();                    }                } else {                    L.d(TAG, "我是实体区域2");                    top2();                }            }        });        top2Img.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                int x = (int) event.getX();                int y = (int) event.getY();                if (x<bitmapTop2.getWidth()/2){                    isTop1 = true;                }else {                    isTop1 =false;                }                if (x > 0&&x<bitmapTop2.getWidth()&&y>0&&y<bitmapTop2.getHeight()) {                    if (bitmapTop2.getPixel(x, y) == 0) {                        L.i("透明区域");                        iv2TF = true;  //透明区域设置true//                        return true;//透明区域返回true  拦截点击事件                    } else {                        L.i("非透明 ");                        iv2TF = false;                    }                }                return false;            }        });    }    /**     * 3 自行车租赁     * 主要思想:通过onTouch拦截事件,如果当前位置下,imageview所在图片有像素,就传递事件,否则拦截     */    private void setClickTop3() {        top3Img.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                L.d(TAG, "我是雁栖湖上3");                if (iv3TF) {                    L.d(TAG, "我是透明区域3");                } else {                    L.d(TAG, "我是实体区域3");                    top3();                }            }        });        top3Img.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                int x = (int) event.getX();                int y = (int) event.getY();                if (x > 0&&x<bitmapTop3.getWidth()&&y>0&&y<bitmapTop2.getHeight()) {                    if (bitmapTop3.getPixel(x, y) == 0) {                        L.i("透明区域");                        iv3TF = true;  //透明区域设置true                        return true;//透明区域返回true  拦截点击事件                    } else {                        L.i("非透明 ");                        iv3TF = false;                    }                }                return false;            }        });    }    /**     * 1 实时停车位     * 主要思想:通过onTouch拦截事件,如果当前位置下,imageview所在图片有像素,就传递事件,否则拦截     */    private void setClickBottom1() {        bottom1Img.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                L.d(TAG, "我是雁栖湖下1");                if (iv4TF) {                    L.d(TAG, "我是透明区域4");                } else {                    L.d(TAG, "我是实体区域4");                    bottom1();                }            }        });        bottom1Img.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                int x = (int) event.getX();                int y = (int) event.getY();                if (x > 0&&x<bitmapBottom1.getWidth()&&y>0&&y<bitmapBottom1.getHeight()) {                    if (bitmapBottom1.getPixel(x, y) == 0) {                        L.i("透明区域");                        iv4TF = true;  //透明区域设置true                        return true;//透明区域返回true  拦截点击事件                    } else {                        L.i("非透明 ");                        iv4TF = false;                    }                }                return false;            }        });    }    /**     * 2 图片点击事件     * 主要思想:通过onTouch拦截事件,如果当前位置下,imageview所在图片有像素,就传递事件,否则拦截     */    private void setClickBottom2() {        bottom2Img.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                L.d(TAG, "我是雁栖湖下2");                if (iv5TF) {                    L.d(TAG, "我是透明区域5");                    if (isBottom1){                        bottom1();                    }else {                        bottom3();                    }                } else {                    L.d(TAG, "我是实体区域5");                    bottom2();                }            }        });        bottom2Img.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                int x = (int) event.getX();                int y = (int) event.getY();                if (x<bitmapBottom2.getWidth()/2){                    isBottom1 = true;                }else {                    isBottom1 =false;                }                if (x > 0&&x<bitmapBottom2.getWidth()&&y>0&&y<bitmapBottom2.getHeight()) {                    if (bitmapBottom2.getPixel(x, y) == 0) {                        L.i("透明区域");                        iv5TF = true;  //透明区域设置true//                        return true;//透明区域返回true  拦截点击事件                    } else {                        L.i("非透明 ");                        iv5TF = false;                    }                }                return false;            }        });    }    /**     * 3 电动观光车     * 主要思想:通过onTouch拦截事件,如果当前位置下,imageview所在图片有像素,就传递事件,否则拦截     */    private void setClickBottom3() {        bottom3Img.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                L.d(TAG, "我是雁栖湖下3");                if (iv6TF) {                    L.d(TAG, "我是透明区域6");                } else {                    L.d(TAG, "我是实体区域6");                    bottom3();                }            }        });        bottom3Img.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                int x = (int) event.getX();                int y = (int) event.getY();                if (x > 0&&x<bitmapBottom3.getWidth()&&y>0&&y<bitmapBottom3.getHeight()) {                    if (bitmapBottom3.getPixel(x, y) == 0) {                        L.i("透明区域");                        iv6TF = true;  //透明区域设置true                        return true;//透明区域返回true  拦截点击事件                    } else {                        L.i("非透明 ");                        iv6TF = false;                    }                }                return false;            }        });    }    /**     * 中间 图片点击事件     * 主要思想:通过onTouch拦截事件,如果当前位置下,imageview所在图片有像素,就传递事件,否则拦截     */    private void setClickCenter() {        centerImg.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                L.d(TAG, "我是中间");                if (iv7TF) {                    L.d(TAG, "我是透明区域7");                } else {                    L.d(TAG, "我是实体区域7");                    center();                }            }        });        centerImg.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                int x = (int) event.getX();                int y = (int) event.getY();                if (x > 0&&x<bitmapCenter.getWidth()&&y>0&&y<bitmapCenter.getHeight()) {                    if (bitmapCenter.getPixel(x, y) == 0) {                        L.i("透明区域");                        iv7TF = true;  //透明区域设置true//                        return true;//透明区域返回true  拦截点击事件                    } else {                        L.i("非透明 ");                        iv7TF = false;                    }                }                return false;            }        });    }//中间事件    private void center() {        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                centerImg.setImageResource(R.mipmap.ico_youyanqi_center_alpha);                Intent intent = new Intent(mContext, GaoDeAllActivity.class);                startActivity(intent);            }        }, 200);    }    //1事件    private void top1() {        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                top1Img.setImageResource(R.mipmap.ico_youyanqi_top1_alpha);                Intent intent1 = new Intent(mContext, GoodsListActivity.class);                intent1.putExtra("sort", "0");//                intent1.putExtra("keyword",null);                intent1.putExtra("categoryId", "3");                startActivity(intent1);            }        },300);    }    //2事件    private void top2() {        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                top2Img.setImageResource(R.mipmap.ico_youyanqi_top2_alpha);                Intent intent = new Intent(mContext , GaoDeCategoryActivity.class);                intent.putExtra("category","rowboats");                intent.putExtra("title","游船");                startActivity(intent);            }        },300);    }    //3事件    private void top3() {        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                top3Img.setImageResource(R.mipmap.ico_youyanqi_top3_alpha);                Intent intent = new Intent(mContext, GaoDeCategoryActivity.class);                intent.putExtra("category", "bicycle");                intent.putExtra("title", "自行车租赁");                startActivity(intent);            }        }, 300);    }    //1事件    private void bottom1() {        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                bottom1Img.setImageResource(R.mipmap.ico_youyanqi_bottom1_alpha);                Intent intent = new Intent(mContext, GaoDeCategoryActivity.class);                intent.putExtra("category", "parking");                intent.putExtra("title", "停车场");                startActivity(intent);            }        }, 300);    }    //2    private void bottom2() {        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                bottom2Img.setImageResource(R.mipmap.ico_youyanqi_bottom2_alpha);                Intent intent = new Intent(mContext, ApecCenterActivity.class);                startActivity(intent);            }        }, 300);    }    //3    private void bottom3() {        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                bottom3Img.setImageResource(R.mipmap.ico_youyanqi_bottom3_alpha);                Intent intent = new Intent(mContext, GaoDeCategoryActivity.class);                intent.putExtra("category", "sightseeingbus");                intent.putExtra("title", "电动观光车");                startActivity(intent);            }        }, 300);    }}

我的xml代码:

<RelativeLayout        android:id="@+id/sixParents"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/final_bg"        android:orientation="vertical"        android:padding="5dp">        <!-- 四个角 模块-->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical"            android:weightSum="2">            <!--两边的四块-->            <LinearLayout                android:layout_width="match_parent"                android:layout_height="0dp"                android:layout_weight="1"                android:orientation="horizontal"                android:weightSum="2">                <LinearLayout                    android:id="@+id/ll_top1"                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_marginRight="40dp"                    android:layout_weight="1">                    <ImageView                        android:clickable="true"                        android:focusable="true"                        android:id="@+id/top1"                        android:layout_width="match_parent"                        android:layout_height="match_parent"                        android:layout_marginBottom="4dp"                        android:layout_marginRight="12dp"                        android:scaleType="fitXY"                        android:src="@mipmap/ico_youyanqi_top1" />                </LinearLayout>                <LinearLayout                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:gravity="right">                    <ImageView                        android:id="@+id/top3"                        android:layout_width="match_parent"                        android:layout_height="match_parent"                        android:layout_alignParentRight="true"                        android:layout_alignParentTop="true"                        android:layout_marginBottom="4dp"                        android:layout_marginLeft="8dp"                        android:scaleType="fitXY"                        android:src="@mipmap/ico_youyanqi_top3" />                </LinearLayout>            </LinearLayout>            <!--下面 左右两块-->            <LinearLayout                android:layout_width="match_parent"                android:layout_height="0dp"                android:layout_weight="1"                android:orientation="horizontal"                android:weightSum="2">                <LinearLayout                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_marginRight="20dp"                    android:layout_marginTop="6dp"                    android:layout_weight="1">                    <ImageView                        android:id="@+id/bottom1"                        android:layout_width="match_parent"                        android:layout_height="match_parent"                        android:layout_alignParentBottom="true"                        android:layout_alignParentLeft="true"                        android:layout_marginRight="12dp"                        android:scaleType="fitXY"                        android:src="@mipmap/ico_youyanqi_bottom1" />                </LinearLayout>                <LinearLayout                    android:layout_width="0dp"                    android:layout_height="match_parent"                    android:layout_marginLeft="20dp"                    android:layout_marginTop="6dp"                    android:layout_weight="1"                    android:gravity="right">                    <ImageView                        android:id="@+id/bottom3"                        android:layout_width="match_parent"                        android:layout_height="match_parent"                        android:layout_alignParentBottom="true"                        android:layout_alignParentRight="true"                        android:layout_marginLeft="6dp"                        android:layout_toRightOf="@id/bottom1"                        android:scaleType="fitXY"                        android:src="@mipmap/ico_youyanqi_bottom3" />                </LinearLayout>            </LinearLayout>        </LinearLayout>        <!--中间三块-->        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="match_parent">            <!--中间圆圈-->            <ImageView                android:id="@+id/center"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_centerInParent="true"                android:src="@mipmap/ico_youyanqi_center" />            <ImageView                android:id="@+id/top2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_above="@id/center"                android:layout_alignParentTop="true"                android:layout_centerHorizontal="true"                android:src="@mipmap/ico_youyanqi_top2" />            <ImageView                android:id="@+id/bottom2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignParentBottom="true"                android:layout_below="@id/center"                android:layout_centerHorizontal="true"                android:src="@mipmap/ico_youyanqi_bottom2" />        </RelativeLayout>    </RelativeLayout>



0 0
原创粉丝点击