在安卓系统中,当我给按钮设置背景图片后,点击按钮时没有任何视觉效果。
我需要给按钮添加一些效果,以便用户能够识别出按钮已被点击。
当按钮被点击时应该暗淡几秒钟。如何实现这个效果?
在安卓系统中,当我给按钮设置背景图片后,点击按钮时没有任何视觉效果。
我需要给按钮添加一些效果,以便用户能够识别出按钮已被点击。
当按钮被点击时应该暗淡几秒钟。如何实现这个效果?
我想再提供一种简单的方法来实现这个功能:如果你的ImageButton保留了它的背景,并且你没有将其设置为null,那么它就会像普通按钮一样工作,并在点击时显示与其他按钮完全相同的点击动画。隐藏背景的方法如下:
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="1dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="1dp"
android:src="@drawable/squareicon" />
内边距会遮挡背景,使按钮表现得与其他按钮类似。
Step: set a button in XML with onClick Action:
<Button
android:id="@+id/btnEditUserInfo"
style="?android:borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="@dimen/txt_height"
android:layout_gravity="center"
android:background="@drawable/round_btn"
android:contentDescription="@string/image_view"
android:onClick="edit_user_info"
android:text="Edit"
android:textColor="#000"
android:textSize="@dimen/login_textSize" />
Step: on button clicked show animation point
//pgrm mark ---- ---- ----- ---- ---- ----- ---- ---- ----- ---- ---- -----
public void edit_user_info(View view) {
// show click effect on button pressed
final AlphaAnimation buttonClick = new AlphaAnimation(1F, 0.8F);
view.startAnimation(buttonClick);
Intent intent = new Intent(getApplicationContext(), EditUserInfo.class);
startActivity(intent);
}// end edit_user_info
创建bounce.xml文件以进行动画
位置:
res->anim->bounce.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="100"
android:fromXScale="0.9"
android:fromYScale="0.9"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
final Animation myAnim = AnimationUtils.loadAnimation(this,R.anim.bounce);
button.startAnimation(myAnim);
postDelayed
来使颜色过滤器持续一小段时间,以使其更加明显: @Override
public boolean onTouch(final View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
v.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP: {
v.postDelayed(new Runnable() {
@Override
public void run() {
v.getBackground().clearColorFilter();
v.invalidate();
}
}, 100L);
break;
}
}
return false;
}
您可以根据需要更改延迟100L的值。
<item>
<bitmap android:src="@drawable/YOURIMAGE"/>
</item>
从@Ljdawson给我们的第一个答案中得到。
<scale
android:duration="90"
android:fromYScale="0.9"
android:fromXScale="0.9"
android:toYScale="1.0"
android:toXScale="1.0"
android:pivotY="50%"
android:pivotX="50%"/>
Animation animationButton = AnimationUtils.loadAnimation(MyActivity.this,R.anim.bounce_button);
animationButton.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
startActivity(intent);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.startAnimation(animationButton);
}
});
'''
I hope this will help.