From b93323b0b349c46ba6774b004fd5ce208075efdf Mon Sep 17 00:00:00 2001 From: martin seal Date: Sat, 22 Feb 2020 14:45:17 +0000 Subject: [PATCH] added values to attrs you can now set shape and corner radius from xml --- .../com/stfalcon/multiimageview/MultiImageView.kt | 11 ++++++++++- multiimageview/src/main/res/values/attrs.xml | 6 ++++++ .../multiimageview/sample/RecyclerViewExample.java | 9 +++++++-- .../sample/adapters/MultiImageViewAdapter.java | 2 +- .../adapters/view_holders/MyMultiImageViewHolder.java | 2 -- sample/src/main/res/layout/view_holder.xml | 8 ++++++-- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt b/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt index de57454..0b4dbed 100644 --- a/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt +++ b/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt @@ -17,6 +17,7 @@ package com.stfalcon.multiimageview import android.content.Context +import android.content.res.TypedArray import android.graphics.* import android.graphics.drawable.Drawable import android.os.Build @@ -84,7 +85,7 @@ class MultiImageView @JvmOverloads constructor (context: Context, attrs: Attribu } /** - * Get images count + * Set badge text */ fun setBadgeText() { @@ -97,12 +98,16 @@ class MultiImageView @JvmOverloads constructor (context: Context, attrs: Attribu init { attrs?.let { + bitmaps.clear() val typedArray = context.obtainStyledAttributes(it, R.styleable.MultiImageView) + shape = typedArray.getEnum(R.styleable.MultiImageView_shape, Shape.NONE) + rectCorners = typedArray.getInt(R.styleable.MultiImageView_corner_radius, 100) val badgeColor = typedArray.getColor(R.styleable.MultiImageView_badge_color, getThemeAccentColor(context)) val badgeTextColor = typedArray.getColor(R.styleable.MultiImageView_badge_text_color, android.R.attr.textColor) val badgeLocation = typedArray.getString(R.styleable.MultiImageView_badge_location) val badgeMinCount = typedArray.getInt(R.styleable.MultiImageView_badge_shown_from_count, 4) val badgeMaxCount = typedArray.getInt(R.styleable.MultiImageView_badge_shown_to_count, 9) + typedArray.recycle() } } @@ -157,6 +162,10 @@ class MultiImageView @JvmOverloads constructor (context: Context, attrs: Attribu enum class Shape { CIRCLE, RECTANGLE, NONE } + + inline fun > TypedArray.getEnum(index: Int, default: T) = + getInt(index, -1).let { if (it >= 0) enumValues()[it] else default + } } class MultiDrawable(private val bitmaps: ArrayList) : Drawable() { diff --git a/multiimageview/src/main/res/values/attrs.xml b/multiimageview/src/main/res/values/attrs.xml index 5ff90eb..4fe4ce5 100755 --- a/multiimageview/src/main/res/values/attrs.xml +++ b/multiimageview/src/main/res/values/attrs.xml @@ -2,6 +2,12 @@ + + + + + + diff --git a/sample/src/main/java/com/stfalcon/multiimageview/sample/RecyclerViewExample.java b/sample/src/main/java/com/stfalcon/multiimageview/sample/RecyclerViewExample.java index 3554d46..ac40f40 100644 --- a/sample/src/main/java/com/stfalcon/multiimageview/sample/RecyclerViewExample.java +++ b/sample/src/main/java/com/stfalcon/multiimageview/sample/RecyclerViewExample.java @@ -4,6 +4,7 @@ import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -20,6 +21,7 @@ import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import com.stfalcon.multiimageview.MultiImageView; import com.stfalcon.multiimageview.sample.adapters.MultiImageViewAdapter; import com.stfalcon.multiimageview.sample.decorator.MultiImageViewItemDecorator; import com.stfalcon.multiimageview.sample.glide.GlideApp; @@ -38,6 +40,7 @@ public class RecyclerViewExample extends Fragment implements ClickHelper { private MultiImageViewAdapter multiImageViewAdapter; private int[] avatars = new int[]{R.drawable.avatar1,R.drawable.avatar2,R.drawable.avatar3,R.drawable.avatar4}; private int[] structuredImageListCount = new int[]{1,2,3,4,1,2,3,4,1,2}; + private String TAG = "RecyclerViewExample"; @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -87,10 +90,10 @@ private LinkedList createRandomModelImageList(){ private LinkedList createStructuredModelList(){ LinkedList modelList = new LinkedList<>(); - for (int i = 0; i < 10; i++){ + for (int i = 0; i < 30; i++){ MultiImageViewModel model = new MultiImageViewModel(); model.setId("id-" + i); - model.setImages(createStructuredImageList(structuredImageListCount[i])); + model.setImages(createStructuredImageList(structuredImageListCount[i / 3])); modelList.add(model); } return modelList; @@ -141,6 +144,8 @@ private void navigateToPreview(MultiImageViewModel model, View v){ @Override public void click(View v, int position) { + MultiImageView multiImageView = v.findViewById(R.id.iv); + Log.d(TAG,"multiImageView bitmap count " + multiImageView.getBitmapCount()); navigateToPreview(multiImageViewAdapter.getItem(position), v); } } diff --git a/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/MultiImageViewAdapter.java b/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/MultiImageViewAdapter.java index f881db7..be75206 100644 --- a/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/MultiImageViewAdapter.java +++ b/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/MultiImageViewAdapter.java @@ -47,7 +47,7 @@ public MyMultiImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int @Override public void onBindViewHolder(@NonNull final MyMultiImageViewHolder holder, int position) { MultiImageViewModel model = multiImageViewModels.get(position); - Log.d(TAG,"image view bitmap count " + holder.multiImageView.getBitmapCount()); + holder.multiImageView.clear(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { holder.multiImageView.setTransitionName("transitionName-" + model.getId()); } diff --git a/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/view_holders/MyMultiImageViewHolder.java b/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/view_holders/MyMultiImageViewHolder.java index b8b7526..c92681b 100644 --- a/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/view_holders/MyMultiImageViewHolder.java +++ b/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/view_holders/MyMultiImageViewHolder.java @@ -17,8 +17,6 @@ public MyMultiImageViewHolder(@NonNull View itemView, final ClickHelper clickHel super(itemView); multiImageView = itemView.findViewById(R.id.iv); multiImageView.clear(); - multiImageView.setShape(MultiImageView.Shape.RECTANGLE); - multiImageView.setRectCorners(8); multiImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/sample/src/main/res/layout/view_holder.xml b/sample/src/main/res/layout/view_holder.xml index 49f4dc0..079cf81 100644 --- a/sample/src/main/res/layout/view_holder.xml +++ b/sample/src/main/res/layout/view_holder.xml @@ -1,11 +1,15 @@ + android:layout_height="100dp" + app:shape="rectangle" + app:corner_radius="60" /> \ No newline at end of file