Canvas绘制水波进度加载

效果:

用到图片下载:

自定义View:

  1 package com.czm.mysinkingview;
  2
  3 import android.content.Context;
  4 import android.graphics.Bitmap;
  5 import android.graphics.BitmapFactory;
  6 import android.graphics.Canvas;
  7 import android.graphics.Color;
  8 import android.graphics.Paint;
  9 import android.graphics.Paint.Style;
 10 import android.graphics.Path;
 11 import android.graphics.Path.Direction;
 12 import android.graphics.Region.Op;
 13 import android.util.AttributeSet;
 14 import android.widget.FrameLayout;
 15 /**
 16  * 水波浪球形进度View
 17  * @author caizhiming
 18  *
 19  */
 20 public class SinkingView extends FrameLayout {
 21     private static final int DEFAULT_TEXTCOLOT = 0xFFFF0000;
 22
 23     private static final int DEFAULT_TEXTSIZE =40;
 24
 25     private float mPercent;
 26
 27     private Paint mPaint = new Paint();
 28
 29     private Bitmap mBitmap;
 30
 31     private Bitmap mScaledBitmap;
 32
 33     private float mLeft;
 34
 35     private int mSpeed = 15;
 36
 37     private int mRepeatCount = 0;
 38
 39     private Status mFlag = Status.NONE;
 40
 41     private int mTextColor = DEFAULT_TEXTCOLOT;
 42
 43     private int mTextSize = DEFAULT_TEXTSIZE;
 44
 45     public SinkingView(Context context, AttributeSet attrs) {
 46         super(context, attrs);
 47     }
 48
 49     public void setTextColor(int color) {
 50         mTextColor = color;
 51     }
 52
 53     public void setTextSize(int size) {
 54         mTextSize = size;
 55     }
 56
 57     public void setPercent(float percent) {
 58         mFlag = Status.RUNNING;
 59         mPercent = percent;
 60         postInvalidate();
 61
 62     }
 63
 64     public void setStatus(Status status) {
 65         mFlag = status;
 66     }
 67
 68     public void clear() {
 69         mFlag = Status.NONE;
 70         if (mScaledBitmap != null) {
 71             mScaledBitmap.recycle();
 72             mScaledBitmap = null;
 73         }
 74
 75         if (mBitmap != null) {
 76             mBitmap.recycle();
 77             mBitmap = null;
 78         }
 79     }
 80
 81     @Override
 82     protected void dispatchDraw(Canvas canvas) {
 83         super.dispatchDraw(canvas);
 84         int width = getWidth();
 85         int height = getHeight();
 86
 87         //裁剪成圆区域
 88         Path path = new Path();
 89         canvas.save();
 90         path.reset();
 91         canvas.clipPath(path);
 92         path.addCircle(width / 2, height / 2, width / 2, Direction.CCW);
 93         canvas.clipPath(path, Op.REPLACE);
 94
 95         if (mFlag == Status.RUNNING) {
 96             if (mScaledBitmap == null) {
 97                 mBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.wave2);
 98                 mScaledBitmap = Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth(), getHeight(), false);
 99                 mBitmap.recycle();
100                 mBitmap = null;
101                 mRepeatCount = (int) Math.ceil(getWidth() / mScaledBitmap.getWidth() + 0.5) + 1;
102             }
103             for (int idx = 0; idx < mRepeatCount; idx++) {
104                 canvas.drawBitmap(mScaledBitmap, mLeft + (idx - 1) * mScaledBitmap.getWidth(), (1-mPercent) * getHeight(), null);
105             }
106             String str = (int) (mPercent * 100) + "%";
107             mPaint.setColor(mTextColor);
108             mPaint.setTextSize(mTextSize);
109             mPaint.setStyle(Style.FILL);
110             canvas.drawText(str, (getWidth() - mPaint.measureText(str)) / 2, getHeight() / 2 + mTextSize / 2, mPaint);
111
112             mLeft += mSpeed;
113             if (mLeft >= mScaledBitmap.getWidth())
114                 mLeft = 0;
115             // 绘制外圆环
116             mPaint.setStyle(Paint.Style.STROKE);
117             mPaint.setStrokeWidth(4);
118             mPaint.setAntiAlias(true);
119             mPaint.setColor(Color.rgb(33, 211, 39));
120             canvas.drawCircle(width / 2, height / 2, width / 2 - 2, mPaint);
121
122             postInvalidateDelayed(20);
123         }
124         canvas.restore();
125
126     }
127
128     public enum Status {
129         RUNNING, NONE
130     }
131
132 }

调用:

 1 package com.czm.mysinkingview;
 2
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.view.View.OnClickListener;
 7
 8 /**
 9  * 测试用例页
10  *
11  * @author caizhiming
12  */
13 public class MainActivity extends Activity {
14     private SinkingView mSinkingView;
15
16     private float percent = 0;
17
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         mSinkingView = (SinkingView) findViewById(R.id.sinking);
23
24         findViewById(R.id.btn_test).setOnClickListener(new OnClickListener() {
25
26             @Override
27             public void onClick(View v) {
28                 // TODO Auto-generated method stub
29                 updateProgress();
30             }
31         });
32
33         percent = 0.3f;
34         mSinkingView.setPercent(percent);
35     }
36
37
38     private void updateProgress() {
39         Thread thread = new Thread(new Runnable() {
40
41             @Override
42             public void run() {
43
44                 percent = 0;
45                 while (percent <= 1) {
46                     mSinkingView.setPercent(percent);
47                     percent += 0.01f;
48                     try {
49                         Thread.sleep(40);
50                     } catch (InterruptedException e) {
51                         e.printStackTrace();
52                     }
53                 }
54                 percent = 0.78f;
55                 if(percent>0.7&&percent<1){
56                     mSinkingView.setTextColor(0xFFFFFFFF);
57                 }
58                 mSinkingView.setPercent(percent);
59 //                 mSinkingView.clear();
60             }
61         });
62         thread.start();
63     }
64
65 }

调用布局:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5
 6     tools:context=".MainActivity" >
 7
 8     <com.czm.mysinkingview.SinkingView
 9         android:id="@+id/sinking"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:layout_centerInParent="true" >
13
14         <ImageView
15             android:id="@+id/image"
16             android:layout_width="120dp"
17             android:layout_height="120dp"
18             android:src="@drawable/charming2" />
19     </com.czm.mysinkingview.SinkingView>
20
21     <LinearLayout
22         android:layout_width="match_parent"
23         android:layout_height="wrap_content"
24         android:layout_alignParentBottom="true"
25         android:layout_centerHorizontal="true"
26         android:orientation="horizontal" >
27
28         <Button
29             android:id="@+id/btn_test"
30             android:textColor="#ffffff"
31             android:background="#0000ff"
32             android:textSize="18sp"
33             android:layout_width="match_parent"
34             android:layout_height="50dp"
35             android:layout_margin="10dp"
36             android:text="更新" />
37
38     </LinearLayout>
39
40 </RelativeLayout>

清单文件配置:

 1  <activity
 2             android:name="com.czm.mysinkingview.MainActivity"
 3             android:label="@string/app_name"
 4             android:hardwareAccelerated="false"
 5              >
 6             <intent-filter>
 7                 <action android:name="android.intent.action.MAIN" />
 8
 9                 <category android:name="android.intent.category.LAUNCHER" />
10             </intent-filter>
11         </activity>
时间: 2024-08-29 08:13:18

Canvas绘制水波进度加载的相关文章

Canvas制作动态进度加载水球

前言 之前看到一些球型的动态加载的效果,一直想自己动手做一个,正好这段时间重温了一个Canvas,所以就尝试了一下. 样式预览 实现思路 关于水波的实现,使用了sin()函数,通过每一帧不断的移动sin()函数曲线,实现水波动态效果.然后,通过绘制圆形路径,进行clip(),实现球型效果. sin()函数相关 这里说一下sin()函数的相关基础,对于绘制水波的影响. 看一下图,回顾一下中学sin()函数的基础. 从图中可以看出,当函数为sin(x)时,值域为[-1, 1],周期为2π. 当sin

一款基于jquery带百分比的响应式进度加载条

今天要给大家带来一款基于jquery带百分比的响应式进度加载条.这款加载条非常漂亮,而且带有进度的百度比,且在不同的百分比用的是不同的颜色.而且这款加载条采用了响应式设计,在不同的分辨率的显示器下完美支持.一起看下效果图吧. 在线预览   源码下载 实现的代码. html代码: <h1> Battle.net <b>style progress bar</b></h1> <div class="progress"> <b

使用 jsPlumb 绘制拓扑图 —— 异步加载与绘制的实现

本文实现的方法可以边异步加载数据边绘制拓扑图. 有若干点需要说明一下: 1.  一次性获取所有数据并绘制拓扑图, 请参见文章: <使用 JsPlumb 绘制拓扑图的通用方法> ; 本文实现的最终显示效果与之类似, 所使用的基本方法与之类似. 2.  在此次实现中, 可以一边异步加载数据一边绘制拓扑图, 是动态可扩展的: 3.  所有影响节点位置.布局的配置均放置在最前面, 便于修改, 避免在代码中穿梭, 浪费时间: 4.  布局算法比之前的实现更加完善: 5.  此实现由于与业务逻辑绑得比较紧

canvas 绘制圆形进度条

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>canvas 圆形进度条效果</title> <style> *{margin:0;padding:0;} body{text-align:center;background-color:#000;} </style> </he

学习 | canvas实现图片懒加载 &amp;&amp; 下滑底部加载

用canvas实现图片的懒加载并且下滑到据底部60px的时候再次加载数据,模仿UC浏览器的新闻加载. 完整代码:https://github.com/dirkhe1051931999/writeBlog/tree/master/canvasloadimg html结构 <section class="productul" id="productul"> <input type="hidden" id="pagenuml

canvas初体验之加载图片

上一篇的介绍主要是画一些基本的图案,这一篇主要是加载图案. canvas加载图片主要分为两个步骤: 1.获取图片资源. 2.将图片资源画到画布上. 1.1获取图片资源,canvasAPI为我们提供了多个方法 Image元素 <video>标签 其他的canvas中的图片资源 主要可以概括为为两种方法.

基于html5水波的加载特效

先来看效果  图片2,利用canvas生成得到. var aImgArr = [ "http://xinhuatone.com/zt/apecxjp/m/images/4.jpg", "http://xinhuatone.com/zt/apecxjp/m/images/5.jpg", "http://xinhuatone.com/zt/apecxjp/m/images/6.jpg", "http://xinhuatone.com/zt/

Spin.js-CSS动画进度加载器

spin.js是一款非常简单的CSS加载器,他是一款使用了VML(Vector Makeup Language)的CSS动画效果. spin.js的特性 他有着非常强大的适应性,有着以下几个特性: 1.    没有额外的图片,也没有增加的外部CSS文件 2.    不需要依赖于其他工具,对于jQuery而言,它支持jQuery,但是jQuery并非必须的 3.    有很高的可配置性 4.    与分辨率无关 5.    浏览器兼容性很好,在低版本的IE上,采用VML支持 6.    使用了@k

HTML5 Canvas绘制环形进度条

最近比较迷恋canvas,加之做了一个个人网站,有用到环形进度条,记录下来. canvas中没有直接绘制圆的方法,但有一个绘制弧线的context.arc方法, 下面讲下用该方法如何绘制出图片效果. arc()方法介绍 context.arc(x,y,r,sAngle,eAngle,counterclockwise); 参数说明: x: 圆的中心的 x 坐标 y: 圆的中心的 y 坐标 r: 圆的半径 sAngle: 起始角,以弧度计.(弧的圆形的三点钟位置是 0 度) eAngle: 结束角,