Skip to content

MotionEventHelper

sloop edited this page Dec 5, 2016 · 3 revisions

MotionEvent 辅助类,帮助 MotionEvent 中的坐标执行坐标变换。

拷贝自 GoogleSource - MotionEventHelper

方法摘要
transformEvent使用一个 Matrix 对 MotionEvent 中的坐标进行转换。
publicclassTestMorionEventHelperActivityextendsBaseActivity {
privatestaticfinalStringTAG = "TestMorionEventHelper";
MatrixmMatrix;
@OverrideprotectedvoidonCreate(@NullableBundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_see_logcat);
TextViewtext = (TextView) findViewById(R.id.custom_view_text);
text.append("\n在屏幕上触摸,查看坐标变化。\nx,y坐标均增加100");
mMatrix = newMatrix();
mMatrix.postTranslate(100,100);
}
@OverridepublicbooleanonTouchEvent(MotionEventevent) {
Log.i(TAG, "转换前 > "+event.getX()+" : "+event.getY());
MotionEvente = MotionEventHelper.transformEvent(event, mMatrix);
Log.i(TAG, "转换后 > "+e.getX()+" : "+e.getY());
Log.i(TAG, "-------------------------");
returntrue;
}
}

结果:

TestMorionEventHelper: 转换前 > 900.4544 : 960.0
TestMorionEventHelper: 转换后 > 1000.4544 : 1060.0
TestMorionEventHelper: -------------------------
TestMorionEventHelper: 转换前 > 900.4544 : 960.0
TestMorionEventHelper: 转换后 > 1000.4544 : 1060.0
TestMorionEventHelper: -------------------------

Clone this wiki locally