Android实现图片加载进度提示
作者:laibaigan 发布时间:2022-09-11 17:54:44
标签:Android,图片加载,进度
本文实例为大家分享了Android实现图片加载进度提示的具体代码,供大家参考,具体内容如下
先上图:
实现原理:
第一个控件的实现原理是重写ImageView的onDraw()方法,利用Canvas的clipRect()方法控制图片的显示区域,主键扩大图片的显示区域,从而实现逐渐增加的效果
关键代码:
public class LoadingImageView extends ImageView {
/*** 背景图片 */
private Drawable bgDrawable;
/**前景图片*/
private Drawable fgDrawable;
/**是否显示加载进度条*/
private boolean isShowProgress;
private Resources rsc;
private int progress;
private int progressHeight;
private int progressLeft;
private int progressTop;
private int progressRight;
private int progressBottom;
public LoadingImageView(Context context) {
this(context,null);
}
public LoadingImageView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public LoadingImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
rsc = getResources();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(bgDrawable==null){
return;
}
progressLeft = getMeasuredWidth()/2-(fgDrawable.getIntrinsicWidth()/2);
progressTop = getMeasuredHeight()/2-(fgDrawable.getIntrinsicHeight()/2);
progressRight = getMeasuredWidth()/2+(fgDrawable.getIntrinsicWidth()/2);
progressBottom = getMeasuredHeight()/2+(fgDrawable.getIntrinsicHeight()/2);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
/**
* 设置背景图片
* @param drawableRes
*/
public void setBgDrawableRes(int drawableRes){
bgDrawable = rsc.getDrawable(drawableRes);
invalidate();
}
public void setFgDrawableRes(int drawableRes){
fgDrawable = rsc.getDrawable(drawableRes);
invalidate();
}
public void setProgress(int progress,boolean flag) {
isShowProgress = flag;
if(progress>=0&progress<=100){
this.progress = progress;
invalidate();
}
}
@Override
protected void onDraw(Canvas canvas) {
if(bgDrawable!=null){
bgDrawable.setBounds(progressLeft, progressTop, progressRight, progressBottom);
bgDrawable.draw(canvas);
}
super.onDraw(canvas);
if(bgDrawable!=null&&isShowProgress){
bgDrawable.setBounds(progressLeft, progressTop, progressRight, progressBottom);
bgDrawable.draw(canvas);
}
if(fgDrawable!=null&&isShowProgress){
//根据进度计算图片显示的高的比
progressHeight = fgDrawable.getIntrinsicHeight()*progress/100;
//关键代码,设置图片的显示区域
canvas.clipRect(progressLeft,progressBottom-progressHeight,progressRight,progressBottom);
fgDrawable.setBounds(progressLeft, progressTop, progressRight, progressBottom);
fgDrawable.draw(canvas);
}
}
}
第二个圆形加载进度的原理其实也很简单,就是画弧线,不断增加弧线的角度,实现改变进度的功能
关键代码:
public class LoadingCircleView extends View {
private final Paint paint;
private final Context context;
private Resources res;
private int progress;
private int ringWidth;
//圆环的颜色
private int ringColor;
//进度条颜色
private int progressColor;
//字体颜色
private int textColor;
//字的大小
private int textSize;
private String textProgress;
public LoadingCircleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
this.paint = new Paint();
this.res = context.getResources();
this.paint.setAntiAlias(true); //消除锯齿
this.ringWidth = dip2px(context, 10); //设置圆环宽度
this.ringColor = Color.rgb(233, 233, 233);
this.progressColor = Color.rgb(146, 206, 108);
this.textColor = Color.rgb(203, 203, 203);
this.textSize = 30;
}
public LoadingCircleView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public LoadingCircleView(Context context) {
this(context,null);
}
/**
* 设置加载进度,取值范围在0~100之间
* @param progress
*/
public void setProgress(int progress) {
if(progress>=0&&progress<=100){
this.progress = progress;
invalidate();
}
}
/**
* 设置圆环背景色
* @param ringColor
*/
public void setRingColor(int ringColor) {
this.ringColor = res.getColor(ringColor);
}
/**
* 设置进度条颜色
* @param progressColor
*/
public void setProgressColor(int progressColor) {
this.progressColor = res.getColor(progressColor);
}
/**
* 设置字体颜色
* @param textColor
*/
public void setTextColor(int textColor) {
this.textColor = res.getColor(textColor);
}
/**
* 设置字体大小
* @param textSize
*/
public void setTextSize(int textSize) {
this.textSize = textSize;
}
/**
* 设置圆环半径
* @param ringWidth
*/
public void setRingWidthDip(int ringWidth) {
this.ringWidth = dip2px(context, ringWidth);
}
/**
* 通过不断画弧的方式更新界面,实现进度增加
*/
@Override
protected void onDraw(Canvas canvas) {
int center = getWidth()/2;
int radios = center-ringWidth/2;
//绘制圆环
this.paint.setStyle(Paint.Style.STROKE); //绘制空心圆
this.paint.setColor(ringColor);
this.paint.setStrokeWidth(ringWidth);
canvas.drawCircle(center,center, radios, this.paint);
RectF oval = new RectF(center-radios, center-radios, center+radios, center+radios);
this.paint.setColor(progressColor);
canvas.drawArc(oval, 90, 360*progress/100, false, paint);
this.paint.setStyle(Paint.Style.FILL);
this.paint.setColor(textColor);
this.paint.setStrokeWidth(0);
this.paint.setTextSize(textSize);
this.paint.setTypeface(Typeface.DEFAULT_BOLD);
textProgress = progress+"%";
float textWidth = paint.measureText(textProgress);
canvas.drawText(textProgress, center-textWidth/2, center+textSize/2, paint);
super.onDraw(canvas);
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
} }
控件定义好后就可以再Xml里面调用了:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.example.imagetest.LoadingImageView
android:id="@+id/loading_image_view"
android:layout_width="258px"
android:layout_height="257px"
android:background="#330000"
>
</com.example.imagetest.LoadingImageView>
<com.example.imagetest.LoadingCircleView
android:id="@+id/loading_cirle_view"
android:layout_width="100dp"
android:layout_height="100dp"
>
</com.example.imagetest.LoadingCircleView>
<!--
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></ListView> -->
</LinearLayout>
最后就可以使用了,在主线程里面模拟加载进度,起一个线程,模仿加载进度逐渐增加:
public class MainActivity extends Activity {
ListView listview;
private LoadingImageView loadingImageView;
private LoadingCircleView loadingCircleView;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
loadingImageView.setProgress(msg.what,true);
loadingCircleView.setProgress(msg.what);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadingImageView = (LoadingImageView) findViewById(R.id.loading_image_view);
loadingImageView.setFgDrawableRes(R.drawable.bg_click_load_img);
loadingImageView.setBgDrawableRes(R.drawable.ic_launcher);
loadingImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
loading();
}
});
loadingCircleView = (LoadingCircleView) findViewById(R.id.loading_cirle_view);
loadingCircleView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
loading();
}
});
// listview = (ListView) findViewById(R.id.listview);
// showImage();
}
private void loading(){
Thread t = new Thread(){
@Override
public void run() {
int i = 0;
while(i<=100){
try {
i++;
handler.sendEmptyMessage(i);
this.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
super.run();
}
};
t.start();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
来源:https://blog.csdn.net/laibaigan/article/details/8822477
0
投稿
猜你喜欢
- 1.固定大小的线程池简介线程池就是在程序启动的时候先建立几个可以使用的线程放在那里,然后等着具体的任务放进去,这个任务基本可以说都是Runn
- 发现问题肯定有人发现连接mysql失败,然后又找不到问题所在,又出现一大最报错,如下图。解决过程 1.先查询自己的java版本,在
- 抽象类和抽象方法常用知识点:(1)抽象类作为被继承类,子类必须实现抽象类中的所有抽象方法,除非子类也为抽象类。也就是说,如果子类也为抽象类,
- 最近的项目中要实现一个聊天的功能,类似于斗鱼TV的聊天室功能,与服务器端人商量后决定用WebSocket来做,但是在这之前我只知道Socke
- 故障:收到服务器报警,内存使用率超过80%1.查看使用dstat和top查看内存使用最高的应用使用dstat查到内存占用最高的是java应用
- 一、findById(ID id)通过id进行单条查询,先看看 findById(ID id) 的源码@Overridepublic Opt
- eclipse配置tomcattomcat资源下载如果打开没有servers请查看这篇文章:eclipse创建项目没有dynamic web
- 1、servlet层package com.ycz.controller;import com.alibaba.fastjson.JSON;
- 在C#2.0中,微软给我们带来了一些新的特性,例如泛型,匿名委托等。然而,这些新的特性多多少少会给人一种从别的语言中“抄”来的感觉(例如泛型
- Remote Debug 综述当我们的后台项目部署到服务器上时,由于环境和本地不同,有时候也会有一些奇奇怪怪的问题出现。只依赖服务器上的日志
- 1. 通过设置采样率压缩res资源图片压缩 decodeResource public Bitmap decodeSampled
- 介绍超级管理员:系统管理、用户管理、网点管理、运输点管理、快递员管理、网点申请管理(审核)、报价管理(时效报价)等。普通用户:注册登录、个人
- 泛型中 T 类型变量 和 ? 通配符 区别定义不同 :T 是类型变量,? 是通配符使用范围不同:? 通配符用作 参数类型、字段类型、局部变量
- 首先什么是注解?@Override就是注解,它的作用是:1、检查是否正确的重写了父类中的方法。 2、标明代码,这是一个重写的方法。1、体现在
- 写完js倒计时,突然想用java实现倒计时,写了三种实现方式一:设置时长的倒计时;二:设置时间戳的倒计时;三:使用java.util.Tim
- /// <summary> /// 将List转换成Da
- @ApiModel使用场景在实体类上边使用,标记类时swagger的解析类概述提供有关swagger模型的其它信息,类将在操作中用作类型时自
- 目录一对一关联保存:留言表实体类配置:(主表)留言内容表配置:(从表)留言表hbm.xml配置:(主表)留言内容表hbm.xml配置:(从表
- C语言数据结构实现银行模拟实现代码:#include <stdio.h> #include <stdlib.h> #
- 前言本文重点是要将mongodb与spring整合到项目中去,在实践中发现问题,追踪问题,然后解决问题。下面话不多说了,来一起看看详细的介绍