自定义IamgeView,实现区域点击事件
来源:互联网 发布:头晕是怎么回事 知乎 编辑:程序博客网 时间:2024/06/01 23:57
1.自定义ImageView
public class RegionCoordView extends ImageView { private Context mContext; //上下文 public RegionCoordView(Context context) { this(context, null); } public RegionCoordView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { float x = event.getX(); float y = event.getY(); int area = 0; if (x >= 0 && x < 200 && y > 0 && y < 200) { area = 1; } else if (x > 200 && x < 400 && y > 0 && y < 200) { area = 2; } else if (x > 0 && x < 200 && y > 200 && y < 400) { area = 3; } else if (x > 200 && x < 400 && y > 200 && y < 400) { area = 4; } ((MainActivity) mContext).showClickArea(area); } return super.onTouchEvent(event); }}
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.qd.androidview.MainActivity"> <com.example.qd.androidview.RegionCoordView android:layout_width="400px" android:layout_height="400px" android:background="#D0D0D0" /></RelativeLayout>
3.MainActivity
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void showClickArea(int area) { Toast.makeText(MainActivity.this,"您点击到了第" + area + "块区域!",Toast.LENGTH_SHORT).show(); }}
4.运行效果
0 0
- 自定义IamgeView,实现区域点击事件
- 自定义控件实现不规则区域点击事件
- 自定义IamgeView
- 自定义VIEW中区域点击事件
- 自定义视图点击区域
- iOS实现UIImageView透明区域点击事件穿透
- iOS实现UIImageView透明区域点击事件穿透
- iOS实现UIImageView透明区域点击事件穿透
- iOS实现UIImageView透明区域点击事件穿透
- iOS实现UIImageView透明区域点击事件穿透
- iOS 实现 UIImageView 透明区域点击事件穿透
- iOS实现UIImageView透明区域点击事件穿透
- 自定义Notification实现按钮点击事件
- 自定义EditText 实现drawableRight/drawableLeft 点击事件
- 自定义View圆的点击事件实现
- 不规则区域响应点击事件
- 触摸某一区域点击事件
- 自定义布局某个点击区域
- android 控件 侧滑 navigationview 小技巧
- jsp的3个编译指令
- linux 下各errno的意义
- 关于Java类加载器(ClassLoader)
- 【Fibonacci 序列+第一类Stirling数+二项式定理】[CodeForces - 717A]Festival Organization
- 自定义IamgeView,实现区域点击事件
- TCP/IP每层对应的协议及功能
- callable(object)
- 自定义控件的流程
- android : Session 'app': Error Installing APK解决方案
- Android 打开另一个app的两种方式
- 对极几何——本质矩阵E和基础矩阵F
- Guava缓存值CacheBuilder介绍
- 逻辑回归(LR)