Android realizes the effect of holding down to zoom out and release to zoom in

  • In the development process, we sometimes use an effect. For example, when the button is pressed, it will shrink, and when it is released, it will return to the original size. Let's try to achieve this effect.

    The main idea is to use the two methods of setScaleX and setScaleY of view to process in the DOWN event and UP event of onTouch,

    Here is the code:

    XML:

    <Button
    android:id="@+id/btn_test"
    android:layout_width="300dp"
    android:layout_height="60dp"
    android:text="button"/>



    Activity:

    public class MyActivity extends Activity implements View.OnClickListener , View.OnTouchListener {


        private Button mTestBtn;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
            mTestBtn = (Button)findViewById(R.id.btn_test);
            mTestBtn.setOnClickListener(this);
            mTestBtn.setOnTouchListener(this);
        }


        @Override
        public void onClick(View view) {


        }


        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {


            switch (motionEvent.getAction()){
                case MotionEvent.ACTION_DOWN:
                    if(view.getId() == R.id.btn_test){
                        mTestBtn.setScaleX((float)0.95);
                        mTestBtn.setScaleY((float)0.95);
                    }
                    break;


                case MotionEvent.ACTION_UP:
                    if(view.getId() == R.id.btn_test){
                        mTestBtn.setScaleX(1);
                        mTestBtn.setScaleY(1);
                    }
                    break;
            }


            return false;
        }
    }


    Note that the return value of the onTouch event should be set to false, otherwise the event will be consumed by onTouch, and onClick will not respond.

The above is the content of Android to achieve the effect of pressing and shrinking, releasing and zooming in . For more  Android  content, please use the search function on the upper right to obtain relevant information. Reprint address: https://www.aliyun.com/jiaocheng/92113.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324893192&siteId=291194637