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  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM