From 7162409878156181b6729cb92c686e717b253b81 Mon Sep 17 00:00:00 2001 From: David Luhmer Date: Sun, 23 Sep 2018 19:18:57 +0200 Subject: [PATCH] fix center crop issue / improve layout --- .../adapter/ViewHolder.java | 15 ++- .../helper/SquareRoundedBitmapDisplayer.java | 91 +++++++++++++++++++ .../src/main/res/drawable/feed_icon.xml | 9 ++ .../main/res/drawable/rounded_rectangle.xml | 10 ++ .../subscription_detail_list_item_simple.xml | 4 +- 5 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/SquareRoundedBitmapDisplayer.java create mode 100644 News-Android-App/src/main/res/drawable/feed_icon.xml create mode 100644 News-Android-App/src/main/res/drawable/rounded_rectangle.xml diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/adapter/ViewHolder.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/adapter/ViewHolder.java index 00086111..f640c586 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/adapter/ViewHolder.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/adapter/ViewHolder.java @@ -23,7 +23,6 @@ import android.widget.TextView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; -import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer; import org.greenrobot.eventbus.Subscribe; @@ -40,6 +39,7 @@ import de.luhmer.owncloudnewsreader.database.model.RssItem; import de.luhmer.owncloudnewsreader.helper.ColorHelper; import de.luhmer.owncloudnewsreader.helper.FavIconHandler; import de.luhmer.owncloudnewsreader.helper.ImageHandler; +import de.luhmer.owncloudnewsreader.helper.SquareRoundedBitmapDisplayer; import de.luhmer.owncloudnewsreader.services.PodcastDownloadService; import static android.view.View.GONE; @@ -131,12 +131,11 @@ public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickL itemView.setOnClickListener(this); itemView.setOnLongClickListener(this); - displayImageOptionsThumbnail = new DisplayImageOptions.Builder() - .displayer(new RoundedBitmapDisplayer(30)) - //.showImageOnLoading(placeHolder) - //.showImageForEmptyUri(placeHolder) - //.showImageOnFail(placeHolder) + .displayer(new SquareRoundedBitmapDisplayer(30)) + .showImageOnLoading(R.drawable.feed_icon) + .showImageForEmptyUri(R.drawable.feed_icon) + .showImageOnFail(R.drawable.feed_icon) .cacheOnDisk(true) .cacheInMemory(true) .build(); @@ -294,8 +293,8 @@ public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickL // Show Podcast Icon if no thumbnail is available but it is a podcast (otherwise the podcast button will go missing) if (DatabaseConnectionOrm.ALLOWED_PODCASTS_TYPES.contains(rssItem.getEnclosureMime())) { imgViewThumbnail.setVisibility(View.VISIBLE); - imgViewThumbnail.setColorFilter(Color.parseColor("#d8d8d8")); - imgViewThumbnail.setImageDrawable(ContextCompat.getDrawable(itemView.getContext(), R.drawable.default_feed_icon_dark)); + //imgViewThumbnail.setColorFilter(Color.parseColor("#d8d8d8")); + imgViewThumbnail.setImageDrawable(ContextCompat.getDrawable(itemView.getContext(), R.drawable.feed_icon)); } else { imgViewThumbnail.setVisibility(GONE); } diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/SquareRoundedBitmapDisplayer.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/SquareRoundedBitmapDisplayer.java new file mode 100644 index 00000000..9b2e7f05 --- /dev/null +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/SquareRoundedBitmapDisplayer.java @@ -0,0 +1,91 @@ +package de.luhmer.owncloudnewsreader.helper; + +import android.graphics.Bitmap; +import android.graphics.BitmapShader; +import android.graphics.Canvas; +import android.graphics.ColorFilter; +import android.graphics.Matrix; +import android.graphics.Paint; +import android.graphics.PixelFormat; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.Shader; +import android.graphics.drawable.Drawable; + +import com.nostra13.universalimageloader.core.assist.LoadedFrom; +import com.nostra13.universalimageloader.core.display.BitmapDisplayer; +import com.nostra13.universalimageloader.core.imageaware.ImageAware; +import com.nostra13.universalimageloader.core.imageaware.ImageViewAware; + +public class SquareRoundedBitmapDisplayer implements BitmapDisplayer { + + protected final int cornerRadius; + protected final int margin; + + public SquareRoundedBitmapDisplayer(int cornerRadiusPixels) { + this(cornerRadiusPixels, 0); + } + + public SquareRoundedBitmapDisplayer(int cornerRadiusPixels, int marginPixels) { + this.cornerRadius = cornerRadiusPixels; + this.margin = marginPixels; + } + + @Override + public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) { + if (!(imageAware instanceof ImageViewAware)) { + throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected."); + } + + //imageAware.setImageDrawable(new RoundedDrawable(bitmap, cornerRadius, margin)); + imageAware.setImageDrawable(new RoundedDrawable(bitmap, cornerRadius)); + } + + public static class RoundedDrawable extends Drawable { + + private Bitmap bitmap; + private Paint maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private int side; + private float cornerRadius; + private RectF dst; + + public RoundedDrawable(Bitmap wrappedBitmap, int cornerRadius) { + bitmap = wrappedBitmap; + borderPaint.setStyle(Paint.Style.STROKE); + borderPaint.setStrokeWidth(16); + borderPaint.setColor(0xcc220088); + side = Math.min(bitmap.getWidth(), bitmap.getHeight()); + this.cornerRadius = cornerRadius; + } + + @Override + protected void onBoundsChange(Rect bounds) { + Matrix matrix = new Matrix(); + RectF src = new RectF(0, 0, side, side); + src.offset((bitmap.getWidth() - side) / 2f, (bitmap.getHeight() - side) / 2f); + dst = new RectF(bounds); + dst.inset(borderPaint.getStrokeWidth() / 4f, borderPaint.getStrokeWidth() / 4f); // Adjust the factor here to fit into the bounds + matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER); + + Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); + shader.setLocalMatrix(matrix); + maskPaint.setShader(shader); + matrix.mapRect(src); + //radius = src.width() / 2f; + } + + @Override + public void draw(Canvas canvas) { + //Rect b = getBounds(); + //canvas.drawCircle(b.exactCenterX(), b.exactCenterY(), radius, maskPaint); + //canvas.drawCircle(b.exactCenterX(), b.exactCenterY(), radius + borderPaint.getStrokeWidth() / 2, borderPaint); + canvas.drawRoundRect(dst, cornerRadius, cornerRadius, maskPaint); + } + + @Override public void setAlpha(int alpha) {} + @Override public void setColorFilter(ColorFilter cf) {} + @Override public int getOpacity() {return PixelFormat.TRANSLUCENT;} + + } +} diff --git a/News-Android-App/src/main/res/drawable/feed_icon.xml b/News-Android-App/src/main/res/drawable/feed_icon.xml new file mode 100644 index 00000000..e4972776 --- /dev/null +++ b/News-Android-App/src/main/res/drawable/feed_icon.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/News-Android-App/src/main/res/drawable/rounded_rectangle.xml b/News-Android-App/src/main/res/drawable/rounded_rectangle.xml new file mode 100644 index 00000000..0d0a6593 --- /dev/null +++ b/News-Android-App/src/main/res/drawable/rounded_rectangle.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/News-Android-App/src/main/res/layout/subscription_detail_list_item_simple.xml b/News-Android-App/src/main/res/layout/subscription_detail_list_item_simple.xml index a3f424b2..f9aa74b6 100644 --- a/News-Android-App/src/main/res/layout/subscription_detail_list_item_simple.xml +++ b/News-Android-App/src/main/res/layout/subscription_detail_list_item_simple.xml @@ -49,14 +49,14 @@ android:layout_marginEnd="16dp" android:contentDescription="@string/img_view_thumbnail" android:scaleType="centerCrop" - tools:src="@drawable/default_feed_icon_dark" /> + tools:src="@drawable/feed_icon" /> + android:layout_below="@id/summary"/>