소스 검색

review代码:
为标题增加左右间距,防止左右超过边界,并且标题过长时自动在末尾添加省略号

zengjiebin 7 년 전
부모
커밋
05a8f7b231
2개의 변경된 파일131개의 추가작업 그리고 2개의 파일을 삭제
  1. 125 0
      app/src/main/java/com/sheep/gamegroup/util/glide/CornerTransform.java
  2. 6 2
      app/src/main/res/layout/title.xml

+ 125 - 0
app/src/main/java/com/sheep/gamegroup/util/glide/CornerTransform.java

@@ -0,0 +1,125 @@
+package com.sheep.gamegroup.util.glide;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.BitmapShader;
+import android.graphics.Canvas;
+import android.graphics.Matrix;
+import android.graphics.Paint;
+import android.graphics.RectF;
+import android.graphics.Shader;
+import android.support.annotation.NonNull;
+
+import com.bumptech.glide.Glide;
+import com.bumptech.glide.load.engine.Resource;
+import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
+import com.bumptech.glide.load.resource.bitmap.BitmapResource;
+import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
+
+import java.security.MessageDigest;
+
+
+/**
+ * Created by realicing on 2018/8/8.
+ * realicing@sina.com
+ */
+public class CornerTransform extends BitmapTransformation {
+
+
+    private float radius;
+
+    private boolean exceptLeftTop, exceptRightTop, exceptLeftBottom, exceptRightBotoom;
+
+    /**
+     * 除了那几个角不需要圆角的
+     *
+     * @param leftTop
+     * @param rightTop
+     * @param leftBottom
+     * @param rightBottom
+     */
+    public void setExceptCorner(boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom) {
+        this.exceptLeftTop = leftTop;
+        this.exceptRightTop = rightTop;
+        this.exceptLeftBottom = leftBottom;
+        this.exceptRightBotoom = rightBottom;
+    }
+
+    public CornerTransform(Context context, float radius) {
+        this.radius = radius;
+    }
+
+
+    @Override
+    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap source, int outWidth, int outHeight) {
+        int finalWidth, finalHeight;
+        float ratio; //输出目标的宽高或高宽比例
+        if (outWidth > outHeight) { //输出宽度>输出高度,求高宽比
+            ratio = (float) outHeight / (float) outWidth;
+            finalWidth = source.getWidth();
+            finalHeight = (int) ((float) source.getWidth() * ratio); //固定原图宽度,求最终高度
+            if (finalHeight > source.getHeight()) { //求出的最终高度>原图高度,求宽高比
+                ratio = (float) outWidth / (float) outHeight;
+                finalHeight = source.getHeight();
+                finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
+            }
+        } else if (outWidth < outHeight) { //输出宽度 < 输出高度,求宽高比
+            ratio = (float) outWidth / (float) outHeight;
+            finalHeight = source.getHeight();
+            finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
+            if (finalWidth > source.getWidth()) { //求出的最终宽度 > 原图宽度,求高宽比
+                ratio = (float) outHeight / (float) outWidth;
+                finalWidth = source.getWidth();
+                finalHeight = (int) ((float) source.getWidth() * ratio);
+            }
+        } else { //输出宽度=输出高度
+            finalHeight = source.getHeight();
+            finalWidth = finalHeight;
+        }
+
+        //修正圆角
+        this.radius *= (float) finalHeight / (float) outHeight;
+        Bitmap outBitmap = pool.get(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
+
+        Canvas canvas = new Canvas(outBitmap);
+        Paint paint = new Paint();
+        //关联画笔绘制的原图bitmap
+        BitmapShader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
+        //计算中心位置,进行偏移
+        int width = (source.getWidth() - finalWidth) / 2;
+        int height = (source.getHeight() - finalHeight) / 2;
+        if (width != 0 || height != 0) {
+            Matrix matrix = new Matrix();
+            matrix.setTranslate((float) (-width), (float) (-height));
+            shader.setLocalMatrix(matrix);
+        }
+
+        paint.setShader(shader);
+        paint.setAntiAlias(true);
+        RectF rectF = new RectF(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight());
+        canvas.drawRoundRect(rectF, this.radius, this.radius, paint); //先绘制圆角矩形
+
+        if (exceptLeftTop) { //左上角不为圆角
+            canvas.drawRect(0, 0, radius, radius, paint);
+        }
+        if (exceptRightTop) {//右上角不为圆角
+            canvas.drawRect(canvas.getWidth() - radius, 0, radius, radius, paint);
+        }
+
+        if (exceptLeftBottom) {//左下角不为圆角
+            canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), paint);
+        }
+
+        if (exceptRightBotoom) {//右下角不为圆角
+            canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), paint);
+        }
+
+        return outBitmap;
+
+    }
+
+    @Override
+    public void updateDiskCacheKey(MessageDigest messageDigest) {
+
+    }
+}

+ 6 - 2
app/src/main/res/layout/title.xml

@@ -18,9 +18,13 @@
     <TextView
         android:id="@+id/txt_baseactivity_title"
         android:layout_width="wrap_content"
-        android:layout_height="match_parent"
+        android:layout_height="wrap_content"
         android:layout_centerInParent="true"
-        android:gravity="center"
+        android:layout_marginStart="?attr/actionBarSize"
+        android:layout_marginEnd="?attr/actionBarSize"
+        android:singleLine="true"
+        android:gravity="start"
+        android:ellipsize="end"
         android:text="@string/app_name"
         android:textColor="@color/black_text_deep"
         android:textSize="18sp"/>