Android 自定义view实现进度条加载效果实例代码
作者:zhoushenxian 时间:2022-12-21 05:33:48
这个其实很简单,思路是这样的,就是拿view的宽度,除以点的点的宽度+二个点 之间的间距,就可以算出大概能画出几个点出来,然后就通过canvas画出点,再然后就是每隔多少时间把上面移动的点不断的去改变它的坐标就可以,
效果如下:
分析图:
代码如下:
package com.example.dotloadview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by zhouguizhi on 6/18/15.
*/
public class DotLoadProgressView extends View {
private boolean isStart = false;
private int dotIndex =0;//点下面的角标
private int dotNumber = 0;
private Paint mPaint;
private int paddingTop = 50;
private int paddingBottom = 50;
private int paddingLeft = 30;
private int paddingRight = 30;
private Handler mHandler = new Handler();
private long DELAY_TIME = 300;
private Bitmap bitmap;
private int itemDotSpace = 10;//二个点之间的距离
private Bitmap moveBitmap ;
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
if (dotNumber != 0){
dotIndex = (dotIndex + 1) % dotNumber;
}
invalidate();
mHandler.postDelayed(mRunnable, DELAY_TIME);
}
};
public DotLoadProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
moveBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.active_dot);
init();
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setDither(true);
mPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(int i=0;i<dotNumber;i++){
canvas.drawBitmap(bitmap,paddingLeft+i*itemDotSpace+bitmap.getWidth()*i,paddingTop,mPaint);
}
if(isStart){
canvas.drawBitmap(moveBitmap,paddingLeft+dotIndex*itemDotSpace+moveBitmap.getWidth()*dotIndex,paddingTop,mPaint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.inactive_dot);
setMeasuredDimension(parentWidth,bitmap.getHeight()+paddingTop+paddingBottom);
int widthWithoutPadding = parentWidth - paddingLeft - paddingRight;
dotNumber = calculateDotsNumber(widthWithoutPadding);
}
private int calculateDotsNumber(int width) {
int number = (width / (itemDotSpace + bitmap.getWidth()));
return number;
}
public void startProgress() {
isStart = true;
dotIndex = -1;
mHandler.removeCallbacks(mRunnable);
mHandler.post(mRunnable);
}
public void stopProgress() {
isStart = false;
mHandler.removeCallbacks(mRunnable);
invalidate();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
startProgress();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
stopProgress();
}
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.dotloadview.DotLoadProgressView
android:id="@+id/progress1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
二个点其实是图片,在网上别人项目中拿出来用的,如果感觉那个点图片大了的话,有二个办法,第一叫你们美工切小点,第二就是通过drawable.setBound()去限制他大小了,我在这其实是偷懒,没有那么做,当然这点你也可以通过shade去做,而不用图片的话
最终的效果图还是这样的:
总结
以上所述是小编给大家介绍的Android 自定义view实现进度条加载效果实例代码网站的支持!
来源:http://blog.csdn.net/coderinchina/article/details/77626339
标签:android,view,进度条
0
投稿
猜你喜欢
C# task应用实例详解
2023-01-22 05:23:53
Spring Data Jpa实现分页和排序代码实例
2021-11-08 01:19:48
Mybatis中的Criteria条件查询方式
2022-02-21 06:49:46
详细分析Java内存模型
2022-01-31 09:37:05
C语言实现Flappy Bird小游戏
2022-03-13 13:56:24
mybatis中使用InsertProvider注解报错解决全过程
2021-06-25 18:36:42
Spring Security基于JWT实现SSO单点登录详解
2022-02-28 10:56:55
SpringBoot+Spring Security+JWT实现RESTful Api权限控制的方法
2022-07-18 03:38:36
详解C语言的mem系列函数
2021-07-10 13:10:18
android命令行模拟输入事件(文字、按键、触摸等)
2021-10-30 04:48:06
详解SpringBoot定时任务说明
2022-08-11 05:03:11
c#保存窗口位置大小操作类(序列化和文件读写功能)
2023-07-15 18:51:06
Java Web实现文件下载和乱码处理方法
2022-03-14 20:28:38
泛谈Java中的不可变数据结构
2022-02-18 00:12:54
springboot读取resources下文件的方式详解
2022-05-21 16:14:48
c#模拟银行atm机示例分享
2023-04-01 10:59:45
Java多线程之彻底搞懂线程池
2023-12-18 23:54:09
Java常见数据结构面试题(带答案)
2023-11-24 19:44:05
Java数据结构之ArrayList从顺序表到实现
2022-06-14 00:53:25
Android SQLite数据库增删改查操作的使用详解
2023-04-18 09:17:22