【问题标题】:Create android button with tap outer glow animation创建带有点击外发光动画的android按钮
【发布时间】:2017-03-27 15:14:16
【问题描述】:
我搜索了所有互联网,但没有找到如何实现我想要的答案。
我想创建 GlowButton(我的意思是它将是按钮扩展)类,它是在按下或聚焦状态时具有外部发光的按钮。
请看下图以了解我的意思:
这种外发光应该随着动画出现和消失(只是改变不透明度)。
-
简单的问题。如何在没有动画的情况下执行此按钮。我知道我可以创建这样的东西:
<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>
...
但是有一个坚实的光芒。我需要如上图所示的渐变光晕。
难题。如何使用出现和消失动画执行此按钮?用户触摸按钮 — 发光在 300 毫秒内从 0% 不透明度变为 100% 不透明度。当用户停止触摸按钮时,发光应该以类似的方式消失。
非常感谢您!
【问题讨论】:
标签:
android
xml
button
view
android-custom-view
【解决方案1】:
您可以在单击时设置 alpha 动画
按钮。你必须把发光作为背景按钮,当你
按下按钮按钮的背景将像阿尔法一样动画
重复计数为 1 的动画,因此看起来像出现了阴影,并且
消失。设置动画时间300ms
anim 文件夹中的 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);
}
});
}
}