VIDEO
gradle file:
compile 'com.github.iojjj:likeslayout:1.0.0'
compile 'com.jakewharton:butterknife:8.0.1'
BaseFragment
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 = new LikesLinearLayout(getContext());
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));
}
}
ChooseFragment
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(); }
}
CustomDrawableAnimatorFactory
public class CustomDrawableAnimatorFactory implements DrawableAnimator.Factory {
@Override
public DrawableAnimator newInstance () {
return new DrawableAnimator.AlphaDrawableAnimator(); }
}
CustomPositionAnimatorFactory
public class CustomPositionAnimatorFactory
implements PositionAnimator.Factory {
private final AtomicInteger pathGenerator = new AtomicInteger();
@Override
public PositionAnimator newInstance () {
return new PositionAnimator.LinearSuccessiveRoutePositionAnimator(pathGenerator ); }
}
CustomPositionAnimatorFactory2
public class CustomPositionAnimatorFactory2
implements PositionAnimator.Factory {
@Override
public PositionAnimator newInstance () {
return new PositionAnimator.LinearRandomRoutePositionAnimator();
}
}
FromCodeFragment
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); }
}
FromXmlFragment
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();
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 );
}
@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 ); }
}
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (@Nullable Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main );
if (savedInstanceState == null ) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container , ChooseFragment.newInstance ())
.commit(); }
}
}
activity_main
<? xml version= "1.0" encoding= "utf-8" ?>
<FrameLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
android :id= "@+id/container"
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
</FrameLayout>
fragment_code
<? xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: app = "http://schemas.android.com/apk/res-auto"
xmlns: tools = "http://schemas.android.com/tools"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
tools :context= ".MainActivity" >
<TextView android :id= "@+id/status"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :padding= "16dp" />
</RelativeLayout>
fragment_main
<? xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: app = "http://schemas.android.com/apk/res-auto"
xmlns: tools = "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
tools:context= ".MainActivity" >
<TextView
android:id= "@+id/status"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:padding= "16dp" />
<Button
android:id= "@+id/btn_produce"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:layout_below= "@+id/status"
android:layout_marginTop= "16dp"
android:text= "Produce likes for 3 seconds"
android:visibility= "gone" />
<com.github.iojjj.likeslayout.LikesLinearLayout
android:id= "@+id/likes_layout"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:layout_alignParentBottom= "true"
android:gravity= "bottom|center_horizontal"
android:orientation= "horizontal"
android:paddingBottom= "16dp"
app:likes_animationDuration= "1200"
app:likes_tintColors= "@array/drawable_colors"
app:likes_tintMode= "on_successively" >
<ImageButton
android:id= "@+id/btn_favorite"
style= "@style/LikeButton.Favorite" />
<Space
android:layout_width= "24dp"
android:layout_height= "match_parent" />
<ImageButton
android:id= "@+id/btn_grade"
style= "@style/LikeButton.Grade"
app:likes_produceInterval= "200"
app:likes_tintMode= "on_random" />
<Space
android:layout_width= "24dp"
android:layout_height= "match_parent" />
<ImageButton
android:id= "@+id/btn_stars"
style= "@style/LikeButton.Stars"
app:likes_animationDuration= "2500"
app:likes_drawableHeight= "48dp"
app:likes_drawableWidth= "48dp"
app:likes_tintMode= "off" />
</com.github.iojjj.likeslayout.LikesLinearLayout>
</RelativeLayout>
ic_favorite
<? xml version= "1.0" encoding= "utf-8" ?>
<selector xmlns: android = "http://schemas.android.com/apk/res/android" >
<item android :drawable= "@drawable/ic_favorite_pressed" android :state_pressed= "true" />
<item android :drawable= "@drawable/ic_favorite_normal" />
</selector>
ic_grade
<? xml version= "1.0" encoding= "utf-8" ?>
<selector xmlns: android = "http://schemas.android.com/apk/res/android" >
<item android :drawable= "@drawable/ic_grade_pressed" android :state_pressed= "true" />
<item android :drawable= "@drawable/ic_grade_normal" /> </selector>
ic_starts
<? xml version= "1.0" encoding= "utf-8" ?>
<selector xmlns: android = "http://schemas.android.com/apk/res/android" >
<item android :drawable= "@drawable/ic_stars_pressed" android :state_pressed= "true" />
<item android :drawable= "@drawable/ic_stars_normal" />
</selector>
ic_favorite_normal
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>
ic_favorite_pressed
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#c0000000"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>
ic_grade_normal
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
</vector>
ic_grade_pressed
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#c0000000"
android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
</vector>
ic_stars_colored
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#2448d6"
android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM16.23,18L12,15.45 7.77,18l1.12,-4.81 -3.73,-3.23 4.92,-0.42L12,5l1.92,4.53 4.92,0.42 -3.73,3.23L16.23,18z"/>
</vector>
ic_stars_normal
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM16.23,18L12,15.45 7.77,18l1.12,-4.81 -3.73,-3.23 4.92,-0.42L12,5l1.92,4.53 4.92,0.42 -3.73,3.23L16.23,18z"/>
</vector>
ic_stars_pressed
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#c0000000"
android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM16.23,18L12,15.45 7.77,18l1.12,-4.81 -3.73,-3.23 4.92,-0.42L12,5l1.92,4.53 4.92,0.42 -3.73,3.23L16.23,18z"/>
</vector>
style
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="LikeButton">
<item name="android:layout_width">56dp</item>
<item name="android:layout_height">56dp</item>
<item name="android:padding">4dp</item>
<item name="android:background">@null</item>
<item name="android:scaleType">fitCenter</item>
<item name="likes_mode">enabled</item>
<item name="android:tint">@color/colorAccent</item>
</style>
<style name="LikeButton.Favorite">
<item name="android:src">@drawable/ic_favorite</item>
<item name="likes_drawable">@drawable/ic_favorite_normal</item>
</style>
<style name="LikeButton.Grade">
<item name="android:src">@drawable/ic_grade</item>
<item name="likes_drawable">@drawable/ic_grade_normal</item>
</style>
<style name="LikeButton.Stars">
<item name="android:src">@drawable/ic_stars</item>
<item name="likes_drawable">@drawable/ic_stars_colored</item>
</style>
</resources>
String
<resources>
<string name="app_name">Likes Layout</string>
<string-array name="choices">
<item>From XML</item>
<item>From Java</item>
</string-array>
<string name="custom_drawable_animator_factory">com.github.iojjj.likeslayout.CustomDrawableAnimatorFactory</string>
<string name="custom_position_animator_factory">com.github.iojjj.likeslayout.CustomPositionAnimatorFactory</string>
<string name="custom_position_animator_factory2">com.github.iojjj.likeslayout.CustomPositionAnimatorFactory2</string>
</resources>
color
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<array name="drawable_colors">
<item>#f44336</item>
<item>#e91e63</item>
<item>#673ab7</item>
<item>#3f51b5</item>
<item>#2196f3</item>
<item>#03a9f4</item>
<item>#00bcd4</item>
<item>#009688</item>
<item>#4caf50</item>
<item>#8bc34a</item>
<item>#cddc39</item>
<item>#ffeb3b</item>
<item>#ffc107</item>
<item>#ff9800</item>
<item>#ff5722</item>
<item>#795548</item>
<item>#9e9e9e</item>
<item>#607d8b</item>
</array>
</resources>
No comments:
Post a Comment