我怎么能让按钮在按下时略微增大,在释放时再缩小呢?这是为了通过使用大小和不同的颜色来突出显示按下的按钮。
问候, Kiki
duration="0"
来禁用动画。例如:<!-- res/layout/my_layout.xml -->
<LinearLayout ...
android:stateListAnimator="@animator/zoom_animator">...</layout>
<!-- res/animator/zoom_animator -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<set>
<objectAnimator android:propertyName="scaleX" android:valueTo="1.0" android:duration="0" />
<objectAnimator android:propertyName="scaleY" android:valueTo="1.0" android:duration="0" />
</set>
</item>
<item android:state_pressed="true">
<set>
<objectAnimator android:propertyName="scaleX" android:valueTo="1.1" android:duration="0" />
<objectAnimator android:propertyName="scaleY" android:valueTo="1.1" android:duration="0" />
</set>
</item>
</selector>
还有两个关于动画缩放的类似问题:
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
//Code to convert height and width in dp.
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, getResources().getDisplayMetrics());
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 140, getResources().getDisplayMetrics());
if (event.getAction() == MotionEvent.ACTION_DOWN) {
layoutParams.width = width + 5;
layoutParams.height = height + 5;
SearchButton.setLayoutParams(layoutParams);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
layoutParams.width = width;
layoutParams.height = height;
SearchButton.setLayoutParams(layoutParams);
}
return false;
}
});
在你的活动中
Private Button myButton;
//...
protected void onCreate(Bundle savedInstanceState) {
//...
myButton = (Button) findViewById(R.id.my_button);
_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myButton.setWidth(50);
myButton.setHeight(50);
}
});
}
//Try it
Private Button myButton;
//...
protected void onCreate(Bundle savedInstanceState) {
//...
myButton = (Button) findViewById(R.id.my_button);
_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myButton.setWidth(myButton.getWidth()+5);
myButton.setHeight(myButton.getHeight()+5);
}
});
}
android:state_pressed="false"
,否则这个答案不起作用。 - Saint