Пример #1
0
 /**
  * 开始的动画
  *
  * @param canvas
  */
 private void createStart(Canvas canvas, int type) {
   Paint paint;
   Paint bgpaint;
   String text;
   int img;
   int icon;
   switch (type) {
     case TYPE_END:
       paint = endPaint;
       bgpaint = endBgPaint;
       text = option.getBtnEndText();
       img = option.getBtnEndBgImage();
       icon = option.getBtnEndIcon();
       break;
     case TYPE_ERROR:
       paint = errorPaint;
       bgpaint = errorBgPaint;
       text = option.getBtnErrorText();
       img = option.getBtnErrorBgImage();
       icon = option.getBtnErrorIcon();
       break;
     case TYPE_START:
       paint = startPaint;
       bgpaint = startBgPaint;
       text = option.getBtnStartText();
       img = option.getBtnStartBgImage();
       icon = option.getBtnStartIcon();
       break;
     default:
       return;
   }
   if (index < (maxIndex / 2)) { // 从点变成圆
     // 画背景色
     canvas.drawCircle(width / 2, height / 2, radius, bgpaint);
     // 画背景图
     if (img > 0) {
       canvas.drawBitmap(
           createBitmap(
               BitmapFactory.decodeResource(getResources(), img), radius * 2, radius * 2, radius),
           width / 2 - radius,
           height / 2 - radius,
           null);
     }
     int r = 0;
     if (width > height) {
       r = (int) (height * 0.5);
     } else {
       r = (int) (width * 0.5);
     }
     radius = r / maxIndex * 2 * index;
   } else { // 从圆变成按钮
     float wr = 0;
     float hr = 0;
     if (width > height) {
       radius = (int) (height * 0.5);
     } else {
       radius = (int) (width * 0.5);
     }
     if (option.getBtnRadius() > radius) {
       radius = option.getBtnRadius();
     }
     wr = (width / 2 - radius) / maxIndex * 2 * (index - maxIndex / 2);
     hr = (height / 2 - radius) / maxIndex * 2 * (index - maxIndex / 2);
     if (wr > (width / 2 - radius)) {
       wr = (width / 2 - radius);
     }
     if (hr > (height / 2 - radius)) {
       hr = (height / 2 - radius);
     }
     // 画背景色
     RectF rect =
         new RectF(
             width / 2 - wr - radius,
             height / 2 - hr - radius,
             width / 2 + wr + radius,
             height / 2 + hr + radius);
     canvas.drawRoundRect(rect, radius, radius, bgpaint);
     // 画背景图
     if (img > 0) {
       canvas.drawBitmap(
           createBitmap(
               BitmapFactory.decodeResource(getResources(), img),
               (int) hr * 2 + radius * 2,
               (int) wr * 2 + radius * 2,
               radius),
           width / 2 - wr,
           height / 2 - hr,
           null);
     }
     // 绘制前景图片
     if (icon > 0) {
       int w = 0, h = 0;
       float l = 0;
       Bitmap bitmap = BitmapFactory.decodeResource(getResources(), icon);
       if (bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
         l = bitmap.getWidth() / bitmap.getHeight();
         switch (option.getBtnIconSizeType()) {
           case LProgressButtonOption.Builder.btnIconSizeType_AUTO:
             if (wr / hr > l) {
               h = (int) ((hr * 2 + radius * 2) * option.getBtnIconSize());
               w = (int) (h * l);
             } else {
               w = (int) ((wr * 2 + radius * 2) * option.getBtnIconSize());
               h = (int) (w / l);
             }
             break;
           case LProgressButtonOption.Builder.btnIconSizeType_HEIGHT:
             h = (int) ((hr * 2 + radius * 2) * option.getBtnIconSize());
             w = (int) (h * l);
             break;
           case LProgressButtonOption.Builder.btnIconSizeType_WIDTH:
             w = (int) ((wr * 2 + radius * 2) * option.getBtnIconSize());
             h = (int) (w / l);
             break;
         }
         canvas.drawBitmap(
             createBitmap(bitmap, h, w, option.getBtnIconRadius()),
             width / 2 - w / 2,
             height / 2 - h / 2,
             null);
       }
       bitmap.recycle();
       bitmap = null;
       System.gc();
     }
     // 画字
     if (text != null && text.length() > 0) {
       int textSize;
       if (wr > hr) {
         textSize = (int) ((hr * 2 + radius * 2) * 0.3);
         if (textSize * text.length() > (wr * 2 + radius * 2)) {
           textSize = (int) ((wr * 2 + radius * 2) / text.length() * 0.8);
         }
       } else {
         textSize = (int) ((wr * 2 + radius * 2) * 0.3);
         if (textSize * text.length() > (hr * 2 + radius * 2)) {
           textSize = (int) ((hr * 2 + radius * 2) / text.length() * 0.8);
         }
       }
       paint.setTextSize(textSize);
       FontMetrics fm = paint.getFontMetrics();
       float textY = -fm.descent + (fm.descent - fm.ascent) / 2;
       canvas.drawText(text, width / 2, height / 2 + textY, paint);
     }
   }
 }
Пример #2
0
 /**
  * 按钮的完成状态
  *
  * @param canvas
  * @param type
  */
 private void createBtn(Canvas canvas, int type) {
   Paint paint;
   Paint bgpaint;
   String text;
   int img;
   int icon;
   switch (type) {
     case TYPE_END:
       paint = endPaint;
       bgpaint = endBgPaint;
       text = option.getBtnEndText();
       img = option.getBtnEndBgImage();
       icon = option.getBtnEndIcon();
       break;
     case TYPE_ERROR:
       paint = errorPaint;
       bgpaint = errorBgPaint;
       text = option.getBtnErrorText();
       img = option.getBtnErrorBgImage();
       icon = option.getBtnErrorIcon();
       break;
     case TYPE_START:
       paint = startPaint;
       bgpaint = startBgPaint;
       text = option.getBtnStartText();
       img = option.getBtnStartBgImage();
       icon = option.getBtnStartIcon();
       break;
     default:
       return;
   }
   isEnd = false;
   this.type = oldType;
   switch (option.getBtnRadius()) {
     case LProgressButtonOption.Builder.btnRadius_HORIZONTAL:
       radius = height / 2;
       break;
     case LProgressButtonOption.Builder.btnRadius_SQUARE:
       radius = 0;
       break;
     case LProgressButtonOption.Builder.btnRadius_VERTICAL:
       radius = width / 2;
       break;
     default:
       if (option.getBtnRadius() > 0) {
         radius = option.getBtnRadius();
       } else {
         radius = 0;
       }
       break;
   }
   // 画背景色
   RectF rect = new RectF(0, 0, width, height);
   canvas.drawRoundRect(rect, radius, radius, bgpaint);
   // 画背景图
   if (img > 0) {
     canvas.drawBitmap(
         createBitmap(BitmapFactory.decodeResource(getResources(), img), height, width, radius),
         width,
         height,
         null);
   }
   // 绘制前景图片
   if (icon > 0) {
     int w = 0, h = 0;
     float l = 0;
     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), icon);
     if (bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
       l = bitmap.getWidth() / bitmap.getHeight();
       switch (option.getBtnIconSizeType()) {
         case LProgressButtonOption.Builder.btnIconSizeType_AUTO:
           if (width / height > l) {
             h = (int) (height * option.getBtnIconSize());
             w = (int) (h * l);
           } else {
             w = (int) (width * option.getBtnIconSize());
             h = (int) (w / l);
           }
           break;
         case LProgressButtonOption.Builder.btnIconSizeType_HEIGHT:
           h = (int) (height * option.getBtnIconSize());
           w = (int) (h * l);
           break;
         case LProgressButtonOption.Builder.btnIconSizeType_WIDTH:
           w = (int) (width * option.getBtnIconSize());
           h = (int) (w / l);
           break;
       }
       canvas.drawBitmap(
           createBitmap(bitmap, h, w, option.getBtnIconRadius()),
           width / 2 - w / 2,
           height / 2 - h / 2,
           null);
     }
     bitmap.recycle();
     bitmap = null;
     System.gc();
   }
   // 画字
   if (text != null && text.length() > 0) {
     int textSize;
     if (width > height) {
       textSize = (int) (height * 0.3);
       if (textSize * text.length() > width) {
         textSize = (int) (width / text.length() * 0.8);
       }
     } else {
       textSize = (int) (width * 0.3);
       if (textSize * text.length() > height) {
         textSize = (int) (height / text.length() * 0.8);
       }
     }
     startPaint.setTextSize(textSize);
     FontMetrics fm = startPaint.getFontMetrics();
     float textY = -fm.descent + (fm.descent - fm.ascent) / 2;
     canvas.drawText(text, width / 2, height / 2 + textY, paint);
   }
 }