2016-11-14 54 views
1

我通過所有的互聯網搜索到了,但沒有找到答案如何實現我想要的。創建帶水龍頭外部輝光動畫的android按鈕

我想創建GlowButton(我的意思是它將按鈕擴展)類,它是按下或聚焦狀態時帶有外發光的按鈕。 見下面的圖片,明白我的意思:

enter image description here

此外發光應該出現,即消失動畫(只是改變不透明度)。

  1. 簡單的問題。我該如何做這個沒有動畫的按鈕。我知道我可以這樣創造的東西:

    <item android:state_pressed="true"> 
        <shape> 
         <solid android:color="@color/gray"/> 
         <stroke 
          android:width="4dp" 
          android:color="@color/orange" /> 
         <corners 
          android:radius="8dp" /> 
         <padding android:bottom="1dp" 
           android:top="1dp" 
           android:left="1dp" 
           android:right="1dp"/> 
        </shape> 
    </item> 
    ... 
    

    但有一個堅實的光芒。我需要如上圖所示的漸變發光。

  2. 難題。如何使用出現和消失動畫來做這個按鈕?用戶觸摸按鈕 - 在300毫秒內從0%不透明度到100%不透明度出現輝光。當用戶停止觸摸按鈕時,發光應該以類似的方式消失。

非常感謝你提前!

回答

1

您可以點擊 按鈕設置alpha動畫。你必須把輝光當做背景來使用按鈕,當你按下按鈕時,按鈕的背景將會像動畫一樣重複動畫,並重復計數1,這樣看起來陰影就會出現,並且 消失。集動畫時間爲300ms

在動畫文件夾alpha_animation.xml

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

    <alpha 
     android:duration="1000" 
     android:fromAlpha="0.0" 
     android:interpolator="@android:anim/accelerate_interpolator" 
     android:toAlpha="1.0" /> 

</set> 

佈局文件就像波紋管

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

    <Button 
     android:id="@+id/btnGlowBg" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_centerInParent="true" 
     android:background="@drawable/drawable_glow" 
     android:padding="20dp" 
    /> 
    <Button 
     android:id="@+id/btnPinButton" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_centerInParent="true" 
     android:padding="20dp" 
     android:text="10"/> 

</RelativeLayout> 

活動代碼

public class TestActivity extends AppCompatActivity { 
    Button btnGlowBg; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_test); 

     btnGlowBg = (Button) findViewById(R.id.btnGlowBg); 
     btnGlowBg.setVisibility(View.GONE); 
     findViewById(R.id.btnPinButton).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) {     
       btnGlowBg.setVisibility(View.VISIBLE); 
       final Animation startAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha_animation); 
       btnGlowBg.startAnimation(startAnimation); 


      } 
     }); 
    } 
}