-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ added documentation * updated demo. Added sample that constructs layout from code. * refactored code * updated readme
- Loading branch information
Showing
33 changed files
with
1,503 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
app/src/main/java/ua/vlasov/likes_layout/BaseFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
app/src/main/java/ua/vlasov/likes_layout/ChooseFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/ua/vlasov/likes_layout/CustomDrawableAnimatorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/ua/vlasov/likes_layout/CustomPositionAnimatorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/ua/vlasov/likes_layout/CustomPositionAnimatorFactory2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
126
app/src/main/java/ua/vlasov/likes_layout/FromCodeFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
app/src/main/java/ua/vlasov/likes_layout/FromXmlFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.