实训第六周(2)

本次主要实现聊天消息列表中图片信息的LruCache保存实现。

1.首先初始化图片缓存,获取应用最大可用内存,取1/8作为缓存内存。

private Context mContext;
    private LruCache<String, Bitmap> mLruCache;

    public ChatUtils(Context context){
        mContext = context;

        //获取应用最大可用内存,取1/8作为缓存内存
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        int cacheMemory = maxMemory / 8;
        //初始化图片缓存
        mLruCache = new LruCache<String, Bitmap>(cacheMemory){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                //返回图片所占内存
                return value.getRowBytes() * value.getHeight();
            }
        };

    }

2.获得图片消息附件,优先显示缩略图,缩略图不存在的情况下显示原图:

public Bitmap getBitmap(ImageAttachment attachment) {

        // 优先显示缩略图,但是要限制宽高
        if (!TextUtils.isEmpty(attachment.getThumbPath())) {
            Bitmap bitmap = mLruCache.get(attachment.getThumbPath());
            if (bitmap == null){
                bitmap = ImageUtils.getBitmapFromFile(attachment.getThumbPath(), 400, 180);
                mLruCache.put(attachment.getThumbPath(),bitmap);
            }

            return bitmap;
        }

        // 缩略图不存在的情况下显示原图,但是要限制宽高
        if (!TextUtils.isEmpty(attachment.getPath())) {
            Bitmap bitmap = mLruCache.get(attachment.getPath());
            if (bitmap == null){
                bitmap = ImageUtils.getBitmapFromFile(attachment.getPath(), 400, 180);
                mLruCache.put(attachment.getPath(),bitmap);
            }

            return bitmap;
        }

        return null;
    }

3.返回IMMessage的传输状态:

public  boolean isTransferring(IMMessage message) {
        return message.getStatus() == MsgStatusEnum.sending || (message.getAttachment() != null
                && message.getAttachStatus() == AttachStatusEnum.transferring);
    }

4.返回音频文件的时长(单位:秒)

public String getAudioTime(long duration) {
        return String.valueOf(duration / 1000.0) + "'";
    }

5.MsgHeadView下拉后的加载条,用于下周的聊天列表RecycleView。

public class MsgHeadView extends LinearLayout {

    private static final String TAG = MsgHeadView.class.getSimpleName();
    private LinearLayout mContainer;
    private int mHeadHeight;

    public MsgHeadView(Context context) {
        this(context,null);
    }

    public MsgHeadView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MsgHeadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mContainer = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.msg_head_view, null);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 0, 0);
        this.setLayoutParams(lp);
        this.setPadding(0, 0, 0, 0);
        addView(mContainer, new LayoutParams(LayoutParams.MATCH_PARENT, 0));
        setGravity(Gravity.BOTTOM);
        int height = View.MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
        int width = View.MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
        mContainer.measure(width,height);
        mHeadHeight = mContainer.getMeasuredHeight();
    }

    public int getHeadHeight(){
        return mHeadHeight;
    }

    public void setVisibleHeight(int height) {
        if (height < 0) height = 0;
        LayoutParams lp = (LayoutParams) mContainer .getLayoutParams();
        lp.height = height;
        mContainer.setLayoutParams(lp);
    }

    public int getVisibleHeight() {
        LayoutParams lp = (LayoutParams) mContainer.getLayoutParams();
        return lp.height;
    }

}

msg_head_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/msg_header_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:indeterminate="true"
        android:indeterminateDrawable="@drawable/progress_bar_rotate"
        android:indeterminateBehavior="repeat"/>
即图中的加载环
展开阅读全文

没有更多推荐了,返回首页