Android 一鍵清理動畫

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   os   io   

版本:1.0 日期:2014.7.29 2014.7.30著作權:© 2014 kince 轉載註明出處
  一鍵清理是很多Launcher都會帶有的功能,其效果也比較美觀。實現方式也許有很多中,其中常見的是使用圖片drawable來完成的,具體可以參考這篇文章:模仿實現360案頭水晶球式的一鍵清理特效。本文另闢蹊徑,使用自訂View來完成同樣的效果,效能、效率更高。  ProgressWheel相信很多人並不陌生,我參考了其中一些代碼。有意思的是,看完它的代碼,發現其中隱藏了沒有使用的矩形進度條,因為項目名字的原因我估計也永遠不會出現了吧。所以就在其基礎之上增增改改,形成了ProgressRectangle。為了節省時間,第一版本並沒有使用自訂的屬性,這個以後再添加吧,畢竟有些雞肋。代碼如下:  
/*** */package com.kince.progressrectangle;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.graphics.Paint.Style;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.util.Log;import android.view.View;/*** @author kince* @category 仿solo案頭記憶體清理效果* @since 2014.7.30* @version 1.0.0* {@link }* */public class ProgressRectangle extends View {     // Sizes (with defaults)     private int layout_height = 0;     private int layout_width = 0;     // Colors (with defaults)     private int bgColor = Color.TRANSPARENT;     private int progressColor = 0xFF339933;     // Paints     private Paint progressPaint = new Paint();     private Paint bgPaint = new Paint();     private Paint titlePaint = new Paint();     private Paint usePaint = new Paint();     // Rectangles     private RectF rectBgBounds = new RectF();     private RectF rectProgressBounds = new RectF();     int progress = 100;     boolean isProgress;     private Handler spinHandler = new Handler() {          /**          * This is the code that will increment the progress variable and so          * spin the wheel          */          @Override          public void handleMessage(Message msg) {               invalidate();               // super.handleMessage(msg);          }     };     /**     * @param context     */     public ProgressRectangle(Context context) {          super(context);          // TODO Auto-generated constructor stub     }     /**     * @param context     * @param attrs     */     public ProgressRectangle(Context context, AttributeSet attrs) {          super(context, attrs);          // TODO Auto-generated constructor stub     }     /**     * @param context     * @param attrs     * @param defStyleAttr     */     public ProgressRectangle(Context context, AttributeSet attrs,               int defStyleAttr) {          super(context, attrs, defStyleAttr);          // TODO Auto-generated constructor stub     }     @Override     protected void onSizeChanged(int w, int h, int oldw, int oldh) {          // TODO Auto-generated method stub          super.onSizeChanged(w, h, oldw, oldh);          // Share the dimensions          layout_width = w;          Log.i("layout_width", layout_width + "");          layout_height = h;          Log.i("layout_height", layout_height + "");          setupBounds();          setupPaints();          invalidate();     }     private void setupPaints() {          // TODO Auto-generated method stub          bgPaint.setColor(bgColor);          bgPaint.setAntiAlias(true);          bgPaint.setStyle(Style.FILL);          progressPaint.setColor(progressColor);          progressPaint.setAntiAlias(true);          progressPaint.setStyle(Style.FILL);          titlePaint.setColor(Color.WHITE);          titlePaint.setTextSize(20);          titlePaint.setAntiAlias(true);          titlePaint.setStyle(Style.FILL);          usePaint.setColor(Color.WHITE);          usePaint.setAntiAlias(true);          usePaint.setTextSize(30);          usePaint.setStyle(Style.FILL);     }     private void setupBounds() {          // TODO Auto-generated method stub          int width = getWidth(); // this.getLayoutParams().width;          Log.i("width", width + "");          int height = getHeight(); // this.getLayoutParams().height;          Log.i("height", height + "");          rectBgBounds = new RectF(0, 0, width, height);     }     @Override     protected void onDraw(Canvas canvas) {          // TODO Auto-generated method stub          super.onDraw(canvas);          canvas.drawRect(rectBgBounds, bgPaint);          Log.i("progress", progress + "");          rectProgressBounds = new RectF(0, 0, progress, layout_height);          canvas.drawRect(rectProgressBounds, progressPaint);          canvas.drawText("使用記憶體", 25, 25, titlePaint);          canvas.drawText(progress + "M" + "/1024M", 25, 60, usePaint);     }     /**     * Increment the progress by 1 (of 100)     */     public void incrementProgress() {          isProgress = true;          progress++;          if (progress > 200)               progress = 100;          // setText(Math.round(((float) progress / 360) * 100) + "%");          spinHandler.sendEmptyMessage(0);     }     /**     * Increment the progress by 1 (of 100)     */     public void unIncrementProgress() {          isProgress = true;          progress--;          if (progress < 1)               progress = 100;          // setText(Math.round(((float) progress / 360) * 100) + "%");          spinHandler.sendEmptyMessage(0);     }     /**     * Set the progress to a specific value     */     public void setProgress(int i) {          progress = i;          spinHandler.sendEmptyMessage(0);     }}
  實現思路也是很簡單的,就是在onDraw()方法裡面繪製進度條的背景以及進度,進度的參數是傳遞進來的數值。Activity的代碼如下:
package com.kince.progressrectangle;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class RecActivity extends Activity {     boolean running;     int progress = 0;     ProgressRectangle progressRectangle;          @Override     protected void onCreate(Bundle savedInstanceState) {          // TODO Auto-generated method stub          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_rec);                    progressRectangle=(ProgressRectangle) findViewById(R.id.progressBar);          final Runnable r = new Runnable() {                    public void run() {                         running = true;                         while(progress<100) {                              progressRectangle.incrementProgress();                              progress++;                              try {                                   Thread.sleep(15);                              } catch (InterruptedException e) {                                   // TODO Auto-generated catch block                                   e.printStackTrace();                              }                         }                         while(progress>0) {                              progressRectangle.unIncrementProgress();                              progress--;                              try {                                   Thread.sleep(15);                              } catch (InterruptedException e) {                                   // TODO Auto-generated catch block                                   e.printStackTrace();                              }                         }                                   running = false;                    }             };                       Button increment = (Button) findViewById(R.id.btn_increment);        increment.setOnClickListener(new OnClickListener() {               public void onClick(View v) {                    if(!running) {                         progress = 0;                         Thread s = new Thread(r);                         s.start();                    }               }        });     }}
  效果如下:


  總體來說,就是通過繪製矩形來達到目的。當然,在實際使用中的效果還是有所差異的,歡迎大家反饋、交流。  <--  csdn下載:http://download.csdn.net/detail/wangjinyu501/7694607  gitub地址:https://github.com/wangjinyu501/ProgressRectangle  -->

聯繫我們

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