创建覆盖 ImageView 动画 Google 地图

2024-01-19

我正在尝试使我的叠加图像执行以下操作:

  • 地图的 onClick / onDrag ,在地图中间显示恒定图像 这是一个引脚
  • onTouchUp ,将标记图像更改为加载标记和一次数据 加载完整更改将图像加载到带有文本的新图像。

这是与我的问题非常相似的解决方案:

到目前为止我做了什么?

  • 在我的谷歌地图中间放置一个图像视图,然后得到 该 imageView 的位置使用mMap.getCameraPosition().target它给出了大约中间位置坐标setOnCameraChanged,我知道这已被弃用,我们有一个更好的 解决方案?。
  • 其余部分我无法弄清楚,尽管我读过几个SO问题,声称他们已经实现了将framelayout包装在谷歌地图片段上,并对其应用了onTouch,但也找不到该问题的真实答案。

我的 xml 片段发生了这一切,如下所示:

<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:context=".fragments.MovableMapFragment">

    <fragment
        android:id="@+id/map_frag_fmm"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tablayout_global_tabs"
        android:layout_alignParentTop="true" />
<! -- long xml ahead -->

有人知道如何实现这种行为吗?


最好的解决方案是使用 RxJava/RxAndroid 并用它执行后台任务,但我无法在没有看到您的代码的情况下使用 rx 发布解决方案,并且 @user27799 已经有一个示例,@Manas Chaudhari 也提供了逻辑的很好解释(在下面)。

//bunch of imports
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback
{

  private GoogleMap mGoogleMap;
  private FrameLayout frameLayout, circleFrameLayout;
  private ProgressBar progress;
  private TextView textView;
  private int circleRadius;
  private boolean isMoving = false;
  private SupportMapFragment mapFragment;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    initViews();
  }

  private void initViews() {

    frameLayout = (FrameLayout) findViewById(R.id.map_container);

    circleFrameLayout = (FrameLayout) frameLayout.findViewById(R.id.pin_view_circle);
    textView = (TextView) circleFrameLayout.findViewById(R.id.textView);
    progress = (ProgressBar) circleFrameLayout.findViewById(R.id.profile_loader);

    mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

  private void moveMapCamera() {
    if (mGoogleMap == null) {
        return;
    }

    CameraUpdate center = CameraUpdateFactory
            .newLatLng(new LatLng(40.76793169992044, -73.98180484771729));
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);

    mGoogleMap.moveCamera(center);
    mGoogleMap.animateCamera(zoom);
}

  @Override
  public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;

    mGoogleMap.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {

        /* User starts moving map - change your pin's image here 
        */
        @Override
        public void onCameraMoveStarted(int i) {
            isMoving = true;
            textView.setVisibility(View.GONE);
            progress.setVisibility(View.GONE);
            Drawable mDrawable;
            if (Build.VERSION.SDK_INT >= 21)
                mDrawable = getApplicationContext().getResources().getDrawable(R.drawable.circle_background_moving, null);
            else
                mDrawable = getApplicationContext().getResources().getDrawable(R.drawable.circle_background_moving);

            circleFrameLayout.setBackground(mDrawable);
            resizeLayout(false);
        }
    });

    /*User stoped moving map -> retrieve location and do your background task */
    mGoogleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
        @Override
        public void onCameraIdle() {

            isMoving = false;
            textView.setVisibility(View.INVISIBLE);
            progress.setVisibility(View.VISIBLE);
            resizeLayout(true);

            /* this is just an example that simulates data loading
               you shoud substitute it with your logic
            */
            new Handler().postDelayed(new Runnable() {
                public void run() {

                    Drawable mDrawable;
                    if (Build.VERSION.SDK_INT >= 21)
                        mDrawable = getApplicationContext().getResources().getDrawable(R.drawable.circle_background, null);
                    else
                        mDrawable = getApplicationContext().getResources().getDrawable(R.drawable.circle_background);

                    if (!isMoving) {
                        circleFrameLayout.setBackground(mDrawable);
                        textView.setVisibility(View.VISIBLE);
                        progress.setVisibility(View.GONE);
                    }
                }
            }, 1500);
        }
    });

    MapsInitializer.initialize(this);
    moveMapCamera();
  }

  //resing circle pin
  private void resizeLayout(boolean backToNormalSize){
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) circleFrameLayout.getLayoutParams();

    ViewTreeObserver vto = circleFrameLayout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            circleFrameLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            circleRadius = circleFrameLayout.getMeasuredWidth();
        }
    });

    if (backToNormalSize) {
        params.width = WRAP_CONTENT;
        params.height = WRAP_CONTENT;
        params.topMargin = 0;

    } else {
        params.topMargin = (int) (circleRadius * 0.3);
        params.height = circleRadius - circleRadius / 3;
        params.width = circleRadius - circleRadius / 3;
    }

    circleFrameLayout.setLayoutParams(params);
  }

}

屏幕中心带有图钉的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/map_container"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

<fragment xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/map"
          android:name="com.google.android.gms.maps.SupportMapFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context="com.example.mapstest.MapsActivity" />

<FrameLayout
    android:id="@+id/pin_view_line"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="@dimen/line_top_margin"
    android:background="@drawable/line_background"/>

<FrameLayout
    android:id="@+id/pin_view_circle"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/circle_background">

    <TextView
        android:id="@+id/textView"
        android:layout_margin="@dimen/inner_circle_margin"
        android:layout_width="@dimen/inner_circle_radius"
        android:layout_height="@dimen/inner_circle_radius"
        android:layout_gravity="top|center_horizontal"
        android:gravity="center"
        android:text="12 min"
        android:textSize="@dimen/text_size"
        android:textColor="@android:color/white"/>

    <ProgressBar
        android:id="@+id/profile_loader"
        android:layout_margin="@dimen/inner_circle_margin"
        android:layout_width="@dimen/inner_circle_radius"
        android:layout_height="@dimen/inner_circle_radius"
        android:indeterminate="true"
        android:layout_gravity="top|center_horizontal"
        android:visibility="gone"
        android:contentDescription="@null"/>

</FrameLayout>

不移动时的圆形背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="oval">    
<solid android:color="@android:color/holo_green_dark"/>
<stroke
    android:width="@dimen/stroke_width"
    android:color="@android:color/black"/>
<size
    android:width="@dimen/circle_radius"
    android:height="@dimen/circle_radius"/>
</shape>

地图移动时的圆形背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="oval">    
<solid android:color="@android:color/black"/>
<size
    android:width="@dimen/circle_radius"
    android:height="@dimen/circle_radius"/>
</shape>

线路背景文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">    
<solid android:color="@android:color/black"/>
<size
    android:width="@dimen/line_width"
    android:height="@dimen/line_height"/>
</shape>

方面:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <dimen name="circle_radius">48dp</dimen>
  <dimen name="line_width">4dp</dimen>
  <dimen name="line_height">40dp</dimen>
  <dimen name="stroke_width">4dp</dimen>
  <dimen name="text_size">10sp</dimen>
  <dimen name="inner_circle_radius">40dp</dimen>
  <dimen name="inner_circle_margin">4dp</dimen>
  <dimen name="line_top_margin">20dp</dimen>
</resources>

我希望它对您有所帮助,但如果您有任何问题,请随时提出,或者您发现需要改进的地方 - 编辑我的解决方案。

Cheers.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

创建覆盖 ImageView 动画 Google 地图 的相关文章

  • Android-如何将 android.net.Uri 对象转换为 java.net.URI 对象?

    我正在尝试获得一个FileInputStream用户从图片库中选择的图像上的对象 这是安卓URI由返回android provider MediaStore Images Media INTERNAL CONTENT URI content
  • 使用 JSONArray 还是普通数组来存储/读取数据更有效?

    我正在使用一个连接到PHP MySQL返回所有内容的服务器JSON格式 例如 用户列表作为JSONArray of JSONObject 每个对象都包含单个用户的信息 姓名 位置 电话号码等 处理这种格式的信息时 将所有内容保留在其中会更有
  • 如何替换 Android 中已弃用的 Bundle/Argument get(key) 调用

    我有以下扩展函数 允许我在应用程序活动和片段之间传递捆绑数据项 inline fun
  • 使用 Fragment 在工具栏中实现 SearchView

    当前情况 我的应用程序主页由导航抽屉组成 因此我将视图作为片段加载 我的工具栏中也有搜索图标 我在中实现了它menu xml 下一步我实施了SearchView通过以下问题的答案来获取搜索图标在工具栏中实现搜索 https stackove
  • 拖动时跳转 ImageView。 getX() 和 getY() 值正在跳跃

    我创建了一个用于拖动视图的 onTouchListener 如果我使用的话 图像可以顺利拖动getRawX and getRawY 问题是 当您向下放置第二个指针然后抬起第一个指针时 图像将跳转到第二个指针 此 onTouchListene
  • 覆盖 Android 中的电源按钮

    我正在开发一个应用程序 其中我需要在按下电源按钮时执行一个操作 但不幸的是我无法处理按下电源按钮时的操作 我尝试使用 onKeyDown 和dispatchKeyEvent 方法 但似乎没有任何效果 任何人都可以建议我解决这个问题的任何其他
  • 从ListView中隐藏行而不占用空间

    我有一个带有关联 ArrayAdapter 的 ListView 它在多个活动中显示其内容 不幸的是 现在有必要 我的 ListView 在其中一项设置中不显示其所有元素 而仅显示 属性 未设置为 true 的元素 我想避免使用两个具有不同
  • 如何禁用操作栏上“向上”按钮的翻转?

    背景 我做了一个 应用程序管理器 https play google com store apps details id com lb app manager 替代应用程序 我希望添加 RTL 从右到左 语言的翻译 因为我知道在某些 And
  • React Native Expo StackNavigator 重叠通知栏

    我正在尝试为我的 React Native Expo 应用程序实现导航栏 这里有一个问题 dependencies expo 18 0 3 react 16 0 0 alpha 12 react native 0 45 1 react na
  • 选项卡主机内的 Android Fragment 视图状态 [重复]

    这个问题在这里已经有答案了 可能的重复 使用 Fragment 为 Android 中的每个选项卡单独的返回堆栈 https stackoverflow com questions 6987334 separate back stack f
  • 在android中,将相机预览流到视图上

    我想将 Android 相机的相机预览流式传输到视图上 目的是随后使用 onDraw 将各种内容添加到视图中 我不需要随时实际捕捉图像 它不必是最高质量或每秒最大数量的帧 有谁知道如何做到这一点 将其添加到您的 xml 中
  • 当 minifyEnabled 为 true 时 Android 应用程序崩溃

    我正在使用多模块应用程序 并且该应用程序崩溃时minifyEnabled true in the installed模块的build gradle 以下是从游戏控制台检索到的反混淆堆栈跟踪 FATAL EXCEPTION Controlle
  • 安卓。 CalendarView...一次仅显示一个月的日历

    我正在使用 CalendarView 其中我想一次仅查看一个月的日历并滚动查看下个月 但 CalendarView 一次显示所有月份 下面是我的代码
  • 无法使用 findViewById() 找到视图

    我找不到TextView通过致电findViewById 即使 ID 确实存在 OtherActivity public class OtherActivity extends Activity Override protected voi
  • Android SearchView 在启动时隐藏键盘

    我有一个小问题正在尝试解决 当我打开应用程序时 键盘会显示输入搜索视图的查询 不过 我只想在单击搜索视图时显示键盘 我该如何解决 Thanks 这对我有用 用于隐藏焦点的代码 searchView SearchView view findV
  • 谷歌地图的地址建议

    有人知道是否有任何方法可以重现 ajax 建议框 例如http maps google com http maps google com 我的网页中有使用 google 地图 api 的吗 例如 如果有人写下 15 Avenue 的建议列表
  • 当目标小于 Android O 时,如何在 Android O 上创建快捷方式?

    背景 Android O 对快捷方式的工作方式进行了各种更改 https developer android com preview behavior changes html as https developer android com
  • 如何在android中通过蓝牙向配对设备发送短信?

    在我的应用程序中 我想通过蓝牙发送和接收短信 我可以在列表视图中看到配对设备名称和地址的列表 但是当我尝试向配对设备发送文本时 什么也没有发生 在其他设备中没有收到文本 这是我向配对设备发送消息的代码 private void sendDa
  • Android 标记如何实现拖放?

    你好 我正在 Android 中开发 MapView 应用程序 我有三个标记 我希望稍后能够使用 Google Map API getlocation function 为了尝试一下 我想使用拖放功能移动标记 然后检查位置 任何人都可以通过
  • 检查应用程序是否在 Android Market 上可用

    给定 Android 应用程序 ID 包名称 如何以编程方式检查该应用程序是否在 Android Market 上可用 例如 com rovio angrybirds 可用 而 com random app ibuilt 不可用 我计划从

随机推荐

  • 插入符号交叉验证中的预处理

    我有一个关于数据预处理的问题需要澄清 据我了解 当我们通过交叉验证调整超参数并估计模型性能时 我们需要在交叉验证中进行 而不是预处理整个数据集 换句话说 在交叉验证中 我们对训练折叠进行预处理 然后使用相同的预处理参数来处理测试折叠并进行预
  • .NET 示例 VCF 阅读器

    有谁知道使用 C NET 从 VCF 文件中提取数据的好示例 内联回复或网络教程 现在还有人用VCF文件吗 对于联系人管理系统来说 这值得吗 让我有点惊讶的是 它没有内置到 NET Framework 的任何地方 但我确实找到了本教程 我计
  • 将 ExpandoObject 持久保存到 MongoDB

    我有一个具有任意数量属性的 ExpandoObject 我想将这些属性作为 BsonDocument 保存到 MongoDB 数据库 我尝试使用以下代码来执行此操作 private BsonDocument GetPlayerDocumen
  • 如何在 onStart() 方法中从 Firebase 远程配置实现 fetch() ?

    我正在尝试实现调用 Firebase 远程配置fetch 中的方法onStart 我以为这会很容易 但经过几次尝试后却发现并非如此 首先 我想尽快检查新的配置值用户打开应用程序 and 超出缓存过期时间 这就是我选择的原因onStart 方
  • 如何禁用/关闭/刷新 couchdb 缓存

    我有一个列表 其中对文档进行了一些基本身份验证 我遇到的问题是列表正在缓存 因此除非我更新修订 ID 否则用户将看不到他们具有访问权限 如何显示非缓存列表 if req userCtx name doc permissions owner
  • 最小化二分图中的交叉数

    在为不相关的东西绘制图表时 我遇到了以下算法问题 我们有一个二部图的平面图 其中不相交的集合按列排列 如图所示 我们如何重新排列每列内的节点以使边缘交叉的数量最小化 我知道这个问题对于一般图来说是 NP 困难的 link http en w
  • 调试使用 ES6 模块的 JavaScript 代码

    TL DR 如何从调试器访问 ES 模块中定义的变量 函数 名称 更多背景信息 我是一位经验相对丰富的 JavaScript 程序员 但对模块还是个新手 我已经按照 MDN 上的教程进行操作 https developer mozilla
  • CUDA 就地转置错误

    我正在实现一个 CUDA 程序来转置图像 我创建了 2 个内核 第一个内核进行了异位转置 并且适用于任何图像尺寸 然后我创建了一个用于方形图像就地转置的内核 但是 输出不正确 图像的下三角形被转置 但上三角形保持不变 生成的图像在对角线上有
  • 如何在 android room 和 rxjava 2 中插入数据并获取 id 作为输出参数?

    插入查询 Insert onConflict OnConflictStrategy REPLACE long insertProduct Product product product id is auto generated 查看模型 p
  • tvos UISegmentedControl 焦点样式不改变

    我想在 tvOS 中突出显示 UISegmentedControl 时更改其背景颜色 Normally Segment display like following When change focus for change selected
  • 训练神经网络时出现极小或 NaN 值

    我正在尝试在 Haskell 中实现神经网络架构 并在 MNIST 上使用它 我正在使用hmatrix线性代数包 我的训练框架是使用pipes包裹 我的代码可以编译并且不会崩溃 但问题是 层大小 例如 1000 小批量大小和学习率的某些组合
  • 如何将 DATETIME 转换为 mysql 中的 DATE?

    我的查询是这样的 我有一堆条目 我想按日期对它们进行分组 但我的数据库中没有日期 而是有一个日期时间字段 我该怎么办 select from follow queue group by follow date cast follow dat
  • 反序列化 JSON 对象的一部分并将其序列化回来,其余属性保持不变

    我有一些 JSON 想要将其反序列化为 C 类的实例 但是 该类并不具有与原始 JSON 匹配的所有字段 属性 我希望能够修改类中的属性值 然后将其序列化回 JSON 并且原始 JSON 中的剩余字段和属性仍然完好无损 例如 假设我有以下
  • 强制 TkInter Scale 滑块捕捉到鼠标

    当 GUI 有 TkInter 时Scale当他们单击刻度上的某个位置时 默认行为似乎是沿着刻度向鼠标方向滑动滑块 然后意外地经过鼠标 我想要的是让滑块在用户单击滑块上的任意位置时始终跳转到并保持连接到用户的鼠标点 如果他们单击刻度上的特定
  • Int 和 Integer 有什么区别?

    在 Haskell 中 a 和 a 有什么区别Int and an Integer 答案记录在哪里 Integer 是任意精度 类型 它将保存任何数字 no 无论多大 直到极限 你机器的内存 这意味着你从来没有 算术溢出 在另一 手也意味着
  • 重写 Wildfly 引擎

    我想知道是否可以在没有任何第三方库的情况下使用 Wildfly 应用程序服务器的重写引擎 我尝试过使用重写阀 https help openshift com hc en us articles 202398810 How to redir
  • Rails 控制器中的实例和类变量

    我是 Rails 和 ruby 的新手 我正在研究类和实例变量的概念 我理解其中的区别 但是当我在 Rails 中使用控制器进行尝试时 它让我感到困惑 我所做的是在类方法之外声明一个类和实例变量 class BooksController
  • 有没有办法强制使用 Zend_Auth 进行身份验证?

    我正在使用 Zend Auth 和 cookie 会话持久性 我似乎无法弄清楚如何强制使用此类进行身份验证 有没有办法强制 Zend Auth 相信它已经作为用户进行身份验证 Zend Auth getInstance gt getStor
  • 直接自引用导致循环超类问题 JSON

    我尝试了在搜索时发现的几件事 但没有任何帮助 或者我没有正确实现它 我收到错误 Direct self reference leading to cycle through reference chain io test entity bo
  • 创建覆盖 ImageView 动画 Google 地图

    我正在尝试使我的叠加图像执行以下操作 地图的 onClick onDrag 在地图中间显示恒定图像 这是一个引脚 onTouchUp 将标记图像更改为加载标记和一次数据 加载完整更改将图像加载到带有文本的新图像 这是与我的问题非常相似的解决