Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用自定義 View 實現 Android 一鍵清理動畫

編輯:關於 Android 程式設計

版本:1.0 日期:2014.7.29 – 2014.7.30 版權:© 2014 kince(轉載請註明出處)

一鍵清理是許多 Launcher 都會具備的功能,效果也十分炫酷。常見實現是使用圖片 drawable 逐幀切換,但本文另闢蹊徑,改用自定義 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;

/**
 * 仿 solo 桌面內存清理效果
 * @author kince
 * @version 1.0.0
 */
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() {
        @Override
        public void handleMessage(Message msg) {
            invalidate();
        }
    };

    public ProgressRectangle(Context context) {
        super(context);
    }

    public ProgressRectangle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressRectangle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        layout_width = w;
        layout_height = h;
        setupBounds();
        setupPaints();
        invalidate();
    }

    private void setupPaints() {
        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() {
        int width = getWidth();
        int height = getHeight();
        rectBgBounds = new RectF(0, 0, width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(rectBgBounds, bgPaint);
        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);
    }

    /** 進度加 1 */
    public void incrementProgress() {
        isProgress = true;
        progress++;
        if (progress > 200) progress = 100;
        spinHandler.sendEmptyMessage(0);
    }

    /** 進度減 1 */
    public void unIncrementProgress() {
        isProgress = true;
        progress--;
        if (progress < 1) progress = 100;
        spinHandler.sendEmptyMessage(0);
    }

    /** 設置指定進度 */
    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.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) {
        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) {
                        e.printStackTrace();
                    }
                }
                while (progress > 0) {
                    progressRectangle.unIncrementProgress();
                    progress--;
                    try {
                        Thread.sleep(15);
                    } catch (InterruptedException e) {
                        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();
                }
            }
        });
    }
}

效果如下:

相比逐幀切換圖片,自定義 View 直接繪製的方式在流暢度與內存佔用上更具優勢,非常適合用於此類需要動態變化的清理動畫場景。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved