0

나는 배경을 drawable에서 설정 한 RelativeLayout을 가지고 있습니다. RadioButton을 선택하면 RelativeLayout의 배경을 다른 배경으로 변경할 수있었습니다. 그런데 어떻게 변화가 생길 때 애니메이션을 줄까요?RelativeLayout 배경색

코드 : activity_main.xml : (당김)

<RelativeLayout 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:gravity="center" 
    android:background="@drawable/original" 
    android:id="@+id/rel1"> 
    <RadioButton 
     android:id="@+id/radio" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"/> 
</RelativeLayout> 

original.xml :

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid 
     android:color="#ffffff"> 
    </solid> 
    <corners 
     android:radius="50dp"> 
    </corners> 
</shape> 

pressed.xml :

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid 
     android:color="#2196F3"> 
    </solid> 
    <corners 
     android:radius="50dp"> 
    </corners> 
</shape> 

자바 클래스 :

radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
        if(b){ 
        relativeLayout.setBackgroundResource(R.drawable.pressed); 
       } 
      } 
     }); 
+0

쉽게 ObjectAnimator를 사용하여 수행 할 수 있습니다. –

+0

코드 – jobin

+0

을 보내 주시겠습니까? https://stackoverflow.com/questions/11419141/fading-in-text-in-android-using-animationutils-loadanimation –

답변

2

문제는 어떻게 든 모서리가 잘리고 레이아웃 배경이 애니메이션으로 변하는 것으로 귀결됩니다. 따라서 이론적으로 레이아웃에 전경 드로잉을 설정하고 ArgbEvaluator을 사용하여 배경 드로잉을 애니메이션 할 수 있습니다.

연습으로 이동.

<RelativeLayout 
    android:id="@+id/layout" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:foreground="@drawable/frame" 
    android:background="@color/colorAccent" /> 
:

enter image description here

레이아웃의 전경으로 그 당김 적용 :

는 저자의 주 문제에 대한 편리 할 수있는 마스크 묘화는, this answer에서보세요

코드에서 애니메이션을 수행 할 때마다 :

 


    final int from = ContextCompat.getColor(this, R.color.colorAccent); 
    final int to = ContextCompat.getColor(this, R.color.colorPrimary); 

    ValueAnimator anim = new ValueAnimator(); 
    anim.setIntValues(from, to); 
    anim.setEvaluator(new ArgbEvaluator()); 
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator valueAnimator) { 
      view.setBackgroundColor((Integer)valueAnimator.getAnimatedValue()); 
     } 
    }); 

    anim.setDuration(1000); 
    anim.start(); 
 

다음은 출력을 얻을 것이다 무엇 : 당신이 프레임 그릴 수의 색상을 변경하려면, 당신은 xml로 포장하고 android:tint을 적용 할 수 있습니다

enter image description here

.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item > 
     <bitmap android:src="@drawable/frame" android:tint="#ccaaff"/> 
    </item> 
</layer-list> 

이제이 드로어 블을 레이아웃의 전경으로 설정하십시오.

+0

전경으로 드로어 블을 설정하면 라디오 버튼이 보이지 않습니다 – jobin

+0

배경으로 colorAccent를 설정하면 모서리가 다른 색상이됩니다 – jobin

+0

해당 동작과 함께 간단한 프로젝트를 게시하십시오. – azizbekian