2015-10-20 62 views
0

有没有办法在MapView上创建连锁效果?MapView上的波动动画效果

我的布局显示在帖子结尾。 我知道关于android:background="?attr/selectableItemBackground",它对不同类型的视图很好,但不适用于MapView。

我想要的是对header_containermap_view的不可分割的连锁反应。

为了达到这个目的,我试图把它们放在RelativeLayout中,并在它们上面创建与selectableItemBackground的视图,但是我不喜欢这个解决方案,因为我只能通过指定重叠视图的确切高度来使它工作,我希望它以匹配标题和地图的高度。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:map="http://schemas.android.com/tools" 
    android:id="@+id/cv_root" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="@dimen/card_corner_radius" 
    card_view:cardElevation="@dimen/card_elevation" 
    card_view:cardUseCompatPadding="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/header_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/selectableItemBackground" 
      android:orientation="vertical" 
      android:paddingLeft="@dimen/card_horizontal_padding" 
      android:paddingRight="@dimen/card_horizontal_padding"> 

      <TextView 
       android:id="@+id/tv_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/card_text_small_margin" 
       android:layout_marginTop="@dimen/card_text_big_margin" 
       android:text="Name" 
       android:textSize="@dimen/card_title_text_size" /> 

      <TextView 
       android:id="@+id/tv_subtitle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/card_text_small_margin" 
       android:text="Address" 
       android:textSize="@dimen/card_subtitle_text_size" /> 
     </LinearLayout> 

     <com.google.android.gms.maps.MapView 
      android:id="@+id/map_view" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/card_media_height" 
      map:liteMode="true" 
      map:mapType="none" /> 

     <LinearLayout 
      android:id="@+id/button_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingBottom="@dimen/card_action_padding" 
      android:paddingTop="@dimen/card_action_padding"> 

      <android.support.v7.widget.AppCompatButton 
       android:id="@+id/b_drive" 
       style="?android:attr/borderlessButtonStyle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="@dimen/card_action_padding" 
       android:layout_marginRight="@dimen/card_action_padding" 
       android:minHeight="0dp" 
       android:minWidth="0dp" 
       android:padding="@dimen/card_action_padding" 
       android:text="@string/card_action_drive" 
       android:textColor="@color/color_primary" /> 

      <android.support.v7.widget.AppCompatButton 
       android:id="@+id/b_walk" 
       style="?android:attr/borderlessButtonStyle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:minHeight="0dp" 
       android:minWidth="0dp" 
       android:padding="@dimen/card_action_padding" 
       android:text="@string/card_action_walk" 
       android:textColor="@color/color_primary" /> 
     </LinearLayout> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

回答

0

下面的类是将任何覆盖放在任何视图之上的配方。 您只需将extends View替换为extends Anything,然后使用叠加层(在您的具体问题中为Ripple)呼叫setOverlay(...);

所以你的情况将是:

extends MapView 

然后

mapView.setOverlay(mapView.getResources().getDrawable(R.drawable.ripple)); 

这里是 “配方” 类

public class PressedStateView extends View { 
    public PressedStateView(Context context) { 
     super(context); 
    } 

    public PressedStateView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public PressedStateView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public PressedStateView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    private Drawable overlay; 

    public void setOverlay(Drawable overlay) { 
     if (this.overlay != null) { 
     this.overlay.setCallback(null); 
     } 
     this.overlay = overlay; 
     this.overlay.setCallback(this); 
     this.overlay.setBounds(0, 0, getWidth(), getHeight()); 
     invalidate(); 
    } 

    @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     this.overlay.setBounds(0, 0, w, h); 
    } 

    @Override protected boolean verifyDrawable(Drawable who) { 
     return super.verifyDrawable(who) || who == overlay; 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override 
    public void drawableHotspotChanged(float x, float y) { 
     super.drawableHotspotChanged(x, y); 
     if (overlay != null) { 
     overlay.setHotspot(x, y); 
     } 
    } 

    @Override protected void dispatchDraw(Canvas canvas) { 
     super.dispatchDraw(canvas); 
     if (overlay != null) { 
     overlay.draw(canvas); 
     } 
    } 
} 
+0

谢谢你的回答,我现在正在测试它,请问您能否显示您的'R.drawable.ripple'? – Florin

+0

只是一个普通的XML可绘制:https://developer.android.com/reference/android/graphics/drawable/RippleDrawable.html你必须看到你想要的颜色,如果有选择与否。这是你的设计。 – Budius