自己定义ImageView,实现点击之后算出点击的是身体的哪个部位
近期也是由于项目的原因,所以有机会接触到这边的算法。 此文重点不是怎样实现的思路和原理, 有须要的同事能够借鉴一下
废话不多说,直接上代码:
<span style="font-size:18px;"><span style="font-size:18px;">public class MyImage extends ImageView {
private DisplayMetrics dm;
private int bodyImageViewHeight = 0;
private int bodyImageViewWidth = 0;
public MyImage(Context context) {
super(context);
}
public MyImage(Context context, AttributeSet attrs) {
super(context, attrs);
dm = context.getResources().getDisplayMetrics();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
//初始化各个部位的范围
initParametersForRegion();
if(isTouchPointInTransparent(x, y)) {
Log.e("mcoy", "the point is in transparent area now");
} else {
positionType position = pointToPosition(x, y);
Log.e("mcoy", "the position is " + position);
}
return super.onTouchEvent(event);
}
private enum positionType{
REGION_FRONT_HAND,
REGION_FRONT_HEAD,
REGION_FRONT_CHEST,
REGION_FRONT_WAIST,
REGION_FRONT_LEG
}
private positionType pointToPosition(int x, int y) {
if(x < mHandX1 || x > mHandX2)
return positionType.REGION_FRONT_HAND;
else if (y < mHeadY)
return positionType.REGION_FRONT_HEAD;
else if(y < mChestY)
return positionType.REGION_FRONT_CHEST;
else if(y < mWaistY)
return positionType.REGION_FRONT_WAIST;
else
return positionType.REGION_FRONT_LEG;
}
/**
*
* @param x
* @param y
* @return 推断点击区域是否在透明区域
*/
private boolean isTouchPointInTransparent(int x, int y) {
int paddingLeft = this.getPaddingLeft();
int paddingTop = this.getPaddingTop();
int imageHeight = this.getHeight();
int imageWidth = this.getWidth();
Drawable drawable = this.getDrawable();
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
int intrinsicHeight = drawable.getIntrinsicHeight();
int intrinsicWidth = drawable.getIntrinsicWidth();
int locationInBitmapX = (x - paddingLeft) * intrinsicWidth / imageWidth;
int locationInBitmapY = (y - paddingTop) * intrinsicHeight / imageHeight;
int pixel = bitmap.getPixel(locationInBitmapX, locationInBitmapY);
Log.e("mcoy", "x is " + x);
Log.e("mcoy", "y is " + y);
Log.e("mcoy", "imageHeight is " + imageHeight);
Log.e("mcoy", "imageWidth is " + imageWidth);
Log.e("mcoy", "intrinsicHeight is " + intrinsicHeight);
Log.e("mcoy", "intrinsicWidth is " + intrinsicWidth);
Log.e("mcoy", "locationInBitmapX is " + locationInBitmapX);
Log.e("mcoy", "locationInBitmapY is " + locationInBitmapY);
Log.e("mcoy", "actualBitmapX is " + locationInBitmapX / dm.density);
Log.e("mcoy", "actualBitmapY is " + locationInBitmapY / dm.density);
Log.e("mcoy", "pixel is " + pixel);
return pixel == 0;
}
private int mHeadY;
private int mHandX1;
private int mHandX2;
private int mChestY;
private int mWaistY;
//下面数据须要同UI同事沟通好每一个部位定义的范围。或者自己手动量一下
private final int HEAD_AREA = 130;
private final int LEFT_HAND_AREA = 126;
private final int RIGHT_HAND_AREA = 280;
private final int CHEST_AREA = 260;
private final int WAIST_AREA = 417;
private void initParametersForRegion() {
if(bodyImageViewHeight != this.getHeight()) {
bodyImageViewHeight = this.getHeight();
bodyImageViewWidth = this.getWidth();
int imageIntrinsicHeight = this.getDrawable().getIntrinsicHeight();
int imageIntrinsicWidht = this.getDrawable().getIntrinsicWidth();
Log.e("danny", "bodyImageViewHeight is " + bodyImageViewHeight);
Log.e("danny", "bodyImageViewWidth is " + bodyImageViewWidth);
Log.e("danny", "imageIntrinsicHeight is " + imageIntrinsicHeight);
Log.e("danny", "imageIntrinsicWidht is " + imageIntrinsicWidht);
mHeadY = DensityUtil.dip2px(getContext(), HEAD_AREA) * bodyImageViewHeight / imageIntrinsicHeight
+ this.getPaddingTop();
mHandX1 = DensityUtil.dip2px(getContext(), LEFT_HAND_AREA) * bodyImageViewWidth / imageIntrinsicWidht
+ this.getPaddingLeft();
mHandX2 = DensityUtil.dip2px(getContext(), RIGHT_HAND_AREA) * bodyImageViewWidth / imageIntrinsicWidht
+ this.getPaddingLeft();
mChestY = DensityUtil.dip2px(getContext(), CHEST_AREA) * bodyImageViewHeight / imageIntrinsicHeight
+ this.getPaddingTop();
mWaistY = DensityUtil.dip2px(getContext(), WAIST_AREA) * bodyImageViewHeight / imageIntrinsicHeight
+ this.getPaddingTop();
Log.e("danny", "mHeadY is " + mHeadY);
Log.e("danny", "mHandX1 is " + mHandX1);
Log.e("danny", "mHandX2 is " + mHandX2);
Log.e("danny", "mChestY is " + mChestY);
Log.e("danny", "mWaistY is " + mWaistY);
}
}
}</span></span>
代码非常easy, 就是一个自己实现的ImageView, 可是有几点须要注意的点:
1 图片须要跟UI同事沟通好对应坐标(认真看完代码之后就会知道原因), 假设换图片的话。那HEAD_AREA等值可能须要又一次定义
2 算法思路---在不同的desity的的设备上。 我们都能够算出图片相应的drawable的width/height,而图片缩放或者是放大是依照比例缩放/放大, 因此我们能够得出例如以下等式:
touchX / imageWidth = positonInBitmap / drawable.getIntransicWidth ;
==> positonInBitmap = touchX * drawable.getIntransicWidth / imageWidth;
touchX是点击在ImageView上的x坐标;imageWidth是图片的宽度。positionInBitmap是touchX映射在ImageView所相应drawable上的x坐标(这句话有点拗口,能够细致想一下)
下面是xml文件:
<span style="font-size:18px;"><span style="font-size:18px;"><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.mcoy.pixelinbitmaptest.MainActivity" > <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/hello_world" /> <com.example.jiangxinxing.views.MyImage
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/man_front"
android:layout_below="@+id/tv" /> </RelativeLayout></span></span>
也有一点须要注意:
须要将adjustViewBounds属性设置为true。这样会调整ImageView的界限来保持图像纵横比不变
demo下载链接:http://download.csdn.net/detail/zxm317122667/9000969
自己定义ImageView,实现点击之后算出点击的是身体的哪个部位的更多相关文章
- Android 自己定义ImageView实现圆角/圆形 附加OnTouchListener具体凝视以及Button圆角
转载请注明出处:王亟亟的大牛之路 平时要用一些非方方正正的button之类的小伙伴们是怎样实现的?RadioButton? ImageButton? 还是其它? 今天亟亟上的是ImageView来实现 ...
- 实现RecycleView动态使列表item可以点击或不可点击切换
效果 这里讲的是第二个button跳转的Activity,这里和上一篇不同之处在于可以item点击.item子控件点击 继承BaseAdapter 同样也要继承BaseAdapter public c ...
- 利用Kotlin扩展函数实现任意View的点击处理(点击效果和防止快速点击)
利用Kotlin扩展函数实现View的点击处理(点击效果和防止快速点击) kotlin经典写法: view?.setOnClickListener { //实现 } 项目实践证明,这种写法存在问题 例 ...
- 点击li,点击的li添加class,其余去掉class
点击li,点击的li添加class,其余去掉class <script type="text/javascript"> $(function () { var liob ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验三:按键模块② — 点击与长点击
实验三:按键模块② - 点击与长点击 实验二我们学过按键功能模块的基础内容,其中我们知道按键功能模块有如下操作: l 电平变化检测: l 过滤抖动: l 产生有效按键. 实验三我们也会z执行同样的事情 ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验五:按键模块④ — 点击,长点击,双击
实验五:按键模块④ - 点击,长点击,双击 实验二至实验四,我们一共完成如下有效按键: l 点击(按下有效) l 点击(释放有效) l 长击(长按下有效) l 双击(连续按下有效) 然而,不管哪个实验 ...
- jquery对类的操作,添加,删除,点击添加,再点击删除
jquery对类的操作,添加(addClass),删除l类(remoceClass),点击添加,再点击删除(toggleClass)
- C# Chart 点击获取当前点击坐标和Series
C# Chart 点击获取当前点击坐标和Series https://blog.csdn.net/wumuzhizi/article/details/47168989 2015年07月31日 13:5 ...
- input radio点击选中再点击取消
这里主要说一下这个jquery中的data()方法,个人感觉这个方法平时挺少用到的,所以说一说,按照官方的解释就是 向元素附加数据,然后取回该数据; 嗯,是的,就是这么简单. 那这里说一下这个方法的使 ...
随机推荐
- ogre3D学习基础7---材质详解
物体着色的基础 --- 四种不同光照作用 1.环境反射 近似的模拟了场景中的全局辐射,也就是用来近似模拟所有光在场景中不断散射的结果.材质中有相应的属性来代表这种环境反射颜色. 2.漫反射 这种颜色是 ...
- linux随笔一
1.过滤列表输出:*表示多个字符 ll mon* 结果输出为:mongo.sh mongo_sso.sh 2.man +命令 例如:man ls 查看命令的用法 3.cd 将shell回话移动到 ...
- Pycharm脚本通用部分设置
Python脚本经常要设置同样的注释内容,Pycharm里面提供的模板可以很好的实现这个需求. 查找: File->settings->Editor->File and Code T ...
- Linux Shell系列教程之(八)Shell printf命令详解
本文是Linux Shell系列教程的第(八)篇,更多shell教程请看:Linux Shell系列教程 在上一篇:Linux Shell系列教程之(七)Shell输出这篇文章中,已经对Shell p ...
- 【bzoj3166】[Heoi2013]Alo 可持久化Trie树+STL-set
题目描述 Welcome to ALO ( Arithmetic and Logistic Online).这是一个VR MMORPG ,如名字所见,到处充满了数学的谜题.现在你拥有n颗宝石,每颗宝石 ...
- BZOJ 4552 [Tjoi2016&Heoi2016]排序 ——线段树 二分答案
听说是BC原题. 好题,二分答案变成01序列,就可以方便的用线段树维护了. 然后就是区间查询和覆盖了. #include <map> #include <cmath> #inc ...
- 2018.8.7 Noip2018模拟测试赛(二十)
日期: 八月七号 总分: 300分 难度: 提高 ~ 省选 得分: 100分(呵呵一笑) 题目列表: T1:SS T2:Tree Game T3:二元运算 赛后反思: Emmmmmm…… 开 ...
- 【CF1017D】The Wu(状压前缀和)
题意:给定n个a[i],m个长为n的01串,定义串a与b之间的运算为a^b再按位取反,若第i位为1则运算结果加a[i] q组询问(x,y),每次询问m个串中与串x运算后答案小于等于y的串的个数 n&l ...
- es6总结(六)--新数据类型-Symbol
- poj 2492 A Bug's Life 二分图染色 || 种类并查集
题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...