【问题标题】:Click animation for a button with an image单击带有图像的按钮的动画
【发布时间】:2025-11-24 22:35:01
【问题描述】:

我有这个按钮,透明背景,由图片和文字组成:

<Button
    android:id="@+id/settingsBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:drawableTop="@drawable/settings"
    android:text="@string/settings" />

当我点击它时,我希望有一种点击的感觉,就像图像闪烁一样。 我试图用形状来做到这一点,但它们的效果对图像不起作用。 你能帮帮我吗?

【问题讨论】:

标签: android xml android-studio button onclick


【解决方案1】:

您可以尝试在单击按钮时使用动画来控制按钮的不透明度

Button btn = findViewById(R.id.btn_SignIn);

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Animation animation = new AlphaAnimation(1, 0.5f); //to change visibility from visible to half-visible
        animation.setDuration(50); // 100 millisecond duration for each animation cycle
        animation.setRepeatMode(Animation.REVERSE); //animation will start from end point once ended.
        btn.startAnimation(animation); //to start animation
    }
});

结果

【讨论】:

    【解决方案2】:

    尝试使用这个:

    style="?android:attr/borderlessButtonStyle"
    

    【讨论】: