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,下一步将要去简单理解自 ...
随机推荐
- HDU 5909 Tree Cutting
传送门 题意: 有一棵n个点的无根树,节点依次编号为1到n,其中节点i的权值为vi, 定义一棵树的价值为它所有点的权值的异或和. 现在对于每个[0,m)的整数k,请统计有多少T的非空连通子树的价值等于 ...
- 计蒜客NOIP模拟赛(2) D1T2 表演艺术
凡和邻家男孩玩完了纸牌,兴致很高,于是准备了一场表演艺术对抗赛. 他特意请来了很多表演艺术家,分成绿黑两队,进行名为 PK,实则捞金的表演. 凡为了捞金,开设了一个赌局,在比赛开始之前招揽人们来押注谁 ...
- ●SPOJ 8222 NSUBSTR–Substrings
题链: http://www.spoj.com/problems/NSUBSTR/题解: 后缀自动机. 不难发现,对于自动机里面的一个状态s, 如果其允许的最大长度为maxs[s],其right集合的 ...
- 洛谷P3275 [SCOI2011]糖果
差分约束大坑题 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring ...
- hdu 5514 Frogs(容斥)
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- 【bzoj4009 hnoi2015】接水果
题目描述 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果.由于她已经DT FC 了The big black, 她觉得这个游戏太简单了,于是发明了一个更加难的版本. 首先有 ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- 最小公共祖先 (Tarjan) POJ1470
POJ 1470 标准的LCA,输入感觉怪怪的=.= 自己看了下Tarjan,再参考了下别人的处理方法(感觉自己好弱..) #include <iostream> #include < ...
- 【NOIP2013】传染病控制
题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染病,难以准确判别病毒携带 ...
- URAL 1297 最长回文子串(后缀数组)
1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...