Android : 애니메이션을 사용하여보기 표시 / 숨기기
세로 애니메이션을 통해보기를 표시 / 숨기는 방법을 결정하기 위해 여기에서 많은 Google 결과 / 질문을 살펴 봤지만 정확히 맞거나 너무 모호하지 않은보기를 찾을 수없는 것 같습니다.
다른 레이아웃 아래에 있고 다른 여러 위젯 위에있는 레이아웃 (실행 취소 막대)이 있습니다. 이 실행 취소 막대는 상황에 따라 수직으로 열리고 닫혀 야합니다.
현재 내가하는 일은보기를 표시하거나 사라지도록 설정하는 것입니다.
android:animateLayoutChanges="true"
부모 레이아웃 내부 에 속성을 설정합니다 .
그렇지 않은 경우보기를 레이아웃에 넣고 android:animateLayoutChanges="true"
해당 레이아웃에 대해 설정 합니다.
참고 : 이는 API 레벨 11 이상 (Android 3.0)에서만 작동합니다.
RelativeLayout
애니메이션으로 레이아웃을 표시하거나 숨기는 확장 프로그램을 만들었습니다 . View
이러한 기능을 얻기 위해 모든 종류의 확장 이 가능합니다.
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;
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 = (AnimationSet) 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;
}
}
및 Animation
사용하여 원본을 재정의 할 수 있습니다.overrideDefaultInAnimation
overrideDefaultOutAnimation
내 원래 애니메이션은 페이드 인 / 아웃이었고, 화면 안팎으로 번역하기위한 XML 애니메이션 파일을 추가하고 있습니다 (위에서 위로 번역).
in_animation.xml :
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="600"
android:fillAfter="false"
android:fromXDelta="0"
android:fromYDelta="-100%p"
android:toXDelta="0"
android:toYDelta="0" />
out_animation.xml :
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="600"
android:fillAfter="false"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="-100%p" />
이는 API 12 이상에서 한 줄 문으로 합리적으로 달성 할 수 있습니다. 아래는 v
애니메이션을 적용하려는 뷰 의 예 입니다.
v.animate().translationXBy(-1000).start();
그러면 View
해당 항목이 왼쪽으로 1000px만큼 슬라이드 됩니다. 뷰를 UI로 다시 슬라이드하려면 다음을 수행하면됩니다.
v.animate().translationXBy(1000).start();
누군가 이것이 유용하다고 생각하기를 바랍니다.
뷰의 높이 (예 : 0에서 특정 숫자까지) 만 애니메이션하려면 고유 한 애니메이션을 구현할 수 있습니다.
final View v = getTheViewToAnimateHere();
Animation anim=new Animation(){
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
// Do relevant calculations here using the interpolatedTime that runs from 0 to 1
v.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, (int)(30*interpolatedTime)));
}};
anim.setDuration(500);
v.startAnimation(anim);
이 두 기능을 사용하여 전환 애니메이션으로보기를 부드럽게 숨기고 표시했습니다.
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void expand(final View v, int duration, int targetHeight, final int position) {
int prevHeight = v.getHeight();
v.setVisibility(View.VISIBLE);
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, targetHeight);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
v.getLayoutParams().height = (int) animation.getAnimatedValue();
v.requestLayout();
}
});
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.setDuration(duration);
valueAnimator.start();
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
v.clearAnimation();
}
});
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void collapse(final View v, int duration, int targetHeight, final int position) {
if (position == (data.size() - 1)) {
return;
}
int prevHeight = v.getHeight();
ValueAnimator valueAnimator = ValueAnimator.ofInt(prevHeight, targetHeight);
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
v.getLayoutParams().height = (int) animation.getAnimatedValue();
v.requestLayout();
}
});
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.setDuration(duration);
valueAnimator.start();
valueAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
animBoolArray.put(position, false);
v.clearAnimation();
}
});
}
아마도 당신이 찾고있는 것은 SlidingDrawer 일 것 입니다.
위치 변경을위한 애니메이션을 생성하는 TranslateAnimation 클래스를 사용해보십시오 . 도움이 필요하면 이것을 읽어보십시오-http: //developer.android.com/reference/android/view/animation/TranslateAnimation.html
업데이트 : 여기에 이에 대한 예가 있습니다. 보기 높이가 50이고 숨기기 모드에서 10 픽셀 만 표시하려는 경우. 샘플 코드는 다음과 같습니다.
TranslateAnimation anim=new TranslateAnimation(0,0,-40,0);
anim.setFillAfter(true);
view.setAnimation(anim);
추신 : 필요에 따라 애니메이션을 사용하는 데 도움이되는 많은 또는 다른 방법이 있습니다. 코드를 완전히 사용자 지정하려는 경우 RelativeLayout.LayoutParams도 살펴보십시오. 그러나 TranslateAnimation을 사용하는 것이 사용하기 더 쉽습니다.
편집 :-LayoutParams를 사용하는 복잡한 버전
RelativeLayout relParam=new RelativeLayout.LayoutParam(RelativeLayout.LayoutParam.FILL_PARENT,RelativeLayout.LayoutParam.WRAP_CONTENT); //you can give hard coded width and height here in (width,height) format.
relParam.topMargin=-50; //any number that work.Set it to 0, when you want to show it.
view.setLayoutParams(relparam);
이 예제 코드는 레이아웃의 이름을 변경하지 않으면 뷰를 RelativeLayout에 배치한다고 가정하지만 다른 레이아웃은 작동하지 않을 수 있습니다. 애니메이션 효과를 주려면 topMargin을 천천히 줄이거 나 늘립니다. 거기에서도 Thread.sleep () 사용을 고려할 수 있습니다.
이 시도.
view.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
}
});
우선보고 싶은 뷰의 높이를 얻고 뷰가 표시되면 저장하기 위해 부울을 만듭니다.
int heigth=0;
boolean showing=false;
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
proDetailsLL.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// gets called after layout has been done but before display
// so we can get the height then hide the view
proHeight = proDetailsLL.getHeight(); // Ahaha! Gotcha
proDetailsLL.getViewTreeObserver().removeGlobalOnLayoutListener(this);
proDetailsLL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0));
}
});
그런 다음보기 숨기기를 표시하는 메서드를 호출하고 부울 값을 변경합니다.
slideInOutAnimation(showing, heigth, layout);
proShowing = !proShowing;
방법:
/**
* Method to slide in out the layout
*
* @param isShowing
* if the layout is showing
* @param height
* the height to slide
* @param slideLL
* the container to show
*/
private void slideInOutAnimation(boolean isShowing, int height, final LinearLayout slideLL, final ImageView arroIV) {
if (!isShowing) {
Animation animIn = new Animation() {
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
// Do relevant calculations here using the interpolatedTime that runs from 0 to 1
slideLL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) (heigth * interpolatedTime)));
}
};
animIn.setDuration(500);
slideLL.startAnimation(animIn);
} else {
Animation animOut = new Animation() {
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
// Do relevant calculations here using the interpolatedTime that runs from 0 to 1
slideLL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
(int) (heigth * (1 - interpolatedTime))));
}
};
animOut.setDuration(500);
slideLL.startAnimation(animOut);
}
}
ViewAnimator :
XML에서 :
<ViewAnimator
android:id="@+id/animator_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inAnimation="@anim/slide_down_text"
android:outAnimation="@anim/slide_up_text">
<TextView
android:id="@+id/text_message_authentication"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="message_error_authentication" />
<TextView
android:id="@+id/text_message_authentication_connection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="message_error_authentication_connection" />
<TextView
android:id="@+id/text_message_authentication_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="message_error_authentication_field_empty" />
</ViewAnimator>
기능 :
public void show(int viewId) {
ViewAnimator animator = (ViewAnimator) findView(animatorId);
View view = findViewById(viewId);
if (animator.getDisplayedChild() != animator.indexOfChild(view)) {
animator.setDisplayedChild(animator.indexOfChild(view));
}
}
private void showAuthenticationConnectionFailureMessage() {
show(R.id.text_message_authentication_connection);
}
참고URL : https://stackoverflow.com/questions/9252740/android-show-hide-a-view-using-an-animation
'Nice programing' 카테고리의 다른 글
ASP.NET MVC 3 작업 필터에 종속성 삽입. (0) | 2020.10.14 |
---|---|
Facebook의 Graph API 호출 제한은 얼마입니까? (0) | 2020.10.14 |
Bitbucket에서 git을 사용하여 Heroku에 배포 (0) | 2020.10.14 |
배열에없는 경우 coffeescript 확인 (0) | 2020.10.14 |
초기 및 후기 바인딩 (0) | 2020.10.14 |