android自定义view实现progressbar的效果
一键清理是很多Launcher都会带有的功能,其效果也比较美观。实现方式也许有很多中,其中常见的是使用图片drawable来完成的,具体可以参考这篇文章:模仿实现360桌面水晶球式的一键清理特效。本文另辟蹊径,使用自定义View来完成同样的效果,性能、效率更高。
- /**
- *
- */
- 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);
- }
- }
- 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();
- }
- }
- });
- }
- }
效果如下:
android自定义view实现progressbar的效果的更多相关文章
- Android自定义View之ProgressBar出场记
关于自定义View,我们前面已经有三篇文章在介绍了,如果筒子们还没阅读,建议先看一下,分别是android自定义View之钟表诞生记.android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检 ...
- Android 自定义view实现水波纹效果
http://blog.csdn.net/tianjian4592/article/details/44222565 在实际的开发中,很多时候还会遇到相对比较复杂的需求,比如产品妹纸或UI妹纸在哪看了 ...
- android自定义View之3D索引效果
效果图: 我的小霸王太卡了. 最近工作比较忙,今天搞了一下午才搞出来这个效果,这种效果有很多种实现方式,最常见的应该是用贝塞尔曲线实现的.今天我们来看另一种不同的实现方式,只需要用到 canvas.s ...
- Android自定义View——刮刮卡效果
想要红包的实现效果的可以关注我的博客,仿饿了么红包 下层图片:我们的红包的图片 上层图片:有两部分 一部分是灰色背景 一部分是拥有透明度为0,并且模式为交集的画笔 使用滑动监听,滑动时,用透明度为0的 ...
- Android自定义View 画弧形,文字,并增加动画效果
一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类 B ...
- Android自定义View(LimitScrollerView-仿天猫广告栏上下滚动效果)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/53303872 本文出自:[openXu的博客] 1分析 2定义组合控件布局 3继承最外层控件 ...
- 我的Android进阶之旅------>Android自定义View实现带数字的进度条(NumberProgressBar)
今天在Github上面看到一个来自于 daimajia所写的关于Android自定义View实现带数字的进度条(NumberProgressBar)的精彩案例,在这里分享给大家一起来学习学习!同时感谢 ...
- Android 自定义View合集
自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...
- Android 自定义 view(三)—— onDraw 方法理解
前言: 上一篇已经介绍了用自己定义的属性怎么简单定义一个view<Android 自定义view(二) -- attr 使用>,那么接下来我们继续深究自定义view,下一步将要去简单理解自 ...
随机推荐
- [Codeforces]605E Intergalaxy Trips
小C比较棘手的概率期望题,感觉以后这样的题还会贴几道出来. Description 给定一个n*n的邻接矩阵,邻接矩阵中元素pi,j表示的是从 i 到 j 这条单向道路在这一秒出现的概率百分比,走一条 ...
- NOIP2016 玩脱记
NOIP前: NOIP前停课了一个多月,这一个多月里浪得飞起,内心十分紧张,然后就不知不觉就到NOIP了. Day 0: 上火车前ryc给我们出了道题"一个数列,只有两个数出现了奇数次,找出 ...
- Ubuntu一些常用的软件安装及配置
软件 安装 Vim echo "y" | sudo apt-get install vim 安装搜狗输入法 这个我在虚拟机里面尝试了好多遍,不断恢复备份然后重试.终于有了这个纯靠命 ...
- input中v-model和value不能同时调用时解决方案
<input type="text" v-model="keyWord" value="请输入地名地址" > 当使用如上代码时, ...
- TypeScript: Week Reflection
TypeScript: Week Reflection Introduction Type Script already provide decorators to help developers i ...
- Angular5学习笔记 http请求
在anular4更新到angular5后,有些模块也发生了有些变化,例如http模块. 首先在app.module.ts里面引入HttpClientModule import { HttpClient ...
- c++银行家算法
#include <iostream> #include<string> #define False 0 #define True 1 using namespace std; ...
- 02_Action
1.action VS Action action:代表一个Struts2的请求 Action:能够处理action请求的类 属性名必须与JavaBeans属性名相同 属性的类型可以是任意类型,从字符 ...
- MySQL PHP 语法
MySQL PHP 语法 MySQL 可应用于多种语言,包括 PERL, C, C++, JAVA 和 PHP. 在这些语言中,MySQL在PHP的web开发中是应用最广泛. 在本教程中我们大部分实例 ...
- Docker常见仓库Node.js
Node.js 基本信息 Node.js是基于 JavaScript 的可扩展服务端和网络软件开发平台. 该仓库提供了 Node.js 0.8 ~ 0.11 各个版本的镜像. 使用方法 在项目中创建一 ...