在触摸侦听器事件上



我希望我的动画从触摸开始,在手指释放时停止。如果动画完成其循环,则视图将变得不可战胜。

使用我当前的代码,一旦您点击按钮,即使松开手指,动画也会继续。动画在没有手指的情况下完成了它的循环,景色让我无敌。

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

为此添加 double 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;
        }
    });
}

相关内容

  • 没有找到相关文章

最新更新