我创建了一个非常简单的ImageView,我正在尝试实现OnTouchEvent()。这个应用程序是在Xamarin中开发的,所以它在C#中。
当我将CustomImageView添加到布局时,看起来我的OnTouchEvent没有正常启动。
如何更改我的代码以注册OnTouchEvents?
旁注:我想开发一些允许用户缩放/旋转/移动创建的图像视图的内容,因此如果我偏离轨道,请告诉我应该做的事情。
CustomImageView
public class CustomImageView : ImageView
{
private float _viewX;
public CustomImageView (Context context) :
base(context)
{
Initialize ();
}
public CustomImageView (Context context, IAttributeSet attrs) :
base (context, attrs)
{
Initialize ();
}
public CustomImageView (Context context, IAttributeSet attrs, int defStyle) :
base (context, attrs, defStyle)
{
Initialize ();
}
void Initialize ()
{
}
public bool OnTouchEvent(MotionEvent m_event){
switch (m_event.Action) {
case MotionEventActions.Down:
_viewX = m_event.GetX ();
System.Console.WriteLine ("Down");
break;
case MotionEventActions.Up:
System.Console.WriteLine ("Up");
break;
case MotionEventActions.Move:
//int x_cord = (int)m_event.GetX;
// int y_cord = (int)m_event.GetY;
System.Console.WriteLine ("Move");
break;
}
return true;
}
}
以下是创建CustomImageView并将其添加到当前布局的按钮
btnAddImage2.Click += (sender, e) =>
{
CustomImageView bitmapView = new CustomImageView (this);
Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.PixagramLogo);
bitmapView.SetImageBitmap(bitmap);
m_Layout.AddView (bitmapView);
};
固定/解决
看来我需要覆盖该方法。似乎解决了我当前的问题。
public override bool OnTouchEvent(MotionEvent m_event){
switch (m_event.Action) {
case MotionEventActions.Down:
_viewX = m_event.GetX ();
System.Console.WriteLine ("Down");
break;
case MotionEventActions.Up:
System.Console.WriteLine ("Up");
break;
case MotionEventActions.Move:
//int x_cord = (int)m_event.GetX;
// int y_cord = (int)m_event.GetY;
System.Console.WriteLine ("Move");
break;
}
return true;
}
答案 0 :(得分:0)
public override bool OnTouchEvent(MotionEvent m_event){
switch (m_event.Action) {
case MotionEventActions.Down:
_viewX = m_event.GetX ();
System.Console.WriteLine ("Down");
break;
case MotionEventActions.Up:
System.Console.WriteLine ("Up");
break;
case MotionEventActions.Move:
//int x_cord = (int)m_event.GetX;
// int y_cord = (int)m_event.GetY;
System.Console.WriteLine ("Move");
break;
}
return true;
}