- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Mono for Android 中的自定义叠加项上使用弹出气球。我找到了这个项目 https://github.com/jgilfelt/android-mapviewballoons ,但是它是用 Java 编写的。我已经开始手动将它转换为 C#,但这有点让我头疼。我读过可以将 java 文件导入 Mono,但我不确定此时哪条路径更容易。如果有人有我在 C# 中链接到的项目,那就更好了。感谢您的帮助!
最佳答案
我已经冒昧地用 c# 为您重写了第一个类,如果您只是清理从 java 到 c# 的差异,那将是相当容易的。
希望您可以以此为基础,根据需要将其他类转换为 c#。
您需要确保使用作者在其 github 示例中使用的布局文件、可绘制对象和选择器。将它们包含在您的项目中,并根据需要进行调整。
using System;
using Android.Content;
using Android.GoogleMaps;
using Android.Util;
using Android.Views;
using Android.Widget;
namespace MapViewBalloons
{
/**
* A view representing a MapView marker information balloon.
*
* @author Jeff Gilfelt
*
*/
public class BalloonOverlayView : FrameLayout
{
private LinearLayout _layout;
private TextView _title;
private TextView _snippet;
/**
* Create a new BalloonOverlayView.
*
* @param context - The activity context.
* @param balloonBottomOffset - The bottom padding (in pixels) to be applied
* when rendering this view.
*/
public BalloonOverlayView(Context context, int balloonBottomOffset)
: base(context)
{
SetPadding(10, 0, 10, balloonBottomOffset);
_layout = new LimitLinearLayout(context);
_layout.Visibility = ViewStates.Visible;
SetupView(context, _layout);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
LayoutParams.WrapContent, LayoutParams.WrapContent);
layoutParams.Gravity = GravityFlags.NoGravity;
AddView(_layout, layoutParams);
}
/**
* Inflate and initialize the BalloonOverlayView UI. Override this method
* to provide a custom view/layout for the balloon.
*
* @param context - The activity context.
* @param parent - The root layout into which you must inflate your view.
*/
protected void SetupView(Context context, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
View v = inflater.Inflate(Resource.Layout.balloon_overlay, parent);
_title = (TextView)v.FindViewById(Resource.Id.balloon_item_title);
_snippet = (TextView)v.FindViewById(Resource.Id.balloon_item_snippet);
}
/**
* Sets the view data from a given overlay item.
*
* @param item - The overlay item containing the relevant view data.
*/
public void setData(OverlayItem item)
{
_layout.Visibility = ViewStates.Visible;
SetBalloonData(item, _layout);
}
/**
* Sets the view data from a given overlay item. Override this method to create
* your own data/view mappings.
*
* @param item - The overlay item containing the relevant view data.
* @param parent - The parent layout for this BalloonOverlayView.
*/
protected void SetBalloonData(OverlayItem item, ViewGroup parent)
{
if (!string.IsNullOrEmpty(item.Title))
{
_title.Visibility = ViewStates.Visible;
_title.Text = item.Title;
}
else
{
_title.Text = "";
_title.Visibility = ViewStates.Gone;
}
if (!string.IsNullOrEmpty(item.Snippet))
{
_snippet.Visibility = ViewStates.Visible;
_snippet.Text = item.Snippet;
}
else
{
_snippet.Text = "";
_snippet.Visibility = ViewStates.Gone;
}
}
private class LimitLinearLayout : LinearLayout
{
private static int MAX_WIDTH_DP = 280;
private readonly float SCALE;
public LimitLinearLayout(Context context)
: base(context)
{
SCALE = context.Resources.DisplayMetrics.Density;
}
public LimitLinearLayout(Context context, IAttributeSet attrs)
: base(context, attrs)
{
SCALE = context.Resources.DisplayMetrics.Density;
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
var mode = MeasureSpec.GetMode(widthMeasureSpec);
int measuredWidth = MeasureSpec.GetSize(widthMeasureSpec);
int adjustedMaxWidth = (int)(MAX_WIDTH_DP * SCALE + 0.5f);
int adjustedWidth = Math.Min(measuredWidth, adjustedMaxWidth);
int adjustedWidthMeasureSpec = MeasureSpec.MakeMeasureSpec(adjustedWidth, mode);
base.OnMeasure(adjustedWidthMeasureSpec, heightMeasureSpec);
}
}
}
}
更新:添加了另一个类 BalloonItemizedOverlay。我没有测试这段代码,它只是编译。
using System.Collections.Generic;
using System.Linq;
using Android.GoogleMaps;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Views;
using Java.Lang;
namespace MapViewBalloons
{
public abstract class BalloonItemizedOverlay : ItemizedOverlay, View.IOnTouchListener, View.IOnClickListener
{
private static long BALLOON_INFLATION_TIME = 300;
private static Handler _handler = new Handler();
private MapView _mapView;
private BalloonOverlayView _balloonView;
private View _clickRegion;
private View _closeRegion;
private int _viewOffset;
private MapController _mc;
private OverlayItem _currentFocusedItem;
private int _currentFocusedIndex;
private float _startX,
_startY;
private bool _showClose = true;
private bool _showDisclosure = false;
private bool _snapToCenter = true;
private static bool _isInflating = false;
private BalloonRunnable _balloonRunnable;
/**
* Create a new BalloonItemizedOverlay
*
* @param defaultMarker - A bounded Drawable to be drawn on the map for each item in the overlay.
* @param mapView - The view upon which the overlay items are to be drawn.
*/
public BalloonItemizedOverlay(Drawable defaultMarker, MapView mapView)
: base(defaultMarker)
{
this._mapView = mapView;
_viewOffset = 0;
_mc = mapView.Controller;
_balloonRunnable = new BalloonRunnable(this);
}
/**
* Set the horizontal distance between the marker and the bottom of the information
* balloon. The default is 0 which works well for center bounded markers. If your
* marker is center-bottom bounded, call this before adding overlay items to ensure
* the balloon hovers exactly above the marker.
*
* @param pixels - The padding between the center point and the bottom of the
* information balloon.
*/
public int BalloonBottomOffset
{
get { return _viewOffset; }
set { _viewOffset = value; }
}
/**
* Override this method to handle a "tap" on a balloon. By default, does nothing
* and returns false.
*
* @param index - The index of the item whose balloon is tapped.
* @param item - The item whose balloon is tapped.
* @return true if you handled the tap, otherwise false.
*/
protected bool OnBalloonTap(int index, OverlayItem item)
{
return false;
}
/**
* Override this method to perform actions upon an item being tapped before
* its balloon is displayed.
*
* @param index - The index of the item tapped.
*/
protected void OnBalloonOpen(int index) { }
protected override bool OnTap(int index)
{
_handler.RemoveCallbacks(_balloonRunnable);
_isInflating = true;
_handler.PostDelayed(_balloonRunnable, BALLOON_INFLATION_TIME);
_currentFocusedIndex = index;
_currentFocusedItem = CreateItem(index) as OverlayItem;
SetLastFocusedIndex(index);
OnBalloonOpen(index);
CreateAndDisplayBalloonOverlay();
if (_snapToCenter)
{
animateTo(index, _currentFocusedItem.Point);
}
return true;
}
/**
* Animates to the given center point. Override to customize how the
* MapView is animated to the given center point
*
* @param index The index of the item to center
* @param center The center point of the item
*/
protected void animateTo(int index, GeoPoint center)
{
_mc.AnimateTo(center);
}
/**
* Creates the balloon view. Override to create a sub-classed view that
* can populate additional sub-views.
*/
protected BalloonOverlayView CreateBalloonOverlayView()
{
return new BalloonOverlayView(GetMapView().Context, BalloonBottomOffset);
}
/**
* Expose map view to subclasses.
* Helps with creation of balloon views.
*/
protected MapView GetMapView()
{
return _mapView;
}
/**
* Makes the balloon the topmost item by calling View.bringToFront().
*/
public void BringBalloonToFront()
{
if (_balloonView != null)
{
_balloonView.BringToFront();
}
}
/**
* Sets the visibility of this overlay's balloon view to GONE and unfocus the item.
*/
public void HideBalloon()
{
if (_balloonView != null)
{
_balloonView.Visibility = ViewStates.Gone;
}
_currentFocusedItem = null;
}
/**
* Hides the balloon view for any other BalloonItemizedOverlay instances
* that might be present on the MapView.
*
* @param overlays - list of overlays (including this) on the MapView.
*/
private void HideOtherBalloons(List<Overlay> overlays)
{
foreach (var overlay in overlays)
{
if (overlay is BalloonItemizedOverlay && overlay != this)
{
((BalloonItemizedOverlay)overlay).HideBalloon();
}
}
}
public void HideAllBalloons()
{
if (!_isInflating)
{
List<Overlay> mapOverlays = _mapView.Overlays.ToList();
if (mapOverlays.Count > 1)
{
HideOtherBalloons(mapOverlays);
}
HideBalloon();
}
}
public override Java.Lang.Object Focus
{
get
{
return _currentFocusedItem;
}
set
{
_currentFocusedIndex = LastFocusedIndex;
_currentFocusedItem = (OverlayItem)value;
if (_currentFocusedItem == null)
{
HideBalloon();
}
else
{
CreateAndDisplayBalloonOverlay();
}
}
}
/**
* Creates and displays the balloon overlay by recycling the current
* balloon or by inflating it from xml.
* @return true if the balloon was recycled false otherwise
*/
private bool CreateAndDisplayBalloonOverlay()
{
bool isRecycled;
if (_balloonView == null)
{
_balloonView = CreateBalloonOverlayView();
_clickRegion = (View)_balloonView.FindViewById(Resource.Id.balloon_inner_layout);
_clickRegion.SetOnTouchListener(this);
_closeRegion = (View)_balloonView.FindViewById(Resource.Id.balloon_close);
if (_closeRegion != null)
{
if (!_showClose)
{
_closeRegion.Visibility = ViewStates.Gone;
}
else
{
_closeRegion.SetOnClickListener(this);
}
}
if (_showDisclosure && !_showClose)
{
View v = _balloonView.FindViewById(Resource.Id.balloon_disclosure);
if (v != null)
{
v.Visibility = ViewStates.Visible;
}
}
isRecycled = false;
}
else
{
isRecycled = true;
}
_balloonView.Visibility = ViewStates.Gone;
List<Overlay> mapOverlays = _mapView.Overlays.ToList();
if (mapOverlays.Count > 1)
{
HideOtherBalloons(mapOverlays);
}
if (_currentFocusedItem != null)
_balloonView.SetData(_currentFocusedItem);
GeoPoint point = _currentFocusedItem.Point;
MapView.LayoutParams layoutParams = new MapView.LayoutParams(
MapView.LayoutParams.WrapContent, MapView.LayoutParams.WrapContent, point,
MapView.LayoutParams.BottomCenter);
layoutParams.Mode = MapView.LayoutParams.ModeMap;
_balloonView.Visibility = ViewStates.Visible;
if (isRecycled)
{
_balloonView.LayoutParameters = layoutParams;
}
else
{
_mapView.AddView(_balloonView, layoutParams);
}
return isRecycled;
}
public void SetShowClose(bool showClose)
{
_showClose = showClose;
}
public void SetShowDisclosure(bool showDisclosure)
{
_showDisclosure = showDisclosure;
}
public void SetSnapToCenter(bool snapToCenter)
{
_snapToCenter = snapToCenter;
}
public bool IsInflating
{
get { return _isInflating; }
set { _isInflating = value; }
}
private class BalloonRunnable : Java.Lang.Object, IRunnable
{
private BalloonItemizedOverlay _item;
public BalloonRunnable(BalloonItemizedOverlay item)
{
_item = item;
}
public void Run()
{
_item.IsInflating = false;
}
}
public bool OnTouch(View v, MotionEvent e)
{
View l = ((View)v.Parent).FindViewById(Resource.Id.balloon_main_layout);
Drawable d = l.Background;
if (e.Action == MotionEventActions.Down)
{
if (d != null)
{
int[] states = { Android.Resource.Attribute.StatePressed };
if (d.SetState(states))
{
d.InvalidateSelf();
}
}
_startX = e.GetX();
_startY = e.GetY();
return true;
}
else if (e.Action == MotionEventActions.Up)
{
if (d != null)
{
int[] newStates = { };
if (d.SetState(newStates))
{
d.InvalidateSelf();
}
}
if (Java.Lang.Math.Abs(_startX - e.GetX()) < 40 && Java.Lang.Math.Abs(_startY - e.GetY()) < 40)
{
// call overridden method
OnBalloonTap(_currentFocusedIndex, _currentFocusedItem);
}
return true;
}
else
{
return false;
}
}
public void OnClick(View v)
{
HideBalloon();
}
}
}
关于c# - 在 Mono for Android 中使用 Java 代码的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12519083/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!