На ощупь события слушателя - PullRequest
0 голосов
/ 21 июня 2019

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

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

 @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);
    }

}

1 Ответ

0 голосов
/ 21 июня 2019

Для этого добавьте двойной 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;
        }
    });

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...