android替换Glide通讯组件为Okhttp并监控加载进度,并发编程挑战

return responseBody.contentType();

}

@Override

public long contentLength() {

return responseBody.contentLength();

}

@Override

public BufferedSource source() {

if (bufferedSource == null) {

bufferedSource = Okio.buffer(new ProgressSource(responseBody.source()));

}

return bufferedSource;

}

private class ProgressSource extends ForwardingSource {

long totalBytesRead = 0;

int currentProgress;

ProgressSource(Source source) {

super(source);

}

@Override

public long read(Buffer sink, long byteCount) throws IOException {

long bytesRead = super.read(sink, byteCount);

long fullLength = responseBody.contentLength();

if (bytesRead == -1) {

totalBytesRead = fullLength;

} else {

totalBytesRead += bytesRead;

}

int progress = (int) (100f * totalBytesRead / fullLength);

Log.d(TAG, "download progress is " + progress);

if (listener != null && progress != currentProgress) {

listener.onProgress(progress);

}

if (listener != null && totalBytesRead == fullLength) {

listener = null;

}

currentProgress = progress;

return bytesRead;

}

}

}

布局

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“tsou.cn.glidetest.ListActivity”>

<android.support.v4.widget.SwipeRefreshLayout

android:id=“@+id/swipe_refresh”

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<android.support.v7.widget.RecyclerView

android:id=“@+id/recyclerview”

android:layout_width=“match_parent”

android:layout_height=“match_parent” />

</android.support.v4.widget.SwipeRefreshLayout>

使用的Adapter


package tsou.cn.glidetest.adapter;

import android.support.annotation.Nullable;

import android.widget.ImageView;

import android.widget.ProgressBar;

import com.chad.library.adapter.base.BaseQuickAdapter;

import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import java.util.Locale;

import tsou.cn.glidetest.R;

import tsou.cn.glidetest.Util.ImageLoadUtil;

import tsou.cn.glidetest.bean.ListBean;

import tsou.cn.glidetest.view.RoundProgressBar;

/**

  • Created by Administrator on 2017/11/13 0013.

*/

public class ListAdapter extends BaseQuickAdapter<ListBean, BaseViewHolder> {

public ListAdapter(@Nullable List data) {

super(R.layout.item_list, data);

}

@Override

protected void convert(BaseViewHolder helper, ListBean item) {

ImageLoadUtil.display((ImageView) helper.getView(R.id.iv_list_home_photo),

(RoundProgressBar) helper.getView(R.id.round_progressbar), item.getImage());

helper.setText(R.id.tv_list_home_title, item.getTitle());

helper.setText(R.id.tv_list_home_source,

String.format(Locale.getDefault(), mContext.getString(R.string.source), item.getSource()));

helper.setText(R.id.tv_list_home_focus,

String.format(Locale.getDefault(), “%d”, item.getStar()));

helper.setText(R.id.tv_list_home_comments,

String.format(Locale.getDefault(), “%d”, item.getEvn()));

}

}

adapter中的条目布局


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:android_custom=“http://schemas.android.com/apk/res-auto”

xmlns:app=“http://schemas.android.com/apk/res-auto”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”>

<android.support.v7.widget.CardView

android:id=“@+id/cardview”

android:layout_width=“135dp”

android:layout_height=“90dp”

android:layout_marginLeft=“10px”

android:layout_marginRight=“40px”

android:layout_marginTop=“30px”

android:foreground=“?attr/selectableItemBackground”

app:cardBackgroundColor=“@android:color/white”

app:cardCornerRadius=“20px”

app:cardElevation=“10px”

app:cardPreventCornerOverlap=“false”

app:cardUseCompatPadding=“true”

app:contentPadding=“0px”>

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<ImageView

android:id=“@+id/iv_list_home_photo”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:scaleType=“fitXY” />

<tsou.cn.glidetest.view.RoundProgressBar

android:id=“@+id/round_progressbar”

android:layout_width=“55dip”

android:layout_height=“55dip”

android:layout_centerInParent=“true”

android_custom:progress=“1”

android_custom:roundColor=“#D1D1D1”

android_custom:roundProgressColor=“#3F51B5”

android_custom:roundWidth=“5dip”

android_custom:textColor=“#3F51B5”

android_custom:textSize=“12sp” />

</android.support.v7.widget.CardView>

<TextView

android:id=“@+id/tv_list_home_title”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_alignTop=“@id/cardview”

android:layout_marginRight=“20px”

android:layout_toRightOf=“@id/cardview”

android:ellipsize=“end”

android:lineSpacingMultiplier=“1.2”

android:maxLines=“2”

android:paddingTop=“15px”

android:text=“@string/app_name”

android:textColor=“@android:color/black”

android:textSize=“14sp” />

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_alignBottom=“@id/cardview”

android:layout_alignLeft=“@id/tv_list_home_title”

android:layout_marginRight=“20px”>

<TextView

android:id=“@+id/tv_list_home_comments”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentRight=“true”

android:drawableLeft=“@mipmap/icon_comments”

android:drawablePadding=“12px”

android:text=“%d”

android:textColor=“@android:color/darker_gray”

android:textSize=“12sp” />

<TextView

android:id=“@+id/tv_list_home_focus”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginRight=“25px”

android:layout_toLeftOf=“@id/tv_list_home_comments”

android:drawableLeft=“@mipmap/icon_star”

android:drawablePadding=“12px”

android:text=“%d”

android:textColor=“@android:color/darker_gray”

android:textSize=“12sp” />

<TextView

android:id=“@+id/tv_list_home_source”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginBottom=“25px”

android:layout_marginRight=“10px”

android:layout_toLeftOf=“@id/tv_list_home_focus”

android:singleLine=“true”

android:text=“@string/source”

android:textColor=“@android:color/darker_gray”

android:textSize=“12sp” />

主页面代码逻辑


package tsou.cn.glidetest;

import android.os.AsyncTask;

import android.os.Bundle;

import android.os.SystemClock;

import android.support.v4.widget.SwipeRefreshLayout;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import tsou.cn.glidetest.adapter.ListAdapter;

import tsou.cn.glidetest.bean.ListBean;

import tsou.cn.glidetest.view.CustomLoadMoreView;

public class ListActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener,

BaseQuickAdapter.RequestLoadMoreListener {

private RecyclerView mRecyclerview;

private SwipeRefreshLayout mSwipeRefresh;

private ListAdapter mAdapter;

private int mLastIndex;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_list);

initView();

initRecyclerView();

initListener();

mAdapter.setEnableLoadMore(false);

mSwipeRefresh.setRefreshing(true);

fetchData(true);

}

private void initView() {

mRecyclerview = (RecyclerView) findViewById(R.id.recyclerview);

mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);

}

private void initRecyclerView() {

mRecyclerview.setLayoutManager(new LinearLayoutManager(this));

mAdapter = new ListAdapter(null);

mAdapter.isFirstOnly(false);

mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

mAdapter.setLoadMoreView(new CustomLoadMoreView());

mRecyclerview.setAdapter(mAdapter);

}

private void initListener() {

mSwipeRefresh.setOnRefreshListener(this);

mAdapter.setOnLoadMoreListener(this, mRecyclerview);

}

private boolean mIsRefresh;

private void fetchData(boolean isRefresh) {

mIsRefresh = isRefresh;

if (isRefresh) {

mLastIndex = 1;

}

new AsyncTask<Void, Void, Void>() {

private List lists = new ArrayList<>();

@Override

protected Void doInBackground(Void… voids) {

SystemClock.sleep(3000);

if (mLastIndex <= 3) {

// 加载数据

for (int i = 0; i < 10; i++) {

ListBean listBean = new ListBean();

Random random = new Random();

listBean.setImage(“https://unsplash.it/200/200?random&” + random.nextInt(225));

listBean.setTitle(“刘芳,20岁,河北保定人,9个月前,她辞去了北京一饭店服务员的工作,” +

“来到王庆坨镇一家自行车生产上班,只因为好朋友邀请她,” +

““快来,这里现在有好多共享单车的订单”。” +

““她说他们公司忙都忙不过来,几乎天天在加班。”刘芳说,好友告诉她,” +

“加班很累,但收入可观,多的时候一个月能挣到六七千”);

listBean.setSource(“腾讯网”);

listBean.setStar(random.nextInt(100));

listBean.setEvn(random.nextInt(100));

lists.add(listBean);

}

} else {

lists.clear();

}

return null;

}

@Override

protected void onPostExecute(Void aVoid) {

super.onPostExecute(aVoid);

if (mIsRefresh) {

mAdapter.setNewData(lists);

mSwipeRefresh.setRefreshing(false);

mAdapter.setEnableLoadMore(true);

} else {

if (lists != null) {

if (lists.size() == 0) {

mAdapter.loadMoreEnd();

} else {

mAdapter.addData(lists);

mAdapter.loadMoreComplete();

}

}

}

mLastIndex++;

}

}.execute();

}

@Override

public void onRefresh() {

mAdapter.setEnableLoadMore(false);

fetchData(true);

}

@Override

public void onLoadMoreRequested() {

if (mSwipeRefresh != null) {

mSwipeRefresh.setEnabled(false);

fetchData(false);

mSwipeRefresh.setEnabled(true);

}

}

}

最后使用Glide进行图片加载


package tsou.cn.glidetest.Util;

import android.graphics.drawable.Drawable;

import android.view.View;

import android.widget.ImageView;

import com.bumptech.glide.Glide;

import com.bumptech.glide.load.engine.DiskCacheStrategy;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;

import com.bumptech.glide.request.animation.GlideAnimation;

import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;

import com.bumptech.glide.request.target.Target;

import tsou.cn.glidetest.Glide.okhttp.ProgressInterceptor;

import tsou.cn.glidetest.Glide.okhttp.ProgressListener;

import tsou.cn.glidetest.R;

import tsou.cn.glidetest.view.RoundProgressBar;

/**

  • 图片加载类封装

*/

public class ImageLoadUtil {

public static ImageView display(ImageView img, RoundProgressBar roundProgressBar, String url) {

ProgressInterceptor.addListener(url, new MyProgressListener(roundProgressBar));

Glide.with(UIUtils.getContext())

.load(url)

.placeholder(R.drawable.app_loading_pic)

.error(R.drawable.app_loading_pic)

.diskCacheStrategy(DiskCacheStrategy.NONE)

.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)

.into(new MyGlideDrawableImageViewTarget(img, roundProgressBar, url));

return img;

}

private static class MyProgressListener implements ProgressListener {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

总结

【Android 详细知识点思维脑图(技能树)】

image

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

详细整理在GitHub:Android架构视频+BAT面试专题PDF+学习笔记​

图片转存中…(img-RfNosk4y-1711370060776)]
[外链图片转存中…(img-gJtkW95t-1711370060777)]
[外链图片转存中…(img-qdVaMuCJ-1711370060777)]
[外链图片转存中…(img-lbvbgnmm-1711370060778)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-4oxU1tnJ-1711370060778)]

总结

【Android 详细知识点思维脑图(技能树)】

[外链图片转存中…(img-GZLCHnC4-1711370060778)]

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

[外链图片转存中…(img-XK0GaOpG-1711370060779)]

详细整理在GitHub:Android架构视频+BAT面试专题PDF+学习笔记​

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

  • 20
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值