Skip to content

Commit

Permalink
+ added ability to produce likes programmatically
Browse files Browse the repository at this point in the history
* improved memory consumption: reusing existing object
* updated readme
  • Loading branch information
Iojjj committed May 24, 2016
1 parent 718aee0 commit c72106b
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 54 deletions.
56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Likes Layout

LikesLayout is an implementation of layout that can draw likes similar to [Periscope app](https://play.google.com/store/apps/details?id=tv.periscope.android).

![Likes Layout Demo](/images/demo.gif)


Expand All @@ -12,7 +15,7 @@ dependencies {
}
```

There are three implementations of LikesLayout: **LikesFrameLayout**, **LikesLinearLayout**, **LikesRelativeLayout**.
There are three implementations of `LikesLayout` interface: **LikesFrameLayout**, **LikesLinearLayout**, **LikesRelativeLayout**. You can add them to your view via XML or in Java code.

```XML
<ua.vlasov.likeslayout.LikesLinearLayout
Expand Down Expand Up @@ -46,6 +49,46 @@ There are three implementations of LikesLayout: **LikesFrameLayout**, **LikesLin
</ua.vlasov.likeslayout.LikesLinearLayout>
```

or

```JAVA
// create new LikesLayout
LikesLinearLayout likesLinearLayout = new LikesLinearLayout(getContext());
likesLinearLayout.setId(R.id.likes_layout);
// set layout height
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
(int) (getResources().getDisplayMetrics().density * 250));
likesLinearLayout.setLayoutParams(params);
likesLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
// make sure your buttons located at the bottom of LikesLayout
likesLinearLayout.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
likesLinearLayout.setPadding(0, 0, 0, (int) (getResources().getDisplayMetrics().density * 16));
// get attributes object that will store default values
likesLinearLayout.getAttributes().setAnimationDuration(1200);
likesLinearLayout.getAttributes().setTintMode(LikesAttributes.TINT_MODE_ON_SUCCESSIVELY);
likesLinearLayout.getAttributes().setTintColors(colors);

// it's time to add a button
ImageButton button = new ImageButton(getContext(), null, R.style.LikeButton_Grade);
button.setId(R.id.btn_grade);
button.setImageResource(R.drawable.ic_grade);
DrawableCompat.setTint(button.getDrawable(), ContextCompat.getColor(getContext(), R.color.colorAccent));
// you can create layout params for your button using LikesLayout.newLayoutParamsBuilder() method
// then using a builder you can set all necessary attributes values
final ViewGroup.LayoutParams params = likesLinearLayout
.newLayoutParamsBuilder(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_grade_normal))
.setAnimationDuration(3000)
.setProduceInterval(500)
.setPositionAnimatorFactory(new CustomPositionAnimatorFactory2())
// don't forget to enable likes mode :)
.setLikesMode(LikesAttributes.LIKES_MODE_ENABLED)
.build();
// set parameters and add button to LikesLayout
button.setLayoutParams(params);
likesLinearLayout.addView(button);
```

There is the list of available attributes:

| Attribute name | ATLL | CBOBC | Description | Default value |
Expand All @@ -61,7 +104,16 @@ There is the list of available attributes:
| likes_tintMode | yes | yes | Switch that allows to enable/disable tinting drawables. | not_set |
| likes_tintColors | yes | yes | Array of colors used for tinting drawables. If array is empty, `likes_tintMode` will be considered as `off`.| null |

###### Restrictions:
#### Produce likes programmatically
For this case you can use `LikesLayout.produceLikes(...)` methods, for example:

```JAVA
mLikesLayout.produceLikes(mBtnFavorite, 3, TimeUnit.SECONDS);
```

Make sure that child passed as an argument (`mBtnFavorite`) was added to this `mLikesLayout` (via XML or Java code). Otherwise you will get `IllegalArgumentException`.

## Restrictions
* If you will not set `likes_mode` or set it to `disabled`, this view will not draw any likes on touch event.
* All attributes applicable to LikesLayout will be used as default values for child with `likes_mode` set to `enabled`.
* Custom drawable width and height will set exact size ignoring aspect ratio.
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/ua/vlasov/likes_layout/FromXmlFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;

/**
* Fragment that inflates LikesLayout from XML.
*/
public class FromXmlFragment extends BaseFragment {

@BindView(R.id.btn_favorite)
ImageButton mBtnFavorite;

@BindView(R.id.btn_grade)
ImageButton mBtnGrade;

@BindView(R.id.btn_stars)
ImageButton mBtnStars;

public static FromXmlFragment newInstance() {
Bundle args = new Bundle();
FromXmlFragment fragment = new FromXmlFragment();
Expand All @@ -24,4 +39,10 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
return inflater.inflate(R.layout.fragment_main, container, false);
}

@OnClick(R.id.btn_produce)
void onProduceLikesClicked() {
mLikesLayout.produceLikes(mBtnFavorite, 3, TimeUnit.SECONDS);
mLikesLayout.produceLikes(mBtnGrade, 3, TimeUnit.SECONDS);
mLikesLayout.produceLikes(mBtnStars, 3, TimeUnit.SECONDS);
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
android:padding="16dp"
/>

<Button
android:id="@+id/btn_produce"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Produce likes for 3 seconds"
android:layout_below="@+id/status"
android:layout_marginTop="16dp"
/>

<ua.vlasov.likeslayout.LikesLinearLayout
android:id="@+id/likes_layout"
android:layout_width="match_parent"
Expand Down
Binary file modified images/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c72106b

Please sign in to comment.