代码之家  ›  专栏  ›  技术社区  ›  the newbie coder

如何在Android中用动画从中心慢慢填充圆形的背景色

  •  3
  • the newbie coder  · 技术社区  · 7 年前

    下面是我找到的一个示例片段 here

    $('div').click(function() {
      $(this).toggleClass('selected');
    });
    div.button {
      height: 27px;
      width: 27px;
      border-radius: 50%;
      background: green;
    }
    
    div.button:after {
      content: '';                 /* needed for rendering */
      position: relative;          
      display: block;              /* so we can set width and height */
      border-radius: 50%;
      height: 0%;
      width: 0%;
      margin: auto;                /* center horizontally */
      background: red;
      top: 50%;                    /* center vertically */
      transform: translateY(-50%); /* center vertically */
      transition: 1s;
    }
    
    div.button.selected:after {
      height: 100%;
      width: 100%;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="button">
    
    </div>
    1 回复  |  直到 7 年前
        1
  •  1
  •   Amrutha Saj    7 年前

    尝试以下操作

    在您的活动java中

      circle2 = (ImageView) findViewById(R.id.circle2);
        ScaleAnimation fade_in =  new ScaleAnimation(0f, 1f, 0f, 1f, 
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        fade_in.setDuration(1000);     // animation duration in milliseconds
        fade_in.setFillAfter(true);    // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
        circle2.startAnimation(fade_in);
    

    活动xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    
    
    >
    
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/circle" />
    
    <ImageView
        android:id="@+id/circle2"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/circle_two" />
    </FrameLayout>
    

    在你的画圈里。xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="@color/colorAccent" />
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/colorAccent" />
        </shape>
    </item>
    </selector>
    

    类似地创建圆圈2。xml