Skip to content

Commit

Permalink
added values to attrs you can now set shape and corner radius from xml
Browse files Browse the repository at this point in the history
  • Loading branch information
martinseal committed Feb 22, 2020
1 parent 9758f01 commit b93323b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,7 +85,7 @@ class MultiImageView @JvmOverloads constructor (context: Context, attrs: Attribu
}

/**
* Get images count
* Set badge text
*/
fun setBadgeText() {

Expand All @@ -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()
}
}

Expand Down Expand Up @@ -157,6 +162,10 @@ class MultiImageView @JvmOverloads constructor (context: Context, attrs: Attribu
enum class Shape {
CIRCLE, RECTANGLE, NONE
}

inline fun <reified T : Enum<T>> TypedArray.getEnum(index: Int, default: T) =
getInt(index, -1).let { if (it >= 0) enumValues<T>()[it] else default
}
}

class MultiDrawable(private val bitmaps: ArrayList<Bitmap>) : Drawable() {
Expand Down
6 changes: 6 additions & 0 deletions multiimageview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<resources>

<declare-styleable name="MultiImageView">
<attr name="shape" format="enum">
<enum name="circle" value="0" />
<enum name="rectangle" value="1" />
<enum name="none" value="2" />
</attr>
<attr name="corner_radius" format="integer" />
<attr name="badge_color" format="color" />
<attr name="badge_location" format="enum">
<enum name="topRight" value="0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -87,10 +90,10 @@ private LinkedList<Integer> createRandomModelImageList(){

private LinkedList<MultiImageViewModel> createStructuredModelList(){
LinkedList<MultiImageViewModel> 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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions sample/src/main/res/layout/view_holder.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<com.stfalcon.multiimageview.MultiImageView
android:id="@+id/iv"
android:layout_width="100dp"
android:layout_height="100dp" />
android:layout_height="100dp"
app:shape="rectangle"
app:corner_radius="60" />

</LinearLayout>

0 comments on commit b93323b

Please sign in to comment.