2015-01-02 23:15:07 +01:00
|
|
|
/*
|
2019-01-23 18:03:33 +01:00
|
|
|
* This is the source code of Telegram for Android v. 5.x.x.
|
2015-01-02 23:15:07 +01:00
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
2019-01-23 18:03:33 +01:00
|
|
|
* Copyright Nikolai Kudashov, 2013-2018.
|
2015-01-02 23:15:07 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.ui.Components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.animation.DecelerateInterpolator;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
2016-04-22 15:49:00 +02:00
|
|
|
import org.telegram.ui.ActionBar.Theme;
|
2015-01-02 23:15:07 +01:00
|
|
|
|
|
|
|
public class SlidingTabView extends LinearLayout {
|
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
public interface SlidingTabViewDelegate {
|
|
|
|
void didSelectTab(int tab);
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private SlidingTabViewDelegate delegate;
|
|
|
|
private int selectedTab = 0;
|
|
|
|
private int tabCount = 0;
|
|
|
|
private float tabWidth = 0;
|
|
|
|
private float tabX = 0;
|
|
|
|
private float animateTabXTo = 0;
|
|
|
|
private Paint paint = new Paint();
|
|
|
|
private long startAnimationTime = 0;
|
|
|
|
private long totalAnimationDiff = 0;
|
|
|
|
private float startAnimationX = 0;
|
|
|
|
private DecelerateInterpolator interpolator;
|
|
|
|
|
2015-05-03 13:48:36 +02:00
|
|
|
public SlidingTabView(Context context) {
|
|
|
|
super(context);
|
2015-01-02 23:15:07 +01:00
|
|
|
setOrientation(HORIZONTAL);
|
|
|
|
setWeightSum(100);
|
|
|
|
paint.setColor(0xffffffff);
|
2015-05-03 13:48:36 +02:00
|
|
|
setWillNotDraw(false);
|
2015-01-02 23:15:07 +01:00
|
|
|
interpolator = new DecelerateInterpolator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addTextTab(final int position, String title) {
|
|
|
|
TextView tab = new TextView(getContext());
|
|
|
|
tab.setText(title);
|
|
|
|
tab.setFocusable(true);
|
|
|
|
tab.setGravity(Gravity.CENTER);
|
|
|
|
tab.setSingleLine();
|
|
|
|
tab.setTextColor(0xffffffff);
|
|
|
|
tab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
|
|
|
|
tab.setTypeface(Typeface.DEFAULT_BOLD);
|
2017-03-31 01:58:05 +02:00
|
|
|
tab.setBackgroundDrawable(Theme.createSelectorDrawable(Theme.ACTION_BAR_PICKER_SELECTOR_COLOR, 0));
|
2015-01-02 23:15:07 +01:00
|
|
|
|
|
|
|
tab.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
didSelectTab(position);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
addView(tab);
|
|
|
|
LayoutParams layoutParams = (LayoutParams)tab.getLayoutParams();
|
2015-05-03 13:48:36 +02:00
|
|
|
layoutParams.height = LayoutHelper.MATCH_PARENT;
|
2015-01-02 23:15:07 +01:00
|
|
|
layoutParams.width = 0;
|
|
|
|
layoutParams.weight = 50;
|
|
|
|
tab.setLayoutParams(layoutParams);
|
|
|
|
|
|
|
|
tabCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDelegate(SlidingTabViewDelegate delegate) {
|
|
|
|
this.delegate = delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getSeletedTab() {
|
|
|
|
return selectedTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void didSelectTab(int tab) {
|
|
|
|
if (selectedTab == tab) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
selectedTab = tab;
|
|
|
|
animateToTab(tab);
|
|
|
|
if (delegate != null) {
|
|
|
|
delegate.didSelectTab(tab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void animateToTab(int tab) {
|
|
|
|
animateTabXTo = tab * tabWidth;
|
|
|
|
startAnimationX = tabX;
|
|
|
|
totalAnimationDiff = 0;
|
|
|
|
startAnimationTime = System.currentTimeMillis();
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
|
|
|
super.onLayout(changed, l, t, r, b);
|
|
|
|
tabWidth = (r - l) / (float)tabCount;
|
|
|
|
animateTabXTo = tabX = tabWidth * selectedTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDraw(Canvas canvas) {
|
|
|
|
if (tabX != animateTabXTo) {
|
|
|
|
long dt = System.currentTimeMillis() - startAnimationTime;
|
|
|
|
startAnimationTime = System.currentTimeMillis();
|
|
|
|
totalAnimationDiff += dt;
|
|
|
|
if (totalAnimationDiff > 200) {
|
|
|
|
totalAnimationDiff = 200;
|
|
|
|
tabX = animateTabXTo;
|
|
|
|
} else {
|
|
|
|
tabX = startAnimationX + interpolator.getInterpolation(totalAnimationDiff / 200.0f) * (animateTabXTo - startAnimationX);
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas.drawRect(tabX, getHeight() - AndroidUtilities.dp(2), (tabX + tabWidth), getHeight(), paint);
|
|
|
|
}
|
|
|
|
}
|