高德地图添加类似手机百度地图屏幕下方弹出框

主要采用的是PopupWindow做的弹出框

首先 需要定义两个xml动画:push_buttom_in.xml 和 push_buttom_out.xml文件

代码:

push_buttom_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

</set>

push_bottom_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="0"
        android:toYDelta="50%p" />
</set>

第二步:需要自定义xml布局用于显示,自己修改的样式可以用于参考

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="130dp"
              android:background="#ffffff">
    <LinearLayout
        android:id="@+id/id_pop_layout"
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/agent_name"
            android:layout_marginLeft="14dp"
            android:layout_marginRight="14dp"
            android:layout_marginTop="11dp"
            android:layout_width="match_parent"
            android:layout_height="24dp"
            android:textSize="16sp"
            android:textColor="@color/black_one"/>
        <TextView
            android:id="@+id/agent_addr"
            android:layout_marginLeft="14dp"
            android:layout_marginRight="14dp"
            android:layout_marginTop="2dp"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:textSize="15sp"
            android:textColor="@color/black_two"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_marginTop="9dp"
                android:id="@+id/check_LL"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                >
                <ImageView
                    android:layout_marginLeft="30dp"
                    android:layout_width="17dp"
                    android:layout_height="18dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/inforwindow_check"/>
                <TextView
                    android:layout_marginLeft="9dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/inforwindow_check"
                    android:textSize="15sp"
                    android:textColor="@color/black_one" />
            </LinearLayout>
            <LinearLayout
                android:layout_marginTop="9dp"
                android:id="@+id/warning_LL"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                >
                <ImageView
                    android:layout_marginLeft="30dp"
                    android:layout_width="19dp"
                    android:layout_height="18dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/inforwindow_warning"/>
                <TextView
                    android:layout_marginLeft="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/inforwindow_warning"
                    android:textSize="15sp"
                    android:textColor="@color/black_one"/>
            </LinearLayout>
            <LinearLayout
                android:layout_marginTop="9dp"
                android:id="@+id/more_LL"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                >
                <ImageView
                    android:layout_marginLeft="30dp"
                    android:layout_width="19dp"
                    android:layout_height="18dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/inforwindow_more"/>
                <TextView
                    android:layout_marginLeft="8dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/inforwindow_more"
                    android:textSize="15sp"
                    android:textColor="@color/black_one"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

第三步:创建CustomPopupWindow.java继承PopupWindow 重写里面的方法
代码如下:
public class CustomPopupWindow extends PopupWindow implements View.OnClickListener {

    private View check;
    private View warning;
    private View more ;
    private TextView name;
    private TextView addr ;
    private OnItemClickListener mListener;
    private View mPopView;

    public CustomPopupWindow(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init(context);
        setPopupWindow();
        check.setOnClickListener(this);
        warning.setOnClickListener(this);
        more.setOnClickListener(this);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        //绑定布局
        mPopView = inflater.inflate(R.layout.person_view, null);
        check = mPopView.findViewById(R.id.check_LL);
        warning = mPopView.findViewById(R.id.warning_LL);
        more =  mPopView.findViewById(R.id.more_LL);
        name =  mPopView.findViewById(R.id.agent_name);
        addr =  mPopView.findViewById(R.id.agent_addr);
    }

    /**
     * 设置窗口的相关属性
     */
    @SuppressLint("InlinedApi")
    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置可弹出窗口
        this.setAnimationStyle(R.style.mypopwindow_anim_style);// 设置动画
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        mPopView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = mPopView.findViewById(R.id.id_pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    /**
    * 修改文本
    */
    public void setName(String agentname) {
        name.setText(agentname);
    }

    public void setAddr(String address) {
        addr.setText(address);
    }

    /**
     * 定义一个接口,公布出去 在Activity中操作按钮的单击事件
     */
    public interface OnItemClickListener {
        void setOnItemClick(View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.setOnItemClick(v);
        }
    }
}

第四步:在mainActivity里面调用

代码:只贴相关的一部分 CustomPopupWindow以接口引入
//marker单击事件
@Override
public boolean onMarkerClick(Marker marker) {
    for (int i=0;i<mList.size();i++){
        if (marker.equals(mList.get(i))) {
            if (aMap != null) {
                marker.setInfoWindowEnable(false);
                jumpPoint(marker);
                mPop = new CustomPopupWindow(this);
                mPop.setOnItemClickListener(this);
                mPop.setName(title[i]);
                mPop.setAddr(addrlst.get(i));
                mPop.showAtLocation(MainActivity.this.findViewById(R.id.map), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        }
    }


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wentianyunhe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值