Last active
April 3, 2016 18:41
-
-
Save ViliusKraujutis/2c123434429c6710112a64fb9725a4f1 to your computer and use it in GitHub Desktop.
Do you want to create a tab button with rotatable image view? I wanted to create a button for Playgong's "Explore" tab with spinning compass icon, so I created `RotatableTabButtonImageView` which not only fades in/out icon, but also rotates it as pages are scrolled. Look to `setVividness` method. This method is called from `TabsToolbar` method `…
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
package com.playgong.view; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.os.Build; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.RelativeLayout; | |
import com.playgong.R; | |
import com.playgong.fragment.TabNavigationFragment; | |
import butterknife.ButterKnife; | |
import butterknife.InjectView; | |
import butterknife.OnClick; | |
/** | |
* @author Vilius Kraujutis | |
* @since 2015-01-14 12:50 | |
*/ | |
public class TabsToolbar extends RelativeLayout implements ViewPager.OnPageChangeListener { | |
@InjectView(R.id.tabnav_tabs_explore_tab) | |
TabButtonImageView discover; | |
@InjectView(R.id.tabnav_tabs_playgong_tab) | |
TabButtonImageView playgong; | |
@InjectView(R.id.tabnav_tabs_profile_tab) | |
TabButtonImageView profile; | |
@InjectView(R.id.tabnav_tabs_indicator) | |
TabsIndicatorView indicator; | |
private ViewPager pager; | |
public TabsToolbar(Context context) { | |
super(context); | |
} | |
public TabsToolbar(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public TabsToolbar(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public TabsToolbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
} | |
@Override | |
protected void onFinishInflate() { | |
super.onFinishInflate(); | |
ButterKnife.inject(this); | |
} | |
@OnClick(R.id.tabnav_tabs_explore_tab) | |
public void onDiscoverTabClicked(View tabButton) { | |
pager.setCurrentItem(TabNavigationFragment.TAB_EXPLORE); | |
} | |
@OnClick(R.id.tabnav_tabs_playgong_tab) | |
public void onPlaygongTabClicked(View tabButton) { | |
pager.setCurrentItem(TabNavigationFragment.TAB_PLAYGONG); | |
} | |
@OnClick(R.id.tabnav_tabs_profile_tab) | |
public void onProfileTabClicked(View tabButton) { | |
pager.setCurrentItem(TabNavigationFragment.TAB_DASHBOARD); | |
} | |
@Override | |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
TabButtonImageView leftTab = getTabButton(position); | |
TabButtonImageView rightTab = getTabButton(position + 1); | |
indicator.onPageSwiped(leftTab, 1 - positionOffset, rightTab, positionOffset); | |
rightTab.setVividness(positionOffset); | |
leftTab.setVividness(1 - positionOffset); | |
} | |
@Override | |
public void onPageSelected(@TabNavigationFragment.TAB int position) { | |
discover.setAlpha(position == TabNavigationFragment.TAB_EXPLORE ? Vividnessable.TAB_ACTIVE_ALPHA : Vividnessable.TAB_INACTIVE_ALPHA); | |
playgong.setAlpha(position == TabNavigationFragment.TAB_PLAYGONG ? Vividnessable.TAB_ACTIVE_ALPHA : Vividnessable.TAB_INACTIVE_ALPHA); | |
profile.setAlpha(position == TabNavigationFragment.TAB_DASHBOARD ? Vividnessable.TAB_ACTIVE_ALPHA : Vividnessable.TAB_INACTIVE_ALPHA); | |
} | |
@Override | |
public void onPageScrollStateChanged(int state) { | |
} | |
private TabButtonImageView getTabButton(int pageIndex) { | |
switch (pageIndex) { | |
case TabNavigationFragment.TAB_EXPLORE: | |
return discover; | |
case TabNavigationFragment.TAB_PLAYGONG: | |
return playgong; | |
default: | |
case TabNavigationFragment.TAB_DASHBOARD: | |
return profile; | |
} | |
} | |
public void setPager(ViewPager pager) { | |
this.pager = pager; | |
} | |
} |
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
package com.playgong.view; | |
/** | |
* @author Vilius Kraujutis | |
* @since 2015-04-29 01:53. | |
*/ | |
public interface Vividnessable { | |
float TAB_ACTIVE_ALPHA = 1f; | |
float TAB_INACTIVE_ALPHA = 0.3f; | |
void setVividness(float vividness); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment