探索HitTest

声明:大神勿喷谢谢!
  产品总监最近无聊,给我来改需求,,要求一个滑动的选择日期的Calendar控件。小改怡情大改伤身啊,,老夫我默默的叼起一根烟。跑到楼道,去感叹了一下人
生。回来之后仍然没有思路,我就去找IOS的"小苹果"们请教。他们告诉我一个叫HitTest()的方法。
老夫翻遍整个Android的容器控件Api没有找到这个方法,但是天不亡我大android。身为google亲儿子的android怎么可能没有Hittest的方法?于是我就去查了下
,结果还真被我找到了一个可以用的类似的方法,那就是------getHitRect(Rect rect);于是我心怀忐忑的去阅读他的解释,发现这也许就是我想要的东西,在此我给
大家看下这个函数介绍:

void android. view. View.getHitRect( Rect outRect)

 Hit rectangle in parent's coordinates

 Parameters:
outRect The hit rectangle of the view.
我发现这里所有的View都带有这一个函数。然后我想到之前做过的一个模块,里面有这样一个方法(这个方法是由Rect对象进行调用的):
boolean android. graphics. Rect.contains( int x, int y)

Returns true if (x,y) is inside the rectangle. The left and top are considered to be inside, while the right and bottom are not. This means that for a x,y to be contained: left <= x < right and top <= y < bottom. An empty rectangle never contains any point.

Parameters:
x The X coordinate of the point being tested for containment
y The Y coordinate of the point being tested for containment
Returns:
true iff (x,y) are contained by the rectangle, where containment means left <= x < right and top <= y < bottom
思路突然如尿崩啊有木有!!程序员翻身把歌唱啊有木有!!!
然后一切都顺利的进行下去了(上代码咯):

layout文件:

<LinearLayout
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/red"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="#f00"
             />

        <TextView
            android:id="@+id/green"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="#0f0"
             />

        <TextView
            android:id="@+id/blue"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="#00f"
            />
    </LinearLayout>

Class文件:(copy到Activity下面)

private void initView() {
		// TODO Auto-generated method stub
		red = (TextView) this.findViewById(R.id.red);
		green = (TextView) this.findViewById(R.id.green);
		blue = (TextView) this.findViewById(R.id.blue);

		linear = (LinearLayout) this.findViewById(R.id.linear);

		linear.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					 cheekHit1(event.getX(), event.getY());
					 cheekHit2(event.getX(), event.getY());
					 cheekHit3(event.getX(), event.getY());
					break;
				case MotionEvent.ACTION_MOVE:
					cheekHit1(event.getX(), event.getY());
					cheekHit2(event.getX(), event.getY());
					cheekHit3(event.getX(), event.getY());
					break;
				case MotionEvent.ACTION_UP:
					 cheekHit1(event.getX(), event.getY());
					 cheekHit2(event.getX(), event.getY());
					 cheekHit3(event.getX(), event.getY());
					break;
				default:
					break;
				}

				return true;
			}

		});
	}

	private View cheekHit1(float parantX, float parantY) {

		boolean isHit = false;
		Rect rect = new Rect(red.getLeft(), red.getTop(), red.getRight(),
				red.getBottom());//得到子View的4点的坐标
		red.getHitRect(rect);
		isHit = rect.contains((int) parantX, (int) parantY);//当前red控件的矩形是否包含touch事件传入的X,跟Y坐标(相对于父View)
		if (isHit) {
			Log.i("texts", "red");
			return red;
		}
		return null;

	}

	private void cheekHit2(float parantX, float parantY) {

		boolean isHit = false;
		Rect rect = new Rect(green.getLeft(), green.getTop(), green.getRight(),
				green.getBottom());
		green.getHitRect(rect);
		isHit = rect.contains((int) parantX, (int) parantY);
		if (isHit) {
			Log.i("texts", "green");
		}

	}

	private void cheekHit3(float parantX, float parantY) {

		boolean isHit = false;
		Rect rect = new Rect(blue.getLeft(), blue.getTop(), blue.getRight(),
				blue.getBottom());

		blue.getHitRect(rect);
		isHit = rect.contains((int) parantX, (int) parantY);
		if (isHit) {
			Log.i("texts", "blue");
		}

	}
效果图:


Log日志:


hitTest就是这么简简单单的结束了,,哈哈哈!我发现网上好多demo都是由这套HitRect(Rect rect)演化了一下,比如:小球或气泡的撞击检测,滑动选日期,
等等、方法随简单,但是可以玩出很多花样,下篇博文我就教你如何使用这个方法做一个类似于快捷酒店的滑动选日期的Calendar控件!

声明:大神勿喷!!!


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值