gpt4 book ai didi

c# - 在android中实现滑动和点击相对布局的最佳方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:22:20 25 4
gpt4 key购买 nike

enter image description here
我正在创建一个相对布局,并希望添加点击和滑动(触摸并在布局上移动指针以实时移动它)并检测左右滑动。到目前为止,我已经尝试了以下代码。

public bool OnTouch(View v, MotionEvent e)
{
if (gestureDetector.OnTouchEvent(e))
{
//This is a Click
return true;
}
else
{
int initialTouchX = 0, initialTouchY = 0;
int newx = 0;
var x = v.Left;
switch (e.Action)
{

case MotionEventActions.Down:
{
_viewX = e.GetX();
_viewY = e.GetY();
initialTouchX = (int)e.RawX;
initialTouchY = (int)e.RawY;
break;
}

case MotionEventActions.Move:
{

var left = (int)(e.RawX - _viewX);
newx = left;
var right = (int)(left + v.Width);

var top = (int)(e.RawY - _viewY);
var bottom = (int)(top + v.Height);

v.Layout(left, top, right, bottom);

break;
}
case MotionEventActions.Up:
{
int lastX = (int)e.GetX();
int lastY = (int)e.GetY();
if ((x - newx) > 40)
{
//Detect Right Swipe

}
else if ((newx - x > 40))
{
//Detect Left Swipe
}
else
{
//Skip others
}
break;
}

}
}
return true;
}

我的 gestureDetector.OnTouchEvent(e) 代码

gestureDetector = new GestureDetector(this, new SingleTapUp());

class SingleTapUp : Android.Views.GestureDetector.SimpleOnGestureListener
{

public override bool OnSingleTapUp(MotionEvent e) {
// Toast.MakeText(this,, ToastLength.Long).Show();
return true;
}

}

我的代码在某些设备和模拟器上运行良好。但有时不工作,onclick 布局会自动移动(布局以触摸指针为中心)。我认为有什么地方出了问题并导致了这个问题。你可以建议我最好的/标准的方法来做到这一点。任何帮助将不胜感激。

最佳答案

我以这种方式实现 swype:

public class MainActivity : Activity, GestureDetector.IOnGestureListener
{
private GestureDetector _gestureDetector;

public bool OnDown(MotionEvent e)
{
return false;
}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
bool result = false;
int SWIPE_THRESHOLD = 80;
int SWIPE_VELOCITY_THRESHOLD = 80;
try
{
float diffY = e2.GetY() - e1.GetY();
float diffX = e2.GetX() - e1.GetX();
if (Math.Abs(diffX) > Math.Abs(diffY))
{
if (Math.Abs(diffX) > SWIPE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
{
if (diffX > 0)
{
//code for swipe right here

}
else
{
//code for swipe Left here

}
}
}
}
catch (Exception exception)
{
//exception.printStackTrace();
}
return result;
}
public void OnLongPress(MotionEvent e) {}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
}
public void OnShowPress(MotionEvent e) {}
public bool OnSingleTapUp(MotionEvent e)
{
return false;
}
public override bool OnTouchEvent(MotionEvent e)
{
_gestureDetector.OnTouchEvent(e);
return false;
}

protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

SetContentView (Resource.Layout.Main);
_gestureDetector = new GestureDetector(this);

}
}

对于ViewPager,你可以用这样的东西重叠页面PageTransformation:

using System;

using Android.Views;
using Android.Support.V4.View;

namespace SomeName
{
public class SinkAndSlideTransformer : Java.Lang.Object, ViewPager.IPageTransformer
{
public void TransformPage(View view, float position)
{
if (position < -1 || position > 1)
{
view.Alpha = 0; // The view is offscreen.
}
else
{
view.Alpha = 1;

if (position < 0)
{
// 'Sink' the view if it's to the left.
// Scale the view.
view.ScaleX = 1 - Math.Abs (position);
view.ScaleY = 1 - Math.Abs (position);

// Set the translationX to keep the view in the middle.
view.TranslationX = view.Width * Math.Abs (position);
}
}
}
}
}

这会产生这样的效果:
enter image description here

使用这个适配器:

using System;
using System.Collections.Generic;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V4.View;

using Square.Picasso;

namespace StaffPro
{
public class SlideshowPagerAdapter : PagerAdapter
{
List<Uri> _items = new List<Uri>();
private readonly Activity _context;

public SlideshowPagerAdapter (Activity context, List<Uri> items) : base()
{
_items = items;
_context = context;
}
public override int Count
{
get { return _items.Count; }
}
public override bool IsViewFromObject(View view, Java.Lang.Object _object)
{
return view == ((RelativeLayout) _object);
}
public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
{
var view = LayoutInflater.From (container.Context).Inflate (Resource.Layout.SlideshowViewPager, container, false);
ImageView imageView = view.FindViewById<ImageView> (Resource.Id.slideshowImageView);
Picasso.With(_context).Load(_items [position].ToString()).Into(imageView);
container.AddView(view);

return view;
}
public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object _object)
{
container.RemoveView((View)_object);
}
}
}

并用类似这样的方式调用 View 页面和适配器:

var viewPagerAdapter = new SlideshowPagerAdapter(Activity, some_Uri_images);
slideshowViewPager.Adapter = viewPagerAdapter;
slideshowViewPager.SetPageTransformer (true, new SinkAndSlideTransformer ());

您可以找到更多转换 here.

另请记住,ViewPager 有一个名为 setPageMargin() 的方法。此方法可以接收负值,这将使 fragment/ View 相互重叠。
您还可以在转换代码中使用 view.Rotation 来实现您想要的照片随机掉落和重叠效果。
如果您需要保留许多已加载和显示的图像,请同时检查 ViewPager.OffscreenPageLimit 它将覆盖为下一个和上一个项目存储页面的默认缓存限制,但是请注意高内存使用率。

关于c# - 在android中实现滑动和点击相对布局的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com