Commit b2a44106 authored by jianglx's avatar jianglx

添加图片上传进度

parent fc9d4725
......@@ -24,6 +24,7 @@ import com.rv.im.glide.GlideOptions;
import com.rv.im.glide.TransformationUtils;
import com.rv.im.util.DateUtils;
import com.rv.im.util.SmileyParser;
import com.rv.im.view.CycleProgress;
import com.rv.im.view.RoundImageView;
import java.util.Date;
......@@ -289,11 +290,15 @@ public class ChatContentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
holder.chatPic.setOnClickListener(v -> listener.onClick(v.getId(), bean));
holder.ivFailed.setOnClickListener(v -> listener.reSend(bean));
if (bean.getMessageStatus() == 0) {
holder.progressBar.setVisibility(View.VISIBLE);
if(bean.getUpLoadStatus() == 1){
holder.progressBar.setVisibility(View.GONE);
holder.ivFailed.setVisibility(View.GONE);
holder.tvRead.setVisibility(View.GONE);
} else if (bean.getUpLoadStatus() == 2) { // 上传 成功
holder.cycleProgress.setVisibility(View.VISIBLE);
holder.cycleProgress.setmProgress(bean.getUploadSchedule());
}else if (bean.getUpLoadStatus() == 2) { // 上传 成功
holder.cycleProgress.setVisibility(View.GONE);
if (bean.getMessageStatus() == 0) {
holder.progressBar.setVisibility(View.VISIBLE);
holder.ivFailed.setVisibility(View.GONE);
......@@ -307,11 +312,11 @@ public class ChatContentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
holder.ivFailed.setVisibility(View.VISIBLE);
holder.tvRead.setVisibility(View.GONE);
}
} else if (bean.getUpLoadStatus() == 3) { // 上传失败
holder.progressBar.setVisibility(View.GONE);
holder.ivFailed.setVisibility(View.VISIBLE);
holder.tvRead.setVisibility(View.GONE);
holder.cycleProgress.setVisibility(View.GONE);
}
}
......@@ -451,6 +456,7 @@ public class ChatContentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
ImageView chatPic;
ImageView ivFailed;
TextView tvRead;
CycleProgress cycleProgress;
public MsgToPicHolder(@NonNull View itemView) {
super(itemView);
......@@ -460,6 +466,7 @@ public class ChatContentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
progressBar = itemView.findViewById(R.id.progress);
ivFailed = itemView.findViewById(R.id.iv_failed);
tvRead = itemView.findViewById(R.id.tv_read);
cycleProgress = itemView.findViewById(R.id.cycle_progress);
}
}
......
package com.rv.im.util;
import android.app.ActivityManager;
import android.content.Context;
import java.util.ArrayList;
public class ServiceUtils {
/**
* 判断服务是否开启
*
* @return
*/
public static boolean isServiceRunning(Context context) {
ActivityManager myManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager
.getRunningServices(30);
for (int i = 0; i < runningService.size(); i++) {
if (runningService.get(i).service.getClassName()
.equals("com.rv.im.ImService")) {
return true;
}
}
return false;
}
}
......@@ -48,10 +48,9 @@ public class UploadEngine {
message.setUploadSchedule(100);
response.onUpProgress(toUserId, message);
} else {
if (totalSize != 0) {
message.setUpLoadStatus(1);
int progress = bytesWritten / totalSize * 100;
int progress = bytesWritten * 100 / totalSize ;
message.setUploadSchedule(progress);
response.onUpProgress(toUserId, message);
}
......@@ -73,6 +72,7 @@ public class UploadEngine {
}
if (result.getFailure() == 1) {//上传失败
if (response != null) {
message.setUpLoadStatus(3);
response.onFailure(toUserId, message);
}
LogUtil.d("上传文件失败,");
......@@ -100,6 +100,7 @@ public class UploadEngine {
if (TextUtils.isEmpty(url)) {//返回成功,但是却获取不到对应的URL,服务器返回值异常<概率极小>
if (response != null) {
message.setUpLoadStatus(3);
response.onFailure(toUserId, message);
}
} else {
......@@ -116,6 +117,7 @@ public class UploadEngine {
public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
requestHandleMap.remove(message.getPackId());
LogUtil.i("上传文件<" + message.getFilePath() + ">失败," + arg3);
message.setUpLoadStatus(3);
if (response != null) {
response.onFailure(toUserId, message);
}
......
package com.rv.im.view;
;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.rv.im.R;
/**
* Created by root on 2018/9/12.
*/
public class CycleProgress extends View {
private Context mContext;
private int cycleBgStrokeWidth;
private int progressStrokeWidth;
private boolean isProgressText;
private int centerTextColor;
private int centerTextSize;
private int cycleBgColor;
private int progressColor;
private Paint progressPaint;
private Paint cycleBgPaint;
private Paint centerTextPaint;
/**
* 圆心x坐标
*/
private float centerX;
/**
* 圆心y坐标
*/
private float centerY;
/**
* 圆的半径
*/
private float radius;
/**
* 进度
*/
private float mProgress = 0;
/**
* 扇形所在矩形
*/
private RectF rectF = new RectF();
public CycleProgress(Context context) {
super(context);
init(context, null);
}
public CycleProgress(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CycleProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
this.mContext = context;
getAttr(attrs); // 获取控件属性,
initPaint(); // 初始化圆圈画笔
initTextPaint(); // 初始化文字画笔
}
/**
* 获取控件属性(命名空间)
*/
private void getAttr(AttributeSet attrs) {
TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CycleProgress);
cycleBgStrokeWidth = 10;
progressStrokeWidth = 10;
isProgressText = true;
centerTextColor = Color.parseColor("#999999");
centerTextSize = 30;
cycleBgColor = Color.parseColor("#ffffff");
progressColor = Color.parseColor("#66999999");
typedArray.recycle();
}
/**
* 初始化圆圈画笔
*/
private void initPaint() {
progressPaint = getPaint(progressStrokeWidth, progressColor);
cycleBgPaint = getPaint(cycleBgStrokeWidth, cycleBgColor);
}
private Paint getPaint(int width, int color) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(width);
// Paint.Style.FILL:填充内部 Paint.Style.FILL_AND_STROKE:填充内部和描边Paint.Style.STROKE :描边
paint.setStyle(Paint.Style.STROKE);
paint.setColor(color);
paint.setAntiAlias(true); // 扛锯齿
paint.setStrokeCap(Paint.Cap.ROUND); // 两端是圆角
return paint;
}
/**
* 初始化文字画笔
*/
private void initTextPaint() {
centerTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
centerTextPaint.setColor(centerTextColor);
centerTextPaint.setTextAlign(Paint.Align.CENTER); // 设置文字位置在中间
centerTextPaint.setTextSize(centerTextSize); // 设置文字大小
centerTextPaint.setAntiAlias(true);
}
/**
* view发生改变的时候调用
*
* @param w
* @param h
* @param oldw
* @param oldh
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
centerX = w / 2;
centerY = h / 2;
radius = Math.min(w, h) / 2 - Math.max(cycleBgStrokeWidth, progressStrokeWidth); // 两数中的最小值 / 2 - 两数中的最大值
rectF.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
}
/**
* 画
*
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(centerX, centerY, radius, cycleBgPaint);
canvas.drawArc(rectF, 270, mProgress * 3.6f, false, progressPaint);
if (isProgressText) {
Paint.FontMetrics fontMetrics = centerTextPaint.getFontMetrics();
int baseline = (int) ((rectF.bottom + rectF.top - fontMetrics.bottom - fontMetrics.top) / 2);
canvas.drawText((int) mProgress + "%", rectF.centerX(), baseline, centerTextPaint);
}
}
public void setmProgress(int progress) {
this.mProgress = progress;
invalidate();
}
}
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:bottomLeftRadius="@dimen/size_100"
android:bottomRightRadius="@dimen/size_100"
android:topLeftRadius="0dp"
android:topRightRadius="@dimen/size_100" />
<solid android:color="@android:color/white" />
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:bottomLeftRadius="@dimen/size_100"
android:bottomRightRadius="@dimen/size_100"
android:topLeftRadius="@dimen/size_100"
android:topRightRadius="0dp" />
<solid android:color="#FFB74B" />
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cyclepb="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
......@@ -65,6 +66,10 @@
</RelativeLayout>
<FrameLayout
android:layout_width="120dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/chat_pic"
android:layout_width="120dp"
......@@ -73,7 +78,15 @@
android:layout_marginLeft="@dimen/dp5"
android:layout_marginTop="@dimen/chat_head_top"
android:layout_marginRight="5dp"
android:maxWidth="210dp" />
android:maxHeight="210dp" />
<com.rv.im.view.CycleProgress
android:id="@+id/cycle_progress"
android:layout_width="@dimen/dp40"
android:layout_height="@dimen/dp40"
android:layout_gravity="center" />
</FrameLayout>
<com.rv.im.view.RoundImageView
android:id="@+id/iv_header"
......
......@@ -136,4 +136,20 @@
<attr name="view_color" format="color" />
</declare-styleable>
<declare-styleable name="CycleProgress">
<attr name="circleBgStrokeWidth" format="dimension" /> <!--圈的宽度 -->
<attr name="progressStrokeWidth" format="dimension" /> <!--进度条的宽度 -->
<attr name="circleBgColor" format="color" /> <!--圈的颜色 -->
<attr name="progressColor" format="color" /> <!--进度条的颜色 -->
<attr name="circleAnimationDuration" format="integer" /> <!--进度条时长 -->
<attr name="isDrawCenterProgressText" format="boolean" /> <!--是否显示圈内文字 -->
<attr name="centerProgressTextColor" format="color"/> <!--圈内文字的颜色 -->
<attr name="centerProgressTextSize" format="dimension"/> <!--圈内文字的大小 -->
</declare-styleable>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment