Есть к примеру кнопка серого цвета. Как сделать, чтобы при нажатии на неё, она плавно становилась красной, а при отпускании красный цвет плавно исчезал, и она опять становилась серой?
Ответ
Попробуйте так. Это пример. values/colors.xml:#ff0000
#a9a9a9
xml activity/fragment(a):
activity/fragment: final Button button = findViewById(R.id.button_hello); button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()){
case MotionEvent.ACTION_DOWN:{
ObjectAnimator colorFade = ObjectAnimator.ofObject(button, "backgroundColor" /*view attribute name*/, new ArgbEvaluator(), getApplicationContext().getResources().getColor(R.color.gray) /*from color*/, getApplicationContext().getResources().getColor(R.color.red) /*to color*/);
colorFade.setDuration(1000);
colorFade.setStartDelay(200);
colorFade.start();
break;
}
case MotionEvent.ACTION_UP:{
ObjectAnimator colorFade = ObjectAnimator.ofObject(button, "backgroundColor" /*view attribute name*/, new ArgbEvaluator(), getApplicationContext().getResources().getColor(R.color.red) /*from color*/, getApplicationContext().getResources().getColor(R.color.gray) /*to color*/);
colorFade.setDuration(1000);
colorFade.setStartDelay(200);
colorFade.start();
break;
}
}
return false;
}
});
Комментариев нет:
Отправить комментарий