您当前的位置: 首页 >  ar

xiangzhihong8

暂无认证

  • 3浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

android自定义view实现progressbar的效果

xiangzhihong8 发布时间:2014-08-10 22:53:38 ,浏览量:3

一键清理是很多Launcher都会带有的功能,其效果也比较美观。实现方式也许有很多中,其中常见的是使用图片drawable来完成的,具体可以参考这篇文章: 模仿实现360桌面水晶球式的一键清理特效。本文另辟蹊径,使用自定义View来完成同样的效果,性能、效率更高。
   ProgressWheel相信很多人并不陌生,我参考了其中一些代码。有意思的是,看完它的代码,发现其中隐藏了没有使用的矩形进度条,因为项目名字的原因我估计也永远不会出现了吧。所以就在其基础之上增增改改,形成了ProgressRectangle。为了节省时间,第一版本并没有使用自定义的属性,这个以后再添加吧,毕竟有些鸡肋。代码如下:
  
[html] view plain copy
  1. /**  
  2. *   
  3. */  
  4. package com.kince.progressrectangle;  
  5.   
  6. import android.content.Context;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.Color;  
  9. import android.graphics.Paint;  
  10. import android.graphics.RectF;  
  11. import android.graphics.Paint.Style;  
  12. import android.os.Handler;  
  13. import android.os.Message;  
  14. import android.util.AttributeSet;  
  15. import android.util.Log;  
  16. import android.view.View;  
  17.   
  18. /**  
  19. * @author kince  
  20. * @category 仿solo桌面内存清理效果  
  21. * @since 2014.7.30  
  22. * @version 1.0.0  
  23. * {@link }  
  24. *   
  25. */  
  26. public class ProgressRectangle extends View {  
  27.   
  28.      // Sizes (with defaults)  
  29.      private int layout_height = 0;  
  30.      private int layout_width = 0;  
  31.      // Colors (with defaults)  
  32.      private int bgColor = Color.TRANSPARENT;  
  33.      private int progressColor = 0xFF339933;  
  34.      // Paints  
  35.      private Paint progressPaint = new Paint();  
  36.      private Paint bgPaint = new Paint();  
  37.      private Paint titlePaint = new Paint();  
  38.      private Paint usePaint = new Paint();  
  39.      // Rectangles  
  40.      private RectF rectBgBounds = new RectF();  
  41.      private RectF rectProgressBounds = new RectF();  
  42.   
  43.      int progress = 100;  
  44.      boolean isProgress;  
  45.   
  46.      private Handler spinHandler = new Handler() {  
  47.           /**  
  48.           * This is the code that will increment the progress variable and so  
  49.           * spin the wheel  
  50.           */  
  51.           @Override  
  52.           public void handleMessage(Message msg) {  
  53.                invalidate();  
  54.   
  55.                // super.handleMessage(msg);  
  56.           }  
  57.      };  
  58.   
  59.      /**  
  60.      * @param context  
  61.      */  
  62.      public ProgressRectangle(Context context) {  
  63.           super(context);  
  64.           // TODO Auto-generated constructor stub  
  65.      }  
  66.   
  67.      /**  
  68.      * @param context  
  69.      * @param attrs  
  70.      */  
  71.      public ProgressRectangle(Context context, AttributeSet attrs) {  
  72.           super(context, attrs);  
  73.           // TODO Auto-generated constructor stub  
  74.      }  
  75.   
  76.      /**  
  77.      * @param context  
  78.      * @param attrs  
  79.      * @param defStyleAttr  
  80.      */  
  81.      public ProgressRectangle(Context context, AttributeSet attrs,  
  82.                int defStyleAttr) {  
  83.           super(context, attrs, defStyleAttr);  
  84.           // TODO Auto-generated constructor stub  
  85.      }  
  86.   
  87.      @Override  
  88.      protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  89.           // TODO Auto-generated method stub  
  90.           super.onSizeChanged(w, h, oldw, oldh);  
  91.           // Share the dimensions  
  92.           layout_width = w;  
  93.           Log.i("layout_width", layout_width + "");  
  94.   
  95.           layout_height = h;  
  96.           Log.i("layout_height", layout_height + "");  
  97.           setupBounds();  
  98.           setupPaints();  
  99.           invalidate();  
  100.   
  101.      }  
  102.   
  103.      private void setupPaints() {  
  104.           // TODO Auto-generated method stub  
  105.           bgPaint.setColor(bgColor);  
  106.           bgPaint.setAntiAlias(true);  
  107.           bgPaint.setStyle(Style.FILL);  
  108.   
  109.           progressPaint.setColor(progressColor);  
  110.           progressPaint.setAntiAlias(true);  
  111.           progressPaint.setStyle(Style.FILL);  
  112.   
  113.           titlePaint.setColor(Color.WHITE);  
  114.           titlePaint.setTextSize(20);  
  115.           titlePaint.setAntiAlias(true);  
  116.           titlePaint.setStyle(Style.FILL);  
  117.   
  118.           usePaint.setColor(Color.WHITE);  
  119.           usePaint.setAntiAlias(true);  
  120.           usePaint.setTextSize(30);  
  121.           usePaint.setStyle(Style.FILL);  
  122.   
  123.      }  
  124.   
  125.      private void setupBounds() {  
  126.           // TODO Auto-generated method stub  
  127.           int width = getWidth(); // this.getLayoutParams().width;  
  128.           Log.i("width", width + "");  
  129.           int height = getHeight(); // this.getLayoutParams().height;  
  130.           Log.i("height", height + "");  
  131.           rectBgBounds = new RectF(0, 0, width, height);  
  132.      }  
  133.   
  134.      @Override  
  135.      protected void onDraw(Canvas canvas) {  
  136.           // TODO Auto-generated method stub  
  137.           super.onDraw(canvas);  
  138.   
  139.           canvas.drawRect(rectBgBounds, bgPaint);  
  140.   
  141.           Log.i("progress", progress + "");  
  142.           rectProgressBounds = new RectF(0, 0, progress, layout_height);  
  143.           canvas.drawRect(rectProgressBounds, progressPaint);  
  144.           canvas.drawText("使用内存", 25, 25, titlePaint);  
  145.           canvas.drawText(progress + "M" + "/1024M", 25, 60, usePaint);  
  146.   
  147.      }  
  148.   
  149.      /**  
  150.      * Increment the progress by 1 (of 100)  
  151.      */  
  152.      public void incrementProgress() {  
  153.           isProgress = true;  
  154.           progress++;  
  155.           if (progress > 200)  
  156.                progress = 100;  
  157.           // setText(Math.round(((float) progress / 360) * 100) + "%");  
  158.           spinHandler.sendEmptyMessage(0);  
  159.      }  
  160.   
  161.      /**  
  162.      * Increment the progress by 1 (of 100)  
  163.      */  
  164.      public void unIncrementProgress() {  
  165.           isProgress = true;  
  166.           progress--;  
  167.           if (progress 
关注
打赏
1482932726
查看更多评论
立即登录/注册

微信扫码登录

0.0496s