Android 自定义View跑马灯效果(一)

            今天通过书籍重新复习了一遍自定义VIew,为了加强自己的学习,我把它写在博客里面,有兴趣的可以看一下,相互学习共同进步:

            通过自定义一个跑马灯效果,来诠释一下简单的效果:

            一、创建一个类继承View,重写onDraw方法,进行绘制文字:

           (1)  定义全局的变量:

             private float textX = 0;

       private Paint paint = new Paint();
       private MyThead thead = null;
       初始化字体的位置,在onDraw方法中开启线程:
1         paint.setTextSize(40);//初始化文字大小
2         canvas.drawText("我是文字", textX, 500, paint);画出文字的开始位置;
3         //圆形进度调效果 起始角度,和区间角度
4         canvas.drawArc(rectF, 0, acrX, true, paint);
5         if (thead == null) {
6             thead = new MyThead();
7             thead.start();
8         }
 

           (2)开启线程进行文字字体的移动:

            

 private boolean running = true;

    private class MyThead extends Thread {
        private Random random = new Random();

        @Override
        public void run() {
            super.run();

            while (running) {

                //文字跑马灯效果啊
                textX = textX + 3;
                if (textX > getWidth()) {
                    textX = 0 - paint.measureText("我是文字");//截取文字的长度
                }
                paint.setARGB(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));// 设置颜色 第一个参数:透明度
                postInvalidate();//重新进行绘制
               
                try {
                    sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }


        }
    }

       注:离开屏幕时调用的方法:

       

 @Override
    protected void onDetachedFromWindow() {
        //离开屏幕的操作
        running = false;
        super.onDetachedFromWindow();

    }

       (3)、在布局或者代码中引用:

             直接包名引用:

 <com.example.zhangyanan.myview.view.DrawView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

        

    二、在Xml中自定义属性:

         列举跑马灯效果多行显示:设置全局变量行数,在xml中可以进行设置行数:

       (1)初始化行数:

              逻辑如下:

               private int lineNum = 0;

 1    for (int i = 0; i < lineNum; i++) {
 2             //文字跑马灯效果
 3             paint.setTextSize(40);
 4             canvas.drawText("我是文字", textX, 200 + i * 50, paint);
 5 
 6             if (thead == null) {
 7                 thead = new MyThead();
 8                 thead.start();
 9             }
10         }

            

       (2)在values中创建attrs.xml文件定义样式属性:

1 <resources>
2     <declare-styleable name="DrawaViewStyle">
3         <attr name="lineNum" format="integer"></attr>
4     </declare-styleable>
5 </resources>

      

         (3)在代码中解析lineNum属性:      

1     public DrawViewAttrs(Context context, @Nullable AttributeSet attrs) {
2         super(context, attrs);
3         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DrawaViewStyle);
4         lineNum=  ta.getInteger(R.styleable.DrawaViewStyle_lineNum, 1);
5     }

         

         (4)在布局中应用:

                 引入命名空间:

xmlns:nt="http://schemas.android.com/apk/res/com.example.zhangyanan.myview"

                 添加自定义属性:

<com.example.zhangyanan.myview.view.DrawViewAttrs
        android:layout_width="match_parent"
        nt:lineNum="3"
        android:layout_height="match_parent" />

      源码地址:链接:http://pan.baidu.com/s/1clQwkI  密码:6unf

       以上就是自定义View的简单应用,不足之处请多指教。联系方式qq:1154749219  

原文地址:https://www.cnblogs.com/huihuizhang/p/7623111.html