有关不规则图片的点击响应事件和手势滑动闪退的处理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()) { //判断图片是否透明,当透明设定iv1TF为true 再点击时间里面处理 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
- 有关不规则图片的点击响应事件和手势滑动闪退的处理d
- 不规则图片的点击响应
- 不规则图片的点击事件
- [UnityUI]不规则图片的点击响应
- android点击和滑动事件的处理
- UGUI_不规则图片点击事件处理
- Unity UGUI不规则图片点击事件处理
- UGUI_不规则图片点击事件处理
- Unity3D UGUI不规则图片点击事件处理
- 手势与表格的点击事件处理
- Android ImageView图片透明区域不响应点击事件,不规则图片透明区域响应点击事件
- 不规则区域响应点击事件
- 点击事件的拦截处理滑动冲突
- android 点击、滑动事件的处理 GestureDetector
- android不规则区域的点击响应思路
- Android:实现手势滑动的事件处理方法
- Android:实现手势滑动的事件处理方法
- Android:实现手势滑动的事件处理方法
- 会话技术(Cookie,Session)
- 课后第6题
- 浅谈HTTP协议
- Win7 pySpark安装
- 关于抖动动画
- 有关不规则图片的点击响应事件和手势滑动闪退的处理d
- mysqladmin: connect to server at 'localhost' failed;错误解决方法
- Thymeleaf教程 (三) 创建一个多语言的首页
- web应用部署到Linux服务器上,CPU占用过高
- 微型投影仪第三篇——磕磕绊绊
- 洛谷 P1361 小猫爬山
- android 常用注解
- 【OpsDEV】VirtualBox下给Solus安装增强工具
- Idea设置字符编码和内存溢出