PDA

View Full Version : انیمیشن برای show/hide کردن view



haniiii
جمعه 11 بهمن 1392, 14:52 عصر
سلام دوستان

بنده یه مشکل در زمینه انیمیشن ها و view ها دارم ... ممنون میشم کمک کنید

فرض کنید قراره در برنامه با زدن دکمه show/hide که در ابتدای صفحه تعبیه شده ویو مورد نظر ( در اینجا از نوع relativelayout ) ... با توجه به دکمه مربوطه show/hide بشه
این ویو قراره با یه translateAnimation به صورت slide up و slide down نشون داده بشه

( ویو از قبل به صورت Gone در xml در نظر گرفته شده )

این کد فقط ویو مورد نظر (Myview ) رو Translate میکنه و بقیه ویو های پایین اون ثابت باقی میمونن ... حالت ایده ال این هست که بقیه ویو ها هم متناسب تغییرمکان بدن



animation1 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation1.setDuration(600);

animation2 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f
);

animation2.setDuration(600);

animation2.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub

MyView.setVisibility(View.GONE);
}
});

MyView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

if(tNumber == 0)
{
tNumber = 1;
MyView.startAnimation(animation1);
MyView.setVisibility(View.VISIBLE);
}
else
{
tNumber = 0;
MyView.startAnimation(animation2);
}

}
});


ممنون

rubiks.kde
جمعه 11 بهمن 1392, 22:30 عصر
من کلاس رو قبلا نوشتم به این صورت :

public class AnimatingRelativeLayout extends RelativeLayout
{
Context context;
Animation inAnimation;
Animation outAnimation;

public AnimatingRelativeLayout(Context context)
{
super(context);
this.context = context;
initAnimations();

}

public AnimatingRelativeLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
this.context = context;
initAnimations();
}

public AnimatingRelativeLayout(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
this.context = context;
initAnimations();
}

private void initAnimations()
{
inAnimation = (Animation) AnimationUtils.loadAnimation(context, R.anim.in_animation);
outAnimation = (Animation) AnimationUtils.loadAnimation(context, R.anim.out_animation);
}

public void show()
{
if (isVisible()) return;
show(true);
}

public void show(boolean withAnimation)
{
if (withAnimation) this.startAnimation(inAnimation);
this.setVisibility(View.VISIBLE);
}

public void hide()
{
if (!isVisible()) return;
hide(true);
}

public void hide(boolean withAnimation)
{
if (withAnimation) this.startAnimation(outAnimation);
this.setVisibility(View.GONE);
}

public boolean isVisible()
{
return (this.getVisibility() == View.VISIBLE);
}

public void overrideDefaultInAnimation(Animation inAnimation)
{
this.inAnimation = inAnimation;
}

public void overrideDefaultOutAnimation(Animation outAnimation)
{
this.outAnimation = outAnimation;
}
}


طریقه استفاده از کلاس هم راحته میتونید برای show/hide کردن هر انیمیشینی بدید و برای show/hide حتما از توابع show و hide تعریف شده استفاده کنید.