Procházet zdrojové kódy

弹窗广告无文本内容时,图片下面采用圆角实现,效果更好

zengjiebin před 7 roky
rodič
revize
b5a80df75d

+ 10 - 5
app/src/main/java/com/sheep/gamegroup/util/ViewUtil.java

@@ -7,14 +7,13 @@ import android.content.DialogInterface;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.Color;
+import android.graphics.drawable.Drawable;
 import android.media.MediaPlayer;
 import android.net.Uri;
 import android.os.Build;
 import android.provider.MediaStore;
-import android.support.v4.content.ContextCompat;
 import android.support.v7.app.AlertDialog;
 import android.support.v7.widget.GridLayoutManager;
-import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.text.Html;
 import android.text.TextUtils;
@@ -31,12 +30,14 @@ import android.widget.TextView;
 import android.widget.VideoView;
 
 import com.bumptech.glide.Glide;
+import com.bumptech.glide.RequestBuilder;
 import com.bumptech.glide.request.RequestOptions;
 import com.sheep.gamegroup.model.entity.Advertising;
 import com.sheep.gamegroup.model.entity.Container;
 import com.sheep.gamegroup.model.entity.DialogConfig;
 import com.sheep.gamegroup.model.entity.GameAccountEntity;
 import com.sheep.gamegroup.model.entity.PayEntity;
+import com.sheep.gamegroup.util.glide.RoundedCornersTransformation;
 import com.sheep.gamegroup.view.activity.SplashAct;
 import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
 import com.sheep.gamegroup.view.dialog.DialogAddAccount;
@@ -45,7 +46,6 @@ import com.sheep.gamegroup.view.dialog.DialogOrienteeringDetails;
 import com.sheep.gamegroup.view.dialog.DialogPayAccount;
 import com.sheep.gamegroup.view.dialog.DialogPayGame;
 import com.sheep.gamegroup.view.dialog.DialogShare;
-import com.sheep.jiuyan.samllsheep.BuildConfig;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
 import com.sheep.jiuyan.samllsheep.utils.G;
@@ -573,14 +573,19 @@ public class ViewUtil {
             ImageView ad_iv = view.findViewById(R.id.ad_iv);
             VideoView ad_video_view = view.findViewById(R.id.ad_video_view);
             TextView ad_msg = view.findViewById(R.id.ad_msg);
-            final TextView ad_skip = view.findViewById(R.id.ad_skip);
+//            final TextView ad_skip = view.findViewById(R.id.ad_skip);
             View dialog_rl = view.findViewById(R.id.dialog_rl);
 
 //            SurfaceHolder holder = ad_video_view.getHolder();
 //            holder.setFixedSize(ad_video_view.getWidth(), ad_video_view.getWidth()*3/4);
 //            ad_video_view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,G.WIDTH*4/7));
             if (advertising.isImage()) {
-                Glide.with(activity).load(advertising.getDisplay_src()).into(ad_iv);
+                RequestBuilder<Drawable> builder = Glide.with(activity).load(advertising.getDisplay_src());
+                if(TextUtils.isEmpty(advertising.getDesc())) {//没有标题时 去掉下面的边距 并给图片左下理右下添加图角
+                    builder.apply(new RequestOptions().transform(new RoundedCornersTransformation(activity.getResources().getDimensionPixelSize(R.dimen.content_padding_36), 0, RoundedCornersTransformation.CornerType.BOTTOM)));
+                    dialog_center_ll.setPadding(0,activity.getResources().getDimensionPixelSize(R.dimen.content_padding_54),0, 0);
+                }
+                builder.into(ad_iv);
                 ad_video_view.setVisibility(View.GONE);
             } else {
                 Uri uri = Uri.parse(advertising.getDisplay_src());

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

@@ -1,125 +0,0 @@
-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) {
-
-    }
-}

+ 255 - 0
app/src/main/java/com/sheep/gamegroup/util/glide/RoundedCornersTransformation.java

@@ -0,0 +1,255 @@
+package com.sheep.gamegroup.util.glide;
+
+/**
+ * Copyright (C) 2018 Wasabeef
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapShader;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.RectF;
+import android.graphics.Shader;
+import android.support.annotation.NonNull;
+import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
+import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
+
+import java.security.MessageDigest;
+
+public class RoundedCornersTransformation extends BitmapTransformation {
+
+    private static final int VERSION = 1;
+    private static final String ID = "jp.wasabeef.glide.transformations.RoundedCornersTransformation." + VERSION;
+
+
+    public enum CornerType {
+        ALL,
+        TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
+        TOP, BOTTOM, LEFT, RIGHT,
+        OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
+        DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
+    }
+
+    private int radius;
+    private int diameter;
+    private int margin;
+    private CornerType cornerType;
+
+    public RoundedCornersTransformation(int radius, int margin) {
+        this(radius, margin, CornerType.ALL);
+    }
+
+    public RoundedCornersTransformation(int radius, int margin, CornerType cornerType) {
+        this.radius = radius;
+        this.diameter = this.radius * 2;
+        this.margin = margin;
+        this.cornerType = cornerType;
+    }
+
+    @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, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
+        drawRoundRect(canvas, paint, width, height);
+        return bitmap;
+    }
+
+    private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {
+        float right = width - margin;
+        float bottom = height - margin;
+
+        switch (cornerType) {
+            case ALL:
+                canvas.drawRoundRect(new RectF(margin, margin, right, bottom), radius, radius, paint);
+                break;
+            case TOP_LEFT:
+                drawTopLeftRoundRect(canvas, paint, right, bottom);
+                break;
+            case TOP_RIGHT:
+                drawTopRightRoundRect(canvas, paint, right, bottom);
+                break;
+            case BOTTOM_LEFT:
+                drawBottomLeftRoundRect(canvas, paint, right, bottom);
+                break;
+            case BOTTOM_RIGHT:
+                drawBottomRightRoundRect(canvas, paint, right, bottom);
+                break;
+            case TOP:
+                drawTopRoundRect(canvas, paint, right, bottom);
+                break;
+            case BOTTOM:
+                drawBottomRoundRect(canvas, paint, right, bottom);
+                break;
+            case LEFT:
+                drawLeftRoundRect(canvas, paint, right, bottom);
+                break;
+            case RIGHT:
+                drawRightRoundRect(canvas, paint, right, bottom);
+                break;
+            case OTHER_TOP_LEFT:
+                drawOtherTopLeftRoundRect(canvas, paint, right, bottom);
+                break;
+            case OTHER_TOP_RIGHT:
+                drawOtherTopRightRoundRect(canvas, paint, right, bottom);
+                break;
+            case OTHER_BOTTOM_LEFT:
+                drawOtherBottomLeftRoundRect(canvas, paint, right, bottom);
+                break;
+            case OTHER_BOTTOM_RIGHT:
+                drawOtherBottomRightRoundRect(canvas, paint, right, bottom);
+                break;
+            case DIAGONAL_FROM_TOP_LEFT:
+                drawDiagonalFromTopLeftRoundRect(canvas, paint, right, bottom);
+                break;
+            case DIAGONAL_FROM_TOP_RIGHT:
+                drawDiagonalFromTopRightRoundRect(canvas, paint, right, bottom);
+                break;
+            default:
+                canvas.drawRoundRect(new RectF(margin, margin, right, bottom), radius, radius, paint);
+                break;
+        }
+    }
+
+    private void drawTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, margin + diameter), radius,
+                radius, paint);
+        canvas.drawRect(new RectF(margin, margin + radius, margin + radius, bottom), paint);
+        canvas.drawRect(new RectF(margin + radius, margin, right, bottom), paint);
+    }
+
+    private void drawTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(right - diameter, margin, right, margin + diameter), radius,
+                radius, paint);
+        canvas.drawRect(new RectF(margin, margin, right - radius, bottom), paint);
+        canvas.drawRect(new RectF(right - radius, margin + radius, right, bottom), paint);
+    }
+
+    private void drawBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(margin, bottom - diameter, margin + diameter, bottom), radius,
+                radius, paint);
+        canvas.drawRect(new RectF(margin, margin, margin + diameter, bottom - radius), paint);
+        canvas.drawRect(new RectF(margin + radius, margin, right, bottom), paint);
+    }
+
+    private void drawBottomRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(right - diameter, bottom - diameter, right, bottom), radius,
+                radius, paint);
+        canvas.drawRect(new RectF(margin, margin, right - radius, bottom), paint);
+        canvas.drawRect(new RectF(right - radius, margin, right, bottom - radius), paint);
+    }
+
+    private void drawTopRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(margin, margin, right, margin + diameter), radius, radius,
+                paint);
+        canvas.drawRect(new RectF(margin, margin + radius, right, bottom), paint);
+    }
+
+    private void drawBottomRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(margin, bottom - diameter, right, bottom), radius, radius,
+                paint);
+        canvas.drawRect(new RectF(margin, margin, right, bottom - radius), paint);
+    }
+
+    private void drawLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, bottom), radius, radius,
+                paint);
+        canvas.drawRect(new RectF(margin + radius, margin, right, bottom), paint);
+    }
+
+    private void drawRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(right - diameter, margin, right, bottom), radius, radius, paint);
+        canvas.drawRect(new RectF(margin, margin, right - radius, bottom), paint);
+    }
+
+    private void drawOtherTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(margin, bottom - diameter, right, bottom), radius, radius,
+                paint);
+        canvas.drawRoundRect(new RectF(right - diameter, margin, right, bottom), radius, radius, paint);
+        canvas.drawRect(new RectF(margin, margin, right - radius, bottom - radius), paint);
+    }
+
+    private void drawOtherTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, bottom), radius, radius,
+                paint);
+        canvas.drawRoundRect(new RectF(margin, bottom - diameter, right, bottom), radius, radius,
+                paint);
+        canvas.drawRect(new RectF(margin + radius, margin, right, bottom - radius), paint);
+    }
+
+    private void drawOtherBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
+        canvas.drawRoundRect(new RectF(margin, margin, right, margin + diameter), radius, radius,
+                paint);
+        canvas.drawRoundRect(new RectF(right - diameter, margin, right, bottom), radius, radius, paint);
+        canvas.drawRect(new RectF(margin, margin + radius, right - radius, bottom), paint);
+    }
+
+    private void drawOtherBottomRightRoundRect(Canvas canvas, Paint paint, float right,
+                                               float bottom) {
+        canvas.drawRoundRect(new RectF(margin, margin, right, margin + diameter), radius, radius,
+                paint);
+        canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, bottom), radius, radius,
+                paint);
+        canvas.drawRect(new RectF(margin + radius, margin + radius, right, bottom), paint);
+    }
+
+    private void drawDiagonalFromTopLeftRoundRect(Canvas canvas, Paint paint, float right,
+                                                  float bottom) {
+        canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, margin + diameter), radius,
+                radius, paint);
+        canvas.drawRoundRect(new RectF(right - diameter, bottom - diameter, right, bottom), radius,
+                radius, paint);
+        canvas.drawRect(new RectF(margin, margin + radius, right - diameter, bottom), paint);
+        canvas.drawRect(new RectF(margin + diameter, margin, right, bottom - radius), paint);
+    }
+
+    private void drawDiagonalFromTopRightRoundRect(Canvas canvas, Paint paint, float right,
+                                                   float bottom) {
+        canvas.drawRoundRect(new RectF(right - diameter, margin, right, margin + diameter), radius,
+                radius, paint);
+        canvas.drawRoundRect(new RectF(margin, bottom - diameter, margin + diameter, bottom), radius,
+                radius, paint);
+        canvas.drawRect(new RectF(margin, margin, right - radius, bottom - radius), paint);
+        canvas.drawRect(new RectF(margin + radius, margin + radius, right, bottom), paint);
+    }
+
+    @Override public String toString() {
+        return "RoundedTransformation(radius=" + radius + ", margin=" + margin + ", diameter="
+                + diameter + ", cornerType=" + cornerType.name() + ")";
+    }
+
+    @Override public boolean equals(Object o) {
+        return o instanceof RoundedCornersTransformation &&
+                ((RoundedCornersTransformation) o).radius == radius &&
+                ((RoundedCornersTransformation) o).diameter == diameter &&
+                ((RoundedCornersTransformation) o).margin == margin &&
+                ((RoundedCornersTransformation) o).cornerType == cornerType;
+    }
+
+    @Override public int hashCode() {
+        return ID.hashCode() + radius * 10000 + diameter * 1000 + margin * 100 + cornerType.ordinal() * 10;
+    }
+
+    @Override public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
+        messageDigest.update((ID + radius + diameter + margin + cornerType).getBytes(CHARSET));
+    }
+}

+ 2 - 2
app/src/main/res/layout/dialog_half_screen_ad.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<merge xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">
@@ -53,4 +53,4 @@
         android:textColor="#939393"
         android:textSize="13sp" />
 
-</LinearLayout>
+</merge>

+ 1 - 0
app/src/main/res/values/common.xml

@@ -58,6 +58,7 @@
     <dimen name="content_padding_22">22dp</dimen>
     <dimen name="content_padding_30">30dp</dimen>
     <dimen name="content_padding_35">35dp</dimen>
+    <dimen name="content_padding_36">36dp</dimen>
     <dimen name="content_padding_40">40dp</dimen>
     <dimen name="content_padding_54">54dp</dimen>
     <dimen name="content_padding_65">65dp</dimen>