Skip to content

Commit

Permalink
+ added animator factories
Browse files Browse the repository at this point in the history
+ added documentation

* updated demo. Added sample that constructs layout from code.
* refactored code
* updated readme
  • Loading branch information
Iojjj committed May 23, 2016
1 parent db08bed commit 938ef77
Show file tree
Hide file tree
Showing 33 changed files with 1,503 additions and 585 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ There are three implementations of LikesLayout: **LikesFrameLayout**, **LikesLin
app:likes_animationDuration="animationduration in milliseconds"
app:likes_tintMode="off|on_successively|on_random"
app:likes_tintColors="array resource id"
app:likes_drawableAnimator="cannonical class name"
app:likes_drawablePositionAnimator="cannonical class name"
>

<ImageButton
Expand All @@ -37,6 +39,8 @@ There are three implementations of LikesLayout: **LikesFrameLayout**, **LikesLin
app:likes_animationDuration="animationduration in milliseconds"
app:likes_tintMode="off|on_successively|on_random"
app:likes_tintColors="array resource id"
app:likes_drawableAnimator="cannonical class name"
app:likes_drawablePositionAnimator="cannonical class name"
/>

</ua.vlasov.likeslayout.LikesLinearLayout>
Expand All @@ -48,8 +52,10 @@ There is the list of available attributes:
| --- | --- | --- | --- | --- |
| likes_animationDuration | yes | yes | Animation duration in milliseconds. | 1200 |
| likes_drawable | yes | yes | Drawable that will be drawn when user presses view. | n/a |
| likes_drawableWidth | yes | yes | Custom drawable width. | 0 |
| likes_drawableAnimator | yes | yes | Drawable animator factory. | `@string/likes_drawable_animator_factory` |
| likes_drawableHeight | yes | yes | Custom drawable height. | 0 |
| likes_drawablePositionAnimator | yes | yes | Drawable's position animator factory. | `@string/likes_position_animator_factory` |
| likes_drawableWidth | yes | yes | Custom drawable width. | 0 |
| likes_mode | no | yes | Switch that allows to enable/disable drawing likes on touching child view. If `likes_drawable` is null, `likes_mode` will be considered as `disabled`. | disabled |
| likes_produceInterval | yes | yes | Rate at which new likes drawables are produced in milliseconds. | 300 |
| likes_tintMode | yes | yes | Switch that allows to enable/disable tinting drawables. | not_set |
Expand Down
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ buildscript {
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'

android {
Expand Down Expand Up @@ -53,5 +55,7 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
compile project(':library')
}
81 changes: 81 additions & 0 deletions app/src/main/java/ua/vlasov/likes_layout/BaseFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ua.vlasov.likes_layout;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import ua.vlasov.likeslayout.LikesLinearLayout;
import ua.vlasov.likeslayout.OnChildTouchListener;

/**
* Base fragment implementation.
*/
public class BaseFragment extends Fragment implements OnChildTouchListener {

@BindView(R.id.status)
TextView mStatus;

@BindView(R.id.likes_layout)
LikesLinearLayout mLikesLayout;

private int mFavoriteCounter;
private int mGradeCounter;
private int mStarsCounter;
private Unbinder mUnbinder;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mUnbinder = ButterKnife.bind(this, view);
mLikesLayout.setOnChildTouchListener(this);
updateStatus();
}

@Override
public void onDestroyView() {
mUnbinder.unbind();
super.onDestroyView();
}

@Override
public void onChildTouched(View child) {

}

@Override
public void onLikeProduced(View child) {
// do something here
switch (child.getId()) {
case R.id.btn_favorite: {
mFavoriteCounter++;
break;
}
case R.id.btn_grade: {
mGradeCounter++;
break;
}
case R.id.btn_stars: {
mStarsCounter++;
break;
}
}
updateStatus();
}

@Override
public void onChildReleased(View child, boolean isCanceled) {

}

private void updateStatus() {
mStatus.setText(String.format(Locale.US, "Counter. favorites: %1$d, grades: %2$d, stars: %3$d",
mFavoriteCounter, mGradeCounter, mStarsCounter));
}
}
43 changes: 43 additions & 0 deletions app/src/main/java/ua/vlasov/likes_layout/ChooseFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ua.vlasov.likes_layout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

/**
* Fragment that allows to choose what demo to show.
*/
public class ChooseFragment extends ListFragment implements AdapterView.OnItemClickListener {

public static ChooseFragment newInstance() {
Bundle args = new Bundle();
ChooseFragment fragment = new ChooseFragment();
fragment.setArguments(args);
return fragment;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String[] items = getResources().getStringArray(R.array.choices);
setListAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, items));
getListView().setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fragment;
if (position == 0) {
fragment = FromXmlFragment.newInstance();
} else {
fragment = FromCodeFragment.newInstance();
}
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ua.vlasov.likes_layout;

import ua.vlasov.likeslayout.DrawableAnimator;

/**
* Custom drawable animator factory.
*/
public class CustomDrawableAnimatorFactory implements DrawableAnimator.Factory {
@Override
public DrawableAnimator newInstance() {
return new DrawableAnimator.AlphaDrawableAnimator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ua.vlasov.likes_layout;

import java.util.concurrent.atomic.AtomicInteger;

import ua.vlasov.likeslayout.PositionAnimator;

/**
* Custom position animator factory.
*/
public class CustomPositionAnimatorFactory implements PositionAnimator.Factory {

private final AtomicInteger pathGenerator = new AtomicInteger();

@Override
public PositionAnimator newInstance() {
return new PositionAnimator.LinearSuccessiveRoutePositionAnimator(pathGenerator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ua.vlasov.likes_layout;

import ua.vlasov.likeslayout.PositionAnimator;

/**
* Custom position animator factory.
*/
public class CustomPositionAnimatorFactory2 implements PositionAnimator.Factory {

@Override
public PositionAnimator newInstance() {
return new PositionAnimator.LinearRandomRoutePositionAnimator();
}
}
126 changes: 126 additions & 0 deletions app/src/main/java/ua/vlasov/likes_layout/FromCodeFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package ua.vlasov.likes_layout;

import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.widget.Space;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import ua.vlasov.likeslayout.LikesAttributes;
import ua.vlasov.likeslayout.LikesLinearLayout;

/**
* Fragment that constructs LikesLayout from code.
*/
public class FromCodeFragment extends BaseFragment {

public static FromCodeFragment newInstance() {
Bundle args = new Bundle();
FromCodeFragment fragment = new FromCodeFragment();
fragment.setArguments(args);
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_code, container, false);
LikesLinearLayout likesLinearLayout = new LikesLinearLayout(getContext());
likesLinearLayout.setId(R.id.likes_layout);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
(int) (getResources().getDisplayMetrics().density * 250));
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
likesLinearLayout.setLayoutParams(params);
likesLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
likesLinearLayout.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
likesLinearLayout.setPadding(0, 0, 0, (int) (getResources().getDisplayMetrics().density * 16));
likesLinearLayout.getAttributes().setAnimationDuration(1200);
likesLinearLayout.getAttributes().setTintMode(LikesAttributes.TINT_MODE_ON_SUCCESSIVELY);
TypedArray colorsArray = getResources().obtainTypedArray(R.array.drawable_colors);
int[] colors = new int[colorsArray.length()];
for (int i = 0; i < colors.length; i++) {
colors[i] = colorsArray.getColor(i, Color.TRANSPARENT);
}
likesLinearLayout.getAttributes().setTintColors(colors);
colorsArray.recycle();
addFavoriteButton(likesLinearLayout);
addSpace(likesLinearLayout);
addGradeButton(likesLinearLayout);
addSpace(likesLinearLayout);
addStarsButton(likesLinearLayout);
view.addView(likesLinearLayout);
return view;
}

private void addSpace(LikesLinearLayout likesLinearLayout) {
Space space = new Space(getContext());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (getResources().getDisplayMetrics().density * 24),
ViewGroup.LayoutParams.MATCH_PARENT);
space.setLayoutParams(params);
likesLinearLayout.addView(space);
}

private void addFavoriteButton(LikesLinearLayout likesLinearLayout) {
ImageButton button = new ImageButton(getContext(), null, R.style.LikeButton_Favorite);
button.setId(R.id.btn_favorite);
button.setImageResource(R.drawable.ic_favorite);
DrawableCompat.setTint(button.getDrawable(), ContextCompat.getColor(getContext(), R.color.colorAccent));
final ViewGroup.LayoutParams params = likesLinearLayout
.newLayoutParamsBuilder(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_favorite_normal))
.setAnimationDuration(1000)
.setProduceInterval(500)
.setDrawableAnimatorFactory(new CustomDrawableAnimatorFactory())
.setLikesMode(LikesAttributes.LIKES_MODE_ENABLED)
.build();
button.setLayoutParams(params);
likesLinearLayout.addView(button);
}

private void addGradeButton(LikesLinearLayout likesLinearLayout) {
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));
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())
.setLikesMode(LikesAttributes.LIKES_MODE_ENABLED)
.build();
button.setLayoutParams(params);
likesLinearLayout.addView(button);
}

private void addStarsButton(LikesLinearLayout likesLinearLayout) {
ImageButton button = new ImageButton(getContext(), null, R.style.LikeButton_Stars);
button.setId(R.id.btn_stars);
button.setImageResource(R.drawable.ic_stars);
DrawableCompat.setTint(button.getDrawable(), ContextCompat.getColor(getContext(), R.color.colorAccent));
final ViewGroup.LayoutParams params = likesLinearLayout
.newLayoutParamsBuilder(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_stars_colored))
.setAnimationDuration(1600)
.setProduceInterval(200)
.setPositionAnimatorFactory(new CustomPositionAnimatorFactory())
.setTintMode(LikesAttributes.TINT_MODE_OFF)
.setDrawableWidth(getResources().getDisplayMetrics().density * 32)
.setDrawableHeight(getResources().getDisplayMetrics().density * 32)
.setLikesMode(LikesAttributes.LIKES_MODE_ENABLED)
.build();
button.setLayoutParams(params);
likesLinearLayout.addView(button);
}

}
27 changes: 27 additions & 0 deletions app/src/main/java/ua/vlasov/likes_layout/FromXmlFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ua.vlasov.likes_layout;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

public static FromXmlFragment newInstance() {
Bundle args = new Bundle();
FromXmlFragment fragment = new FromXmlFragment();
fragment.setArguments(args);
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}

}
Loading

0 comments on commit 938ef77

Please sign in to comment.