如何解决ScaleGestureDetector.IOnScaleGestureListener C#Xamarin问题
我正在构建一个应用程序,该应用程序允许用户在Xamarin android .net中移动2个textview。 一切正常,除了onScale(捏手势)。调试显示从未调用IOnScaleGestureListener函数(这就是为什么我将它们留为空白)的原因。有人知道我需要怎么打电话吗? 主要活动
public class MainActivity : AppCompatActivity,IOnTouchListener,IOnGestureListener,ScaleGestureDetector.IOnScaleGestureListener
{
int position = 0;
private List<TextView> txtGestureView = new List<TextView>();
private GestureDetector gestureDetector;
private ScaleGestureDetector scaleDetector;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this,savedInstanceState);
SetContentView(Resource.Layout.activity_main);
RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(400,400);
txtGestureView.Add(FindViewById<TextView>(Resource.Id.imageView));
txtGestureView.Add(FindViewById<TextView>(Resource.Id.imageView2));
RelativeLayout relativeLayout = FindViewById<RelativeLayout>(Resource.Id.relativeLayout1);
gestureDetector = new GestureDetector(this);
scaleDetector = new ScaleGestureDetector(this,this);
txtGestureView[0].SetonTouchListener(this);
txtGestureView[0].Tag = "0";
txtGestureView[1].SetonTouchListener(this);
txtGestureView[1].Tag = "1";
}
public bool OnTouch(View v,MotionEvent e)
{
position = int.Parse(v.Tag.ToString(),0);
return gestureDetector.OnTouchEvent(e);
}
public bool OnScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY)
{
float diffY = e2.GetY() - e1.GetY();
float diffX = e2.GetX() - e1.GetX();
txtGestureView[position].SetX(txtGestureView[position].GetX() + diffX);
txtGestureView[position].SetY(txtGestureView[position].GetY() + diffY);
return true;
}
public bool OnDown(MotionEvent e) { return true; }
public bool OnFling(MotionEvent e1,float veLocityX,float veLocityY) { return true; }
public void OnLongPress(MotionEvent e) { }
public void OnShowPress(MotionEvent e) { }
public bool OnSingleTapUp(MotionEvent e) { return true; }
public bool OnScale(ScaleGestureDetector detector)
{
return true;
}
public bool OnScaleBegin(ScaleGestureDetector detector)
{
return true;
}
public void OnScaleEnd(ScaleGestureDetector detector)
{
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/activity_main"
android:id="@+id/relativeLayout1">
<TextView
android:id="@+id/imageView"
android:layout_width="200dp"
android:text="imageView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:layout_marginTop="100dp"
android:gravity="center"
android:background="@android:color/black"
android:textColor="@android:color/white"
android:layout_height="50dp"/>
<TextView
android:id="@+id/imageView2"
android:layout_width="200dp"
android:text="imageView2"
android:textSize="35dp"
android:gravity="center"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:background="@android:color/black"
android:layout_height="50dp"/>
</RelativeLayout>
解决方法
问题出在OnTouch()
我回来了
return gestureDetector.OnTouchEvent(e);
代替
return scaleDetector.OnTouchEvent(e);
为什么没有此功能:
public bool OnScale(ScaleGestureDetector detector)
{
return true;
}
public bool OnScaleBegin(ScaleGestureDetector detector)
{
return true;
}
public void OnScaleEnd(ScaleGestureDetector detector)
{
}
被叫。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。