Android簡單的廣告控制項View

來源:互聯網
上載者:User

在布局檔案中引用此View控制項即可。

  1. public class GGView extends View {  
  2.     int COMPONENT_WIDTH; // 該控制項寬度   
  3.     int COMPONENT_HEIGHT; // 該控制項高度   
  4.     boolean initflag = false; // 是否要擷取控制項的高度和寬度標誌   
  5.     static Bitmap[] bma; // 需要播放的圖片的數組   
  6.     Paint paint; // 畫筆   
  7.     int[] drawablesId; // 圖片ID數組   
  8.     int currIndex = 0; // 圖片ID數組下標,根據此變數畫圖片   
  9.     boolean workFlag = true; // 播放圖片線程標誌位   
  10.   
  11.     public GGView(Context father, AttributeSet as) { // 構造器   
  12.         super(father, as);  
  13.         drawablesId = new int[] { // 初始化圖片ID數組   
  14.         R.drawable.adv1, // 將需要播放的圖片ID放於此處即可   
  15.                 R.drawable.adv2, R.drawable.adv3, };  
  16.         bma = new Bitmap[drawablesId.length]; // 建立存放圖片的數組   
  17.         initBitmaps(); // 調用初始化圖片函數,初始化圖片數組   
  18.         paint = new Paint(); // 建立畫筆   
  19.         paint.setFlags(Paint.ANTI_ALIAS_FLAG); // 消除鋸齒   
  20.         new Thread() { // 建立播放圖片線程   
  21.             public void run() {  
  22.                 while (workFlag) {  
  23.                     currIndex = (currIndex + 1) % drawablesId.length;// 改變ID數組下標值   
  24.                     GGView.this.postInvalidate(); // 繪製   
  25.                     try {  
  26.                         Thread.sleep(3000); // 休息三秒   
  27.                     } catch (InterruptedException e) {  
  28.                         e.printStackTrace();  
  29.                     }  
  30.                 }  
  31.             }  
  32.         }.start(); // 啟動線程   
  33.     }  
  34.   
  35.     public void initBitmaps() { // 初始化圖片函數   
  36.         Resources res = this.getResources(); // 擷取Resources對象   
  37.         for (int i = 0; i < drawablesId.length; i++) {  
  38.             bma[i] = BitmapFactory.decodeResource(res, drawablesId[i]);  
  39.         }  
  40.     }  
  41.   
  42.     public void onDraw(Canvas canvas) { // 繪製函數   
  43.         if (!initflag) { // 第一次繪製時需要擷取寬度和高度   
  44.             COMPONENT_WIDTH = this.getWidth(); // 擷取view的寬度   
  45.             COMPONENT_HEIGHT = this.getHeight(); // 擷取view的高度   
  46.             initflag = true;  
  47.         }  
  48.         int picWidth = bma[currIndex].getWidth(); // 擷取當前繪製圖片的寬度   
  49.         int picHeight = bma[currIndex].getHeight(); // 擷取當前繪製圖片的高度   
  50.         int startX = (COMPONENT_WIDTH - picWidth) / 2; // 得到繪製圖片的左上方X座標   
  51.         int startY = (COMPONENT_HEIGHT - picHeight) / 2; // 得到繪製圖片的左上方Y座標   
  52.         canvas.drawARGB(255, 200, 128, 128); // 設定背景色   
  53.         canvas.drawBitmap(bma[currIndex], startX, startY, paint); // 繪製圖片   
  54.     }  
  55. }  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.