触摸监听器事件

时间:2019-06-21 19:32:23

标签: android listener

我希望动画从触摸开始并在手指松开时停止。如果动画结束循环播放,视图将变得无敌。

使用我当前的代码,一旦按下按钮,动画就可以继续进行,即使松开手指也是如此。动画没有手指就结束了循环,而视野无敌。

 @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 :(得分:0)

为此添加了两个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;
        }
    });

}