如何按下按钮的尺寸会在按下时略微增加,在释放按钮时又会再次减小?这是为了突出显示按下的按钮,除了使用不同颜色的尺寸外。
此致 基基
答案 0 :(得分:2)
将Button
字段设为一个字段,并在与其关联的OnTouchListener
中修改其字段大小。使用OnTouchListener,您可以收听不同的MotionEvents
,例如ACTION_DOWN
和ACTION_UP
。
答案 1 :(得分:1)
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;
}
});
答案 2 :(得分:0)
从API 21(Lollipop)开始,基于stateListAnimator属性的纯XML解决方案可用。即使其主要目的是根据状态变化为视图属性设置动画,也可以通过设置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>
关于动画比例尺,还有两个类似的问题:
答案 3 :(得分:-1)
在您的活动中
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);
}
});
}
答案 4 :(得分:-1)
//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);
}
});
}