跑马灯、字体闪烁、跳动和抖动动画

                                    跑马灯,字体跳动和抖动动画

一.主要是记录我查找资料和研究的字体跳动和抖动动画,废话少说,直接干;

1.主界面代码

public class MainActivity extends AppCompatActivity {
    private JumpingBeans jumpingBeans1, jumpingBeans2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text_aaa = (TextView) findViewById(R.id.text_aaa);
        jumpingBeans1 = JumpingBeans.with(text_aaa)//把这个TextView绑定到JumpingBeans 中
                .appendJumpingDots()//只有這個文字后面的"..."跳动,没有"..."默认加上
                .build();//判断一系列参数,构建效果并执行(重新对TextView的字符串内容进行拼接)

        final TextView textView1 = (TextView) findViewById(R.id.jumping_text_1);
        jumpingBeans1 = JumpingBeans.with(textView1)
                .makeTextJump(0, textView1.getText().toString().length())////具体动作的哪几个字如传入(0,3)就是第1-第4个字有动画效果
                .setIsWave(true)//是否允许有动画
                .setLoopDuration(1000)//效果持续的时间,毫秒为单位
                .build();//判断一系列参数,构建效果并执行(重新对TextView的字符串内容进行拼接)


        //上下跳動
        // Note that, even though we access textView2's text when starting to work on
        // the animation builder, we don't alter it in any way, so we're ok
        final TextView textView2 = (TextView) findViewById(R.id.jumping_text_2);
        jumpingBeans2 = JumpingBeans.with(textView2)
                .makeTextJump(0, textView2.getText().toString().indexOf(' '))
                .setIsWave(false)
                .setLoopDuration(1000)
                .build();

        final TextView textView3 = (TextView) findViewById(R.id.aaa);
        SpannableStringBuilder sbb = new SpannableStringBuilder(textView3.getText());
        //buildSingleSpan(sbb);
        buildWavingSpans(sbb, textView3);
        textView3.setText(sbb);

        final TextView bbb = (TextView) findViewById(R.id.bbb);
        startFlick(bbb);//文字闪烁

        //动画左右抖动
        Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake_x);//加载动画资源文件
//        shake.setRepeatCount(Animation.INFINITE);//表示重复多次;可以再shake_x设置
        findViewById(R.id.ccc).startAnimation(shake); //给组件播放动画效果

        //动画上下抖动
        Animation shake1 = AnimationUtils.loadAnimation(this, R.anim.shake_y);//加载动画资源文件
        shake1.setRepeatCount(Animation.INFINITE);//表示重复多次
        findViewById(R.id.ddd).startAnimation(shake1); //给组件播放动画效果

        //文字抬头抖动
        Animation shake2 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim);
        shake.reset();
        shake.setFillAfter(true);
        findViewById(R.id.eee).startAnimation(shake2);
    }


    private JumpingSpan[] buildWavingSpans(SpannableStringBuilder sbb, TextView tv) {
        JumpingSpan[] spans;
        int loopDuration = 1300;
        int startPos = 0;//textview字体的开始位置
        int endPos = tv.getText().length();//结束位置
        int waveCharDelay = loopDuration / (3 * (endPos - startPos));//每个字体延迟的时间


        spans = new JumpingSpan[endPos - startPos];
        for (int pos = startPos; pos < endPos; pos++) {//设置每个字体的jumpingspan
            JumpingSpan jumpingBean = new JumpingSpan(tv, loopDuration, pos - startPos, waveCharDelay, 0.65f);
            sbb.setSpan(jumpingBean, pos, pos + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spans[pos - startPos] = jumpingBean;
        }
        return spans;
    }


    //start*******************************文字闪烁************************************

    /**
     * 开启View闪烁效果
     * 控件闪烁,其实就是控制控件的透明度,从可见到逐渐不可见,再逐渐到可见,一直反复。因此,要想实现控件闪烁,只需要使用android中的alpha动画即可
     */
    private void startFlick(View view) {
        if (null == view) {
            return;
        }
        Animation alphaAnimation = new AlphaAnimation(1, 0.4f);
        alphaAnimation.setDuration(300);
        alphaAnimation.setInterpolator(new LinearInterpolator());
        alphaAnimation.setRepeatCount(Animation.INFINITE);//表示重复多次
        alphaAnimation.setRepeatMode(Animation.REVERSE);//表示动画结束后,反过来再执行;RESTART表示从头开始,REVERSE表示从末尾倒播
        view.startAnimation(alphaAnimation);
    }


    /**
     * 取消View闪烁效果
     */
    private void stopFlick(View view) {
        if (null == view) {
            return;
        }
        view.clearAnimation();
    }
    //end*******************************************************************
}

2.布局




    

    

    

    
    

    

    

    

    


    



3.shake_x.xml




4.cycle_7.xml


    

5.效果

你可能感兴趣的:(功能类)