Przeglądaj źródła

1.优化页面UI

王鹏鹏 2 lat temu
rodzic
commit
e869316443

+ 100 - 0
baselib/src/main/java/com/yingyangfly/baselib/utils/img/GlideRoundTransform.java

@@ -0,0 +1,100 @@
+package com.yingyangfly.baselib.utils.img;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.BitmapShader;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.RectF;
+
+import androidx.annotation.NonNull;
+
+import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
+import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
+import com.bumptech.glide.util.Util;
+
+import java.nio.ByteBuffer;
+import java.security.MessageDigest;
+
+public class GlideRoundTransform extends BitmapTransformation {
+
+    private static final String ID = "com.xiaohe.www.lib.tools.glide.GlideRoundTransform";
+
+    private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
+
+    private int radius;
+
+    public GlideRoundTransform(Context context, int radius) {
+
+        this.radius = dip2px(context, radius);
+
+    }
+
+    @Override
+
+    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
+
+        int width = toTransform.getWidth();
+
+        int height = toTransform.getHeight();
+
+        Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
+
+        bitmap.setHasAlpha(true);
+
+        Canvas canvas = new Canvas(bitmap);
+
+        Paint paint = new Paint();
+
+        paint.setAntiAlias(true);
+
+        paint.setShader(new BitmapShader(toTransform, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
+
+        canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);
+
+        return bitmap;
+
+    }
+
+    @Override
+
+    public boolean equals(Object obj) {
+
+        if (obj instanceof GlideRoundTransform) {
+
+            GlideRoundTransform other = (GlideRoundTransform) obj;
+
+            return radius == other.radius;
+
+        }
+
+        return false;
+
+    }
+
+    @Override
+
+    public int hashCode() {
+
+        return Util.hashCode(ID.hashCode(), Util.hashCode(radius));
+
+    }
+
+    @Override
+
+    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
+
+        messageDigest.update(ID_BYTES);
+
+        byte[] radiusData = ByteBuffer.allocate(4).putInt(radius).array();
+
+        messageDigest.update(radiusData);
+
+    }
+
+    //dp转px
+    public int dip2px(Context context, float dpValue) {
+        final float scale = context.getResources().getDisplayMetrics().density;
+        return (int) (dpValue * scale + 0.5f);
+    }
+}

+ 252 - 0
baselib/src/main/java/com/yingyangfly/baselib/utils/img/ImgUtil.java

@@ -0,0 +1,252 @@
+package com.yingyangfly.baselib.utils.img;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.Matrix;
+import android.graphics.drawable.Drawable;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+
+import androidx.annotation.NonNull;
+
+import com.bumptech.glide.Glide;
+import com.bumptech.glide.load.engine.DiskCacheStrategy;
+import com.bumptech.glide.load.resource.bitmap.CenterCrop;
+import com.bumptech.glide.load.resource.bitmap.CircleCrop;
+import com.bumptech.glide.request.RequestOptions;
+import com.bumptech.glide.request.target.SimpleTarget;
+import com.bumptech.glide.request.transition.Transition;
+import com.yingyangfly.baselib.R;
+
+import java.io.ByteArrayOutputStream;
+
+import io.reactivex.annotations.Nullable;
+
+/**
+ * Author: YongChao
+ * Date: 19-8-21 下午5:49
+ * Description:
+ */
+public class ImgUtil {
+    public static void loadImgFace(Context context, String face_url, final ImageView img) {
+        RequestOptions options = new RequestOptions()
+                .placeholder(R.mipmap.icon_place_holder)
+                .error(R.mipmap.icon_place_holder)
+                .fitCenter()
+                .diskCacheStrategy(DiskCacheStrategy.NONE);
+        Glide.with(context)
+                .load(face_url)
+                .into(new SimpleTarget<Drawable>() {
+                    @Override
+                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
+                        img.setImageDrawable(resource);
+                    }
+                });
+    }
+
+    public static void loadImgCircle(Context context, String imgUrl, ImageView img, int dPlaceHolder, int dError) {
+        RequestOptions options = new RequestOptions()
+                .placeholder(dPlaceHolder)
+                .error(dError)
+                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
+                .diskCacheStrategy(DiskCacheStrategy.ALL);
+
+        Glide.with(context).load(imgUrl).apply(options).into(img);
+    }
+
+    public static void loadImgHeadCircle(Context context, String imgUrl, ImageView img, int dPlaceHolder, int dError) {
+        RequestOptions options = new RequestOptions()
+                .placeholder(dPlaceHolder)
+                .error(dError)
+                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
+                .diskCacheStrategy(DiskCacheStrategy.NONE);
+
+        Glide.with(context).load(imgUrl).apply(options).into(img);
+    }
+
+    public static void loadAvatarRound(Context context, String imgUrl, ImageView img) {
+        RequestOptions options = new RequestOptions()
+                .placeholder(R.mipmap.icon_place_holder)
+                .error(R.mipmap.icon_place_holder)
+                .transform(new CenterCrop(), new GlideRoundTransform(context, 5))
+                .diskCacheStrategy(DiskCacheStrategy.ALL);
+
+        Glide.with(context).load(imgUrl).apply(options).into(img);
+    }
+
+    public static void loadImgCircle(Context context, String imgUrl, ImageView img) {
+        loadImgCircle(context, imgUrl, img, R.mipmap.icon_place_holder, R.mipmap.icon_place_holder);
+    }
+
+    /**
+     * @param: [context, imgUrl, img]
+     * @return: void
+     * @description: 设置加载占位符及加载失败
+     */
+    public static void loadImgPlaceHolderError(Context context, String imgUrl, ImageView img, int dPlaceHolder, int dError) {
+        RequestOptions options = new RequestOptions()
+                .placeholder(dPlaceHolder)
+                .error(dError)
+                .diskCacheStrategy(DiskCacheStrategy.ALL);
+
+        Glide.with(context).load(imgUrl).apply(options).into(img);
+    }
+
+    public static void loadImg(Context context, String imgUrl, ImageView img) {
+        loadImgPlaceHolderError(context, imgUrl, img, R.mipmap.icon_place_holder, R.mipmap.icon_place_holder);
+    }
+
+    public static void loadImgNoPlaceHolder(Context context, String imgUrl, ImageView img) {
+        RequestOptions options = new RequestOptions()
+                .diskCacheStrategy(DiskCacheStrategy.ALL);
+        Glide.with(context).load(imgUrl).apply(options).into(img);
+    }
+
+    public static void loadGifImg(Context context, int imgUrl, ImageView img) {
+        Glide.with(context).asGif().load(imgUrl).into(img);
+    }
+
+    public static void zoomImg(Context context, String imgUrl, ImageView img) {
+        Glide.with(context)
+                .asBitmap()
+                .load(imgUrl)
+                .into(new TransformationUtils(img));
+    }
+
+    /**
+     * 圆角图片
+     *
+     * @param context
+     * @param imgUrl
+     * @param img
+     * @param dPlaceHolder
+     * @param dError
+     */
+    public static void loadRoundImg(Context context, String imgUrl, ImageView img, int dPlaceHolder, int dError) {
+        loadRoundImg(context, imgUrl, img, dPlaceHolder, dError, 5);
+    }
+
+    public static void loadRoundImg(Context context, String imgUrl, ImageView img, int dPlaceHolder, int dError, int radius) {
+
+        RequestOptions options = new RequestOptions()
+                .placeholder(dPlaceHolder)
+                .error(dError)
+                .diskCacheStrategy(DiskCacheStrategy.ALL);
+
+        Glide.with(context).load(imgUrl).transform(new GlideRoundTransform(context, radius)).apply(options).into(img);
+    }
+
+    public static void loadRoundImg(Context context, int imgLocoalSrc, ImageView img, int radius) {
+        Glide.with(context).load(imgLocoalSrc).transform(new GlideRoundTransform(context, radius)).into(img);
+    }
+
+    public static void loadRoundImg(Context context, String imgUrl, ImageView img) {
+        loadRoundImg(context, imgUrl, img, R.mipmap.icon_place_holder, R.mipmap.icon_place_holder);
+    }
+
+    public static void loadBitmap(Context context, Bitmap bitmap, ImageView imageView) {
+        Glide.with(context).load(bitmap).diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView);
+    }
+
+    public static void loadImage(Context context, int id, ImageView imageView) {
+        Glide.with(context).load(id).into(imageView);
+    }
+
+    /**
+     * Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb
+     *
+     * @param bitmap
+     * @param maxKb
+     * @return
+     */
+    public static byte[] bmpToByteArray(Bitmap bitmap, long maxKb, boolean needRecycle) {
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
+//        LogUtil.Companion.i("output " + output.toByteArray().length);
+//        LogUtil.Companion.i("output maxKb " + maxKb);
+        int options = 100;
+//        int fileSize = output.toByteArray().length;
+//        options = (int) Math.max(1, maxKb * 100 / fileSize);
+//        LogUtil.Companion.i("output option " + options);
+        while (output.toByteArray().length > maxKb && options > 0) {
+            output.reset(); //清空output
+            bitmap.compress(
+                    Bitmap.CompressFormat.JPEG,
+                    options,
+                    output
+            ); //这里压缩options%,把压缩后的数据存放到output中
+//            LogUtil.Companion.i("output " + output.toByteArray().length);
+            options -= 10;
+        }
+        if (needRecycle) {
+            bitmap.recycle();
+        }
+        return output.toByteArray();
+    }
+
+
+    /**
+     * 加载自定义宽高圆角图片
+     */
+    public static void loadRoundImgWidth(Context context, String imgUrl, ImageView img, int dPlaceHolder, int dError, int radius, int width) {
+        RequestOptions options = new RequestOptions()
+                .placeholder(dPlaceHolder)
+                .error(dError)
+                .diskCacheStrategy(DiskCacheStrategy.ALL);
+
+        ViewGroup.LayoutParams params = img.getLayoutParams();
+        params.height = width;
+        params.width = width;
+        img.setLayoutParams(params);
+
+        Glide.with(context).load(imgUrl).transform(new GlideRoundTransform(context, radius)).apply(options).into(img);
+    }
+
+    /**
+     * 加载自定义宽高圆角图片
+     */
+    public static void loadRoundImgWidthThumbnail(Context context, String imgUrl, ImageView img, int dPlaceHolder, int dError, int radius, int width) {
+        RequestOptions options = new RequestOptions()
+                .placeholder(dPlaceHolder)
+                .error(dError)
+                .diskCacheStrategy(DiskCacheStrategy.ALL);
+
+        ViewGroup.LayoutParams params = img.getLayoutParams();
+        params.height = width;
+        params.width = width;
+        img.setLayoutParams(params);
+
+        Glide.with(context).load(imgUrl).thumbnail(0.1f).transform(new GlideRoundTransform(context, radius)).apply(options).into(img);
+    }
+
+    /**
+     * 加载自定义宽高圆角图片
+     */
+    public static void loadRoundImgWidthThumbnail(Context context, String imgUrl, ImageView img, int dPlaceHolder, int dError, int radius, int width, float sizeMultiplier) {
+        RequestOptions options = new RequestOptions()
+                .placeholder(dPlaceHolder)
+                .error(dError)
+                .diskCacheStrategy(DiskCacheStrategy.ALL);
+
+        ViewGroup.LayoutParams params = img.getLayoutParams();
+        params.height = width;
+        params.width = width;
+        img.setLayoutParams(params);
+
+        Glide.with(context).load(imgUrl).thumbnail(sizeMultiplier).transform(new GlideRoundTransform(context, radius)).apply(options).into(img);
+    }
+
+    public static Bitmap convertBitmapSize(Bitmap bm, int newWidth, int newHeight) { // 获得图片的宽高
+        int width = bm.getWidth();
+        int height = bm.getHeight();
+        // 计算缩放比例
+        float scaleWidth = Float.parseFloat(newWidth + "") / width;
+        float scaleHeight = Float.parseFloat(newHeight + "") / height;
+        // 取得想要缩放的matrix参数
+        Matrix matrix = new Matrix();
+        matrix.postScale(scaleWidth, scaleHeight);
+        // 得到新的图片
+        return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
+    }
+
+}

+ 53 - 0
baselib/src/main/java/com/yingyangfly/baselib/utils/img/SaveBitmapToPhoto.java

@@ -0,0 +1,53 @@
+package com.yingyangfly.baselib.utils.img;
+
+import android.content.Context;
+import android.content.Intent;
+import android.graphics.Bitmap;
+import android.net.Uri;
+import android.os.Environment;
+
+import com.yingyangfly.baselib.utils.LogUtil;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class SaveBitmapToPhoto {
+    /**
+     * 保存图片到指定路径
+     *
+     * @param context
+     * @param bitmap   要保存的图片
+     * @param fileName 自定义图片名称  getString(R.string.app_name) + "" + System.currentTimeMillis()+".png"
+     * @return true 成功 false失败
+     */
+    public static boolean saveImageToGallery(Context context, Bitmap bitmap, String fileName) {
+        // 保存图片至指定路径
+//        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dayixl";
+        //系统相册目录
+        String storePath = Environment.getExternalStorageDirectory()
+                + File.separator + Environment.DIRECTORY_DCIM
+                + File.separator + "Camera" + File.separator;
+        File appDir = new File(storePath);
+        if (!appDir.exists()) {
+            appDir.mkdir();
+        }
+        File file = new File(appDir, fileName);
+        try {
+            FileOutputStream fos = new FileOutputStream(file);
+            //通过io流的方式来压缩保存图片(80代表压缩20%)
+            boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
+            fos.flush();
+            fos.close();
+
+            //发送广播通知系统图库刷新数据
+            Uri uri = Uri.fromFile(file);
+            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
+            LogUtil.Companion.i("保存图片成功");
+            return isSuccess;
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return false;
+    }
+}

+ 45 - 0
baselib/src/main/java/com/yingyangfly/baselib/utils/img/TransformationUtils.java

@@ -0,0 +1,45 @@
+package com.yingyangfly.baselib.utils.img;
+
+import android.graphics.Bitmap;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+
+import com.bumptech.glide.request.target.ImageViewTarget;
+
+/**
+ * ===========================================
+ * 版    本:1.0
+ * 描    述:设置图片等比缩放
+ * <p>glide处理图片.</p>
+ * ===========================================
+ */
+public class TransformationUtils extends ImageViewTarget<Bitmap> {
+
+    private ImageView target;
+
+    public TransformationUtils(ImageView target) {
+        super(target);
+        this.target = target;
+    }
+
+    @Override
+    protected void setResource(Bitmap resource) {
+        view.setImageBitmap(resource);
+
+        //获取原图的宽高
+        int width = resource.getWidth();
+        int height = resource.getHeight();
+
+        //获取imageView的宽
+        int imageViewWidth = target.getWidth();
+
+        //计算缩放比例
+        float sy = (float) (imageViewWidth * 0.1) / (float) (width * 0.1);
+
+        //计算图片等比例放大后的高
+        int imageViewHeight = (int) (height * sy);
+        ViewGroup.LayoutParams params = target.getLayoutParams();
+        params.height = imageViewHeight;
+        target.setLayoutParams(params);
+    }
+}

+ 1 - 1
push/src/main/java/com/yingyang/push/activity/MessgeListActivity.kt

@@ -118,7 +118,7 @@ class MessgeListActivity : BaseMVVMActivity<ActivityMessgeListBinding, PushViewM
             it.toast()
         }, success = {
             if (it.isNullOrEmpty().not()) {
-                pushList.addAll(it)
+//                pushList.addAll(it)
             }
             pushListAdapter.setData(pushList)
         })

+ 11 - 0
workbenches/src/main/java/com/yingyang/workbenches/adapter/FreeTrainAdapter.kt

@@ -6,6 +6,9 @@ import com.yingyang.workbenches.databinding.ItemFreeTrainingBinding
 import com.yingyang.workbenches.entity.Record
 import com.yingyangfly.baselib.adapter.BaseDataBindingAdapter
 import com.yingyangfly.baselib.ext.setOnSingleClickListener
+import com.yingyangfly.baselib.ext.show
+import com.yingyangfly.baselib.utils.AppUtil
+import com.yingyangfly.baselib.utils.img.ImgUtil
 
 /**
  * 自由训练列表adapter
@@ -15,6 +18,12 @@ class FreeTrainAdapter(override val layoutId: Int = R.layout.item_free_training)
 
     var onGameClickListener: ((bean: Record) -> Unit)? = null
 
+    var method = ""
+
+    fun setType(type: String) {
+        method = type
+    }
+
     @SuppressLint("NotifyDataSetChanged")
     override fun onBindViewHolder(
         binding: ItemFreeTrainingBinding,
@@ -22,6 +31,8 @@ class FreeTrainAdapter(override val layoutId: Int = R.layout.item_free_training)
         position: Int
     ) {
         binding.data = item
+        ImgUtil.loadImg(AppUtil.getContext(), item.gameCoverImage, binding.gameImage)
+        binding.tvType.show(method.isNullOrEmpty())
         binding.gameLayout.setOnSingleClickListener {
             onGameClickListener?.invoke(item)
         }

+ 2 - 0
workbenches/src/main/java/com/yingyang/workbenches/freetraining/FreeTrainActivity.kt

@@ -46,11 +46,13 @@ class FreeTrainActivity : BaseMVVMActivity<ActivityFreeTrainBinding, FreeTrainVi
             freeTrainTypeAdapter.onFreeTrainTypeClickListener = { bean ->
                 binding.tvTitle.text = bean.dictLabel
                 type = bean.dictValue
+                freeTrainListAdapter.setType(type)
                 loadData()
             }
             //自由训练游戏列表
             rvTrain.layoutManager = GridLayoutManager(this@FreeTrainActivity, 4)
             rvTrain.adapter = freeTrainListAdapter
+            freeTrainListAdapter.setType(type)
             freeTrainListAdapter.onGameClickListener = { bean ->
                 val str =
                     bean.gameUrl + "?gameCode=" + bean.gameCode + "&gameLevel=" + bean.currentLevel +

+ 7 - 14
workbenches/src/main/res/layout/item_free_training.xml

@@ -26,20 +26,21 @@
                 android:id="@+id/gameImage"
                 android:layout_width="match_parent"
                 android:layout_height="@dimen/divider_200px"
-                app:isCircle="@{false}"
+                android:scaleType="centerCrop"
                 app:layout_constraintLeft_toLeftOf="parent"
                 app:layout_constraintRight_toRightOf="parent"
-                app:layout_constraintTop_toTopOf="parent"
-                app:loadHeadImg="@{data.gameCoverImage}" />
+                app:layout_constraintTop_toTopOf="parent" />
 
             <TextView
+                android:id="@+id/tvType"
                 android:layout_width="@dimen/divider_108px"
                 android:layout_height="@dimen/divider_46px"
                 android:background="@mipmap/bg_game_type"
                 android:gravity="center"
                 android:text="@{data.gameType}"
                 android:textColor="@android:color/white"
-                android:textSize="@dimen/divider_14px"
+                android:textSize="@dimen/divider_16px"
+                android:textStyle="bold"
                 app:layout_constraintRight_toRightOf="parent"
                 app:layout_constraintTop_toTopOf="parent" />
 
@@ -55,22 +56,14 @@
                 app:layout_constraintTop_toBottomOf="@+id/gameImage">
 
                 <TextView
-                    android:layout_width="wrap_content"
+                    android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     android:gravity="center"
                     android:text="@{data.gameName}"
                     android:textColor="@android:color/white"
-                    android:textSize="@dimen/divider_20px"
+                    android:textSize="@dimen/divider_22px"
                     android:textStyle="bold" />
 
-                <TextView
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    android:layout_marginStart="@dimen/divider_16px"
-                    android:background="@drawable/bg_workbenches_level"
-                    android:text='@{data.currentLevel+"-"+data.totalNum}'
-                    android:textColor="@android:color/white"
-                    android:textSize="@dimen/divider_14px" />
 
             </LinearLayout>
         </androidx.constraintlayout.widget.ConstraintLayout>