android編程中的瑣碎知識點匯總(3)

來源:互聯網
上載者:User

1.圖片載入方法,方便使用者載入圖片

Java代碼  
  1. /*** 
  2.  * 載入本地圖片 
  3.  *  
  4.  * @param context 
  5.  *            :主運行函數執行個體 
  6.  * @param bitAdress 
  7.  *            :圖片地址,一般指向R下的drawable目錄 
  8.  * @return 
  9.  */  
  10. public final Bitmap CreatImage(Context context, int bitAdress) {  
  11.     Bitmap bitmaptemp = null;  
  12.     bitmaptemp = BitmapFactory.decodeResource(context.getResources(),  
  13.             bitAdress);  
  14.     return bitmaptemp;  
  15. }  

 2.圖片平均分割方法,將大圖平均分割為N行N列,方便使用者使用

Java代碼  
  1. /*** 
  2.  * 圖片分割 
  3.  *  
  4.  * @param g 
  5.  *            :畫布 
  6.  * @param paint 
  7.  *            :畫筆 
  8.  * @param imgBit 
  9.  *            :圖片 
  10.  * @param x 
  11.  *            :X軸起點座標 
  12.  * @param y 
  13.  *            :Y軸起點座標 
  14.  * @param w 
  15.  *            :單一圖片的寬度 
  16.  * @param h 
  17.  *            :單一圖片的高度 
  18.  * @param line 
  19.  *            :第幾列 
  20.  * @param row 
  21.  *            :第幾行 
  22.  */  
  23. public final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, int x,  
  24.         int y, int w, int h, int line, int row) {  
  25.     g.clipRect(x, y, x + w, h + y);  
  26.     g.drawBitmap(imgBit, x - line * w, y - row * h, paint);  
  27.     g.restore();  
  28. }  

 3.圖片縮放,對當前圖片進行縮放處理

Java代碼  
  1. /*** 
  2.  * 圖片的縮放方法 
  3.  *  
  4.  * @param bgimage 
  5.  *            :源圖片資源 
  6.  * @param newWidth 
  7.  *            :縮放後寬度 
  8.  * @param newHeight 
  9.  *            :縮放後高度 
  10.  * @return 
  11.  */  
  12. public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {  
  13.     // 擷取這個圖片的寬和高  
  14.     int width = bgimage.getWidth();  
  15.     int height = bgimage.getHeight();  
  16.     // 建立操作圖片用的matrix對象  
  17.     Matrix matrix = new Matrix();  
  18.     // 計算縮放率,新尺寸除原始大小  
  19.     float scaleWidth = ((float) newWidth) / width;  
  20.     float scaleHeight = ((float) newHeight) / height;  
  21.     // 縮放圖片動作  
  22.     matrix.postScale(scaleWidth, scaleHeight);  
  23.     Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,  
  24.             matrix, true);  
  25.     return bitmap;  
  26. }  

 4.繪製帶有邊框的文字,一般在遊戲中起文字的美化作用

Java代碼  
  1. /*** 
  2.  * 繪製帶有邊框的文字 
  3.  *  
  4.  * @param strMsg 
  5.  *            :繪製內容 
  6.  * @param g 
  7.  *            :畫布 
  8.  * @param paint 
  9.  *            :畫筆 
  10.  * @param setx 
  11.  *            ::X軸起始座標 
  12.  * @param sety 
  13.  *            :Y軸的起始座標 
  14.  * @param fg 
  15.  *            :前景色彩 
  16.  * @param bg 
  17.  *            :背景色 
  18.  */  
  19. public void drawText(String strMsg, Canvas g, Paint paint, int setx,  
  20.         int sety, int fg, int bg) {  
  21.     paint.setColor(bg);  
  22.     g.drawText(strMsg, setx + 1, sety, paint);  
  23.     g.drawText(strMsg, setx, sety - 1, paint);  
  24.     g.drawText(strMsg, setx, sety + 1, paint);  
  25.     g.drawText(strMsg, setx - 1, sety, paint);  
  26.     paint.setColor(fg);  
  27.     g.drawText(strMsg, setx, sety, paint);  
  28.     g.restore();  
  29. }  

 5.圖片分割的最簡便方式

Java代碼  
  1. public final Bitmap cuteImage(Bitmap _imgBit, int _startX, int width,  
  2. int _startY, int height) {  
  3.     Bitmap tempMap = null;  
  4.     tempMap = Bitmap.createBitmap(_imgBit, _startX, _startY, width, height);  
  5.     return tempMap;  
  6. }  

 6.字串分行顯示

Java代碼  
  1. public String[] StringFormat(String text, int maxWidth, int fontSize) {  
  2.     String[] result = null;  
  3.     Vector<String> tempR = new Vector<String>();  
  4.     int lines = 0;  
  5.     int len = text.length();  
  6.     int index0 = 0;  
  7.     int index1 = 0;  
  8.     boolean wrap;  
  9.     while (true) {  
  10.         int widthes = 0;  
  11.         wrap = false;  
  12.         for (index0 = index1; index1 < len; index1++) {  
  13.             if (text.charAt(index1) == '\n') {  
  14.                 index1++;  
  15.                 wrap = true;  
  16.                 break;  
  17.             }  
  18.             widthes = fontSize + widthes;  
  19.             if (widthes > maxWidth) {  
  20.                 break;  
  21.             }  
  22.         }  
  23.         lines++;  
  24.         if (wrap) {  
  25.             tempR.addElement(text.substring(index0, index1 - 1));  
  26.         } else {  
  27.             tempR.addElement(text.substring(index0, index1));  
  28.         }  
  29.         if (index1 >= len) {  
  30.             break;  
  31.         }  
  32.     }  
  33.     result = new String[lines];  
  34.     tempR.copyInto(result);  
  35.     return result;  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.