مدیریت کاربران مشترک
بروز خطا
امتیازات دریافتی
یک Animation Tab کاملا اختصاصی
توسط این کلاس میتونید یک انیمیشن کاملا اختصاصی روی Tab خود پیاده کنید
ابعاد این کلاس اختصاصی رو به اندازه ابعاد Tab خود ست کنید و در یک ریلیتیو لیوت روی آن قرار دهید . میتونید زمانهای مختلف انیمیشن اول و دوم و تاخیر بین انیمیشن اول و دوم و رنگ indicator و ضخامت آن و تعداد تب ها و تب پیش فرض فعال را مقداردهی و شخصی سازی کنید . نمونه نوشته شده در مثال رو ببینید .
اسنیپ
AnimationTabexample+
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class AnimationTab extends ImageView {
// -------------------------------------
private int TABS = 4;
private int STARTING_TAB = 1;
private int INDICATOR_HEIGHT = 8;
private int COLOR = Color.parseColor("#ff55cc");
private int FIRST_ANIMATION_TIME = 230; // time in ms
private int DELAY = 140;
private int SECOND_ANIMATION_TIME = 200;
// -------------------------------------
private int new_Tab;
private Paint paint;
private int w, h, tabWidth;
private float x;
private int direction = 1;
private boolean must_Animate;
private int ms;
private float delta_First, delta_Second, delta1, delta2;
// =======================================================
public AnimationTab(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
public AnimationTab(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public AnimationTab(Context context) {
super(context);
initialize();
}
/**
* Sets the number of tabs.
*
* @param tabs number of tabs.
* @return instance of this calss with the specified parameter.
*/
public AnimationTab set_Tabs(int tabs) {
TABS = tabs;
return this;
}
/**
* Sets the default selected tab.
*
* @param startingTab index of default selected tab: (starting from 1)
* @return instance of this calss with the specified parameter.
*/
public AnimationTab set_StartingTab(int startingTab) {
TABS = startingTab;
return this;
}
/**
* Sets the height of indicator line.
*
* @param indicatorHeight indicator line height in pixels.
* @return instance of this calss with the specified parameter.
*/
public AnimationTab set_Indicator_Height(int indicatorHeight) {
TABS = indicatorHeight;
return this;
}
/**
* Sets the color of indicator line.
*
* @param color indicator line color.
* @return instance of this calss with the specified parameter.
*/
public AnimationTab set_Color(int color) {
COLOR = color;
return this;
}
/**
* Sets the forwarding animation time.
*
* @param firstAnimTime forwarding animation time (in milliseconds).
* @return instance of this calss with the specified parameter.
*/
public AnimationTab set_FirstAnimation_Time(int firstAnimTime) {
FIRST_ANIMATION_TIME = firstAnimTime;
return this;
}
/**
* Sets the delay between forwarding animation and backwarding animation.
*
* @param delay The delay (in milliseconds) until the second animation
* will be executed after first animation.
* @return instance of this calss with the specified parameter.
*/
public AnimationTab set_Delay(int delay) {
DELAY = delay;
return this;
}
/**
* Sets the backwarding animation time.
*
* @param secondAnimTime backwarding animation time (in milliseconds).
* @return instance of this calss with the specified parameter.
*/
public AnimationTab set_SecondAnimation_Time(int secondAnimTime) {
SECOND_ANIMATION_TIME = secondAnimTime;
return this;
}
/**
* Sets touch listener and canvas specifications on instance and draws the view.
*/
public void initialize() {
paint = new Paint();
paint.setColor(COLOR);
paint.setStrokeWidth(INDICATOR_HEIGHT);
setOnTouch();
invalidate();
}
// =======================================================
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
w = getWidth();
h = getHeight();
tabWidth = (int) (w / TABS);
paint = new Paint();
paint.setColor(COLOR);
paint.setStrokeWidth(INDICATOR_HEIGHT);
}
// =======================================================
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(tabWidth * (STARTING_TAB - (direction + 1) / 2) + delta1, h - 4,
tabWidth * (STARTING_TAB + (direction - 1) / 2) + delta2, h - 4, paint);
}
// =======================================================
private void setOnTouch() {
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (must_Animate) {
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
x = event.getX();
new_Tab = 1 + ((int) (x / tabWidth));
if (new_Tab < STARTING_TAB) {
direction = -1;
start_Animation();
} else if (STARTING_TAB < new_Tab) {
direction = 1;
start_Animation();
}
break;
}
return true;
}
});
}
// =======================================================
private void start_Animation() {
must_Animate = true;
float d = tabWidth * Math.abs(new_Tab - STARTING_TAB);
delta_First = d / SECOND_ANIMATION_TIME;
delta_Second = d / FIRST_ANIMATION_TIME;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (must_Animate) {
try {
delta1 = direction * (Math.min(Math.max(ms - DELAY, 0), SECOND_ANIMATION_TIME)) * delta_First;
delta2 = direction * Math.min(ms, FIRST_ANIMATION_TIME) * delta_Second;
Thread.sleep(1);
if (ms == Math.max(FIRST_ANIMATION_TIME, DELAY + SECOND_ANIMATION_TIME) + 1) {
delta1 = 0;
delta2 = 0;
STARTING_TAB = new_Tab;
must_Animate = false;
ms = 0;
}
ms++;
postInvalidate();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
}
public class ActivityMain extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AnimationTab(this)
.set_Tabs(4)
.set_StartingTab(1)
.set_Indicator_Height(8)
.set_Color(Color.parseColor("#ff55cc"))
.set_FirstAnimation_Time(230)
.set_Delay(140)
.set_SecondAnimation_Time(200)
.initialize();
}
}
length : 0
words : 0
lines : 0
حقوق مادی و معنوی تمامی آثار و محتویات عرضه شده در این وب سایت، متعلق به شرکت «فوژان رسانه ایده هوشمند» و پدیدآورندگان آثار بوده و حسب مورد دارای مجوز از مراجع ذی صلاح می باشد.
کپی برداری از مطالب این سایت حتی با ذکر منبع جایز نیست.
ویرایش
پیام