События прослушивателя касания

Я хочу, чтобы моя анимация начиналась при касании и останавливалась при отпускании пальца. Если анимация завершает свой цикл, представление становится непобедимым.

С моим текущим кодом, как только вы нажмете кнопку, анимация продолжится, даже если вы отпустите палец. Анимация завершила свой цикл без пальца, и вид стал моим непобедимым.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        init_views();
        init_fonts();
        text.setText("Hello");
        init_clicks();
    }
    private void init_views(){
        round = findViewById(R.id.round);
        text = findViewById(R.id.text);
    }

    private void init_fonts() {
        //Init font
        Raleway_Regular = Typeface.createFromAsset(getAssets(), "font/raleway_regular.ttf");
        Raleway_bold = Typeface.createFromAsset(getAssets(), "font/raleway_bold.ttf");
        Roboto_Regular = Typeface.createFromAsset(getAssets(), "font/roboto_regular.ttf");
        Montserrat_Regular = Typeface.createFromAsset(getAssets(), "font/montserrat_regular.ttf");
        PlayFairDisplay_Regular = Typeface.createFromAsset(getAssets(), "font/playfairdisplay_regular.ttf");
        PlayFairDisplay_Italic = Typeface.createFromAsset(getAssets(), "font/playfair_italic.ttf");
        QuickSand_light = Typeface.createFromAsset(getAssets(), "font/quicksand_light.ttf");
        //txts -> fonts
        text.setTypeface(QuickSand_light);
    }

    private void init_clicks(){
        round.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    // When the user clicks the Button
                    case MotionEvent.ACTION_DOWN:
                        anim();
                        break;

                    // When the user releases the Button
                    case MotionEvent.ACTION_UP:
                        round.clearAnimation();
                        break;
                }
                return false;
            }
        });
    }

    private void anim() {

        anim = AnimationUtils.loadAnimation(Main2Activity.this, R.anim.anim_btn);


        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
round.setVisibilty(View.INVICBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

        });
        round.startAnimation(anim);
    }

}
0
0
32
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Для этого добавьте двойной setOnTouchListener :

round.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                lastDown = System.currentTimeMillis();
                anim();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                lastDuration = System.currentTimeMillis() - lastDown;
                round.clearAnimation();
            }
            return true;
        }
    });
    round.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                lastDown = System.currentTimeMillis();
                anim();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                lastDuration = System.currentTimeMillis() - lastDown;
                round.clearAnimation();
            }
            return true;
        }
    });

}

Другие вопросы по теме