自定义视图中的Android触摸手势-空对象引用
我想开发一个android应用程序,允许用户在手机屏幕上画图,并显示正在使用的触摸手势(轻触、长按、滚动等)。所以我认为要显示手指的轨迹,我们需要使用视图。我创建了两个独立的应用程序——一个显示正在执行的触摸手势,另一个允许用户画画 现在我想把两者结合起来。我如何实现它?绘图是在自定义视图上完成的,而另一个视图在活动中 我尝试在视图上实现手势显示功能。我得到了以下错误: null对象引用上的view.GestureDetectorCompat.onTouchEvent(android.view.MotionEvent)” 下面是我的代码:自定义视图中的Android触摸手势-空对象引用,android,android-custom-view,Android,Android Custom View,我想开发一个android应用程序,允许用户在手机屏幕上画图,并显示正在使用的触摸手势(轻触、长按、滚动等)。所以我认为要显示手指的轨迹,我们需要使用视图。我创建了两个独立的应用程序——一个显示正在执行的触摸手势,另一个允许用户画画 现在我想把两者结合起来。我如何实现它?绘图是在自定义视图上完成的,而另一个视图在活动中 我尝试在视图上实现手势显示功能。我得到了以下错误: null对象引用上的view.GestureDetectorCompat.onTouchEvent(android.view.
public class Tdraw extends View implements GestureDetector.OnDoubleTapListener, GestureDetector.OnGestureListener {
private TextPaint mTextPaint;
private float mTextWidth;
private float mTextHeight;
private TextView gesText;
private GestureDetectorCompat gDetector;
public Tdraw(Context context) {
super(context);
init(null, 0);
gesText = (TextView) findViewById(R.id.gesText);
gDetector = new GestureDetectorCompat(context, this);
gDetector.setOnDoubleTapListener(this);
}
public Tdraw(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public Tdraw(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
gesText.setText("Single Tap");
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
gesText.setText("Double Tap");
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
gesText.setText("Scroll");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
gesText.setText("Long Press");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
gesText.setText("Fling");
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
XML:
错误的原因可能是什么?活动中的相同代码可以正常工作,但在这里它失败了。如何将这两种功能结合起来?提前谢谢
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<com.example.anurooppv.gesturerec.Tdraw android:background="#ccc" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="20dp" android:paddingBottom="40dp"
app:exampleDimension="24sp" app:exampleColor="#33b5e5" app:exampleString="Hello, Tdraw"
app:exampleDrawable="@android:drawable/ic_menu_add"
android:id="@+id/view" />
<TextView
android:layout_width="258dp"
android:layout_height="66dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Perform a touch gesture"
android:id="@+id/textView"
android:layout_gravity="left|top"
android:textStyle="bold"
android:textSize="20dp" />
<TextView
android:layout_width="165dp"
android:layout_height="42dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/gesText"
android:layout_gravity="left|bottom" />
</FrameLayout>