2013-09-25 3 views
2

예, ToggleButton을 2 개의 그림 (On, Off)으로 만들 수 있지만 ToggleButton을 3-5 개의 그림으로 만들고 싶습니다. 예를 들어애니메이션 토글 버튼을 만들려면 어떻게해야합니까?

은 때를 OFF 내가 클릭

  1. 사진 오프
  2. 중동 그림
  3. 사진

  • 그리고 ON을하고 때를 클릭 :

    1. 사진의
    2. 중동 그림

    그래서 그것이 내가 이미지 뷰와 시간에 사용할 수있는, 프레임 애니메이션처럼

  • OFF 사진.

  • +0

    지금까지 시도한 것이 있습니까? 예를 들어, STYLES로 노는가? – bofredo

    답변

    1

    편집 :

    당신은 Frame Animation 사용할 수 있습니다 res/drawable/myanim.xml에서 :

    <?xml version="1.0" encoding="utf-8"?> 
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> 
        <item android:drawable="@drawable/pic_one" android:duration="50"/> 
        <item android:drawable="@drawable/pic_two" android:duration="50" /> 
        <item android:drawable="@drawable/pic_three" android:duration="50" /> 
    </animation-list> 
    

    당신은 다음 일반 당김으로이 애니메이션을 사용할 수 있습니다

    <ImageView android:id="@+id/image" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@drawable/myanim"/> 
    

    을 당신이 애니메이션을 시작하려면

    AnimationDrawable backgroundDrawable = (AnimationDrawable) image.getDrawable(); 
    backgroundDrawable.start(); 
    

    Value Animator을 사용할 수도 있습니다. 나는 이것을 테스트하지는 않았지만 다음과 같은 버튼을 onClick 처리기 버튼에 넣을 수 있어야합니다.

    int[] backgrounds = ...;//ids of the backgrounds for the button 
    ValueAnimator anim = ValueAnimator.ofInt(0, backgrounds.length); 
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
        @Override 
        public void onAnimationUpdate(ValueAnimator animation) { 
         int i = (Integer) animation.getAnimatedValue(); 
         int backgroundId = backgrounds[i]; 
         yourButton.setBackgroundResource(backgroundId); 
        } 
    }); 
    anim.setDuration(500); //0.5 seconds 
    anim.start(); 
    
    +1

    네,하지만 ToggleButton 스타일로 할 수 있다고 생각합니다. –

    +0

    예를 들어 "켜기"또는 "끄기"배경으로 그림을 사용하지 않고 다른 그림의 애니메이션을 사용할 수 있습니다. –

    +0

    @ АлексейМаксимов OK, 내 대답을 편집했습니다. 프레임 애니메이션 예제를 추가했습니다. –

    관련 문제