How to animate the textview (very very long text )scroll automatically horizontally

前端 未结 11 929
长发绾君心
长发绾君心 2020-12-16 06:30

I am not interested in Marquee because, in Marquee you can not control the speed of marquee. I have tried to animate the textview but Parent view clips the text at the end e

11条回答
  •  不思量自难忘°
    2020-12-16 06:51

    Here was my SOLUTION

    To make the long text inside textview not be cut by parent view or by screen, I have done two things.

    First, let textview inside a scroolview like below code

    
        
    
    

    Then, I measure my text size then refine the textview param by doing this.

        marqueeText.setText("my long text");
        Paint textPaint = marqueeText.getPaint();
        String text = marqueeText.getText().toString();//get text
        int width = Math.round(textPaint.measureText(text));//measure the text size
        ViewGroup.LayoutParams params =  marqueeText.getLayoutParams();
        params.width = width;
        marqueeText.setLayoutParams(params); //refine
    
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
        int screenWidth = displaymetrics.widthPixels;
    
        //this is optional. do not scroll if text is shorter than screen width
        //remove this won't effect the scroll
        if (width <= screenWidth) {
            //All text can fit in screen.
            return;
        }
        //set the animation
        TranslateAnimation slide = new TranslateAnimation(0, -width, 0, 0);
        slide.setDuration(20000);
        slide.setRepeatCount(Animation.INFINITE);
        slide.setRepeatMode(Animation.RESTART);
        slide.setInterpolator(new LinearInterpolator());
        marqueeText.startAnimation(slide);
    

    I hope this solution which took me half a day to research can help others who might meet the same problem like me.

提交回复
热议问题