4

如何使按钮的大小在按下时略微增加并在释放时再次减小?这是为了突出显示按下的按钮,除了使用不同的颜色外,还使用大小。

问候,琪琪

4

5 回答 5

2

创建一个字段,并在与之关联的情况下修改Button其大小。OnTouchListener使用 OnTouchListener 您可以监听不同的MotionEvents,例如ACTION_DOWNACTION_UP

于 2010-09-23T12:37:21.883 回答
1

从 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>

关于动画比例还有两个类似的问题:

于 2020-11-19T11:55:14.850 回答
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;
    }
});
于 2016-05-19T10:48:01.017 回答
-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);
        }
      });
    }
于 2010-09-23T15:59:11.100 回答
-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);
        }
      });
    }
于 2020-08-31T02:54:36.430 回答