2016-01-24 87 views
0

我有一個圓形的自定義狀態,我希望它改變其顏色,但我得到的形狀爲正方形不是圓的,因爲我想如何將顏色更改動畫賦予自定義形狀?

這裏是我的形狀:

<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval"> 

<solid 
    android:color="#666666"/> 

<size 
    android:width="120dp" 
    android:height="120dp"/> 

在我的佈局:

<ImageView 
    android:id="@+id/rgb_led" 
    android:visibility="gone" 
    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:background="@drawable/round_led" 
    android:layout_centerHorizontal="true" /> 
代碼

ledImageView.setVisibility(View.VISIBLE); 
    int colorFrom = getResources().getColor(colorFromId); 
    int colorTo = getResources().getColor(colorToId); 
    colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); 
    colorAnimation.setDuration(duration); 
    colorAnimation.setRepeatCount(ValueAnimator.INFINITE); 
    colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 

     @Override 
     public void onAnimationUpdate(ValueAnimator animator) { 
      ledImageView.setBackgroundColor((int) animator.getAnimatedValue()); 
     } 

    }); 
    colorAnimation.start(); 

但我得到一個閃爍的方形不是圓形!?

編輯:
我實際上想動畫一個圓形的圖像,以獲得閃爍的彩色光的效果,可以改變顏色和閃爍持續時間。
乾杯。

+0

我建議你看一下這個【答案】(https://stackoverflow.com/a/3208800/113012)的解決方案 –

回答

0

試試這個代碼,我用它在我的應用程序顯示一個圓形動畫循環:

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="5" 
    android:toDegrees="360" > 

<!-- Duration = 1 means that the rotation will be done in 1 second 
    increase duration if you want to speed up the rotation--> 


<shape 
android:innerRadiusRatio="30" 
android:shape="ring" 
android:thicknessRatio="80" 
android:useLevel="false"> 

<size 
    android:height="80dp" 
    android:width="80dp" /> 

<gradient 
    android:centerColor="@android:color/transparent" 
    android:centerY="0.50" 
    android:endColor="#E6E6E6" 
    android:startColor="#BEBEBE" 
    android:type="sweep" 
    android:useLevel="false" /> 
</shape> 

</rotate> 

編輯1:

您可以使用進度,更容易實現,並通過控制代碼:

在XML文件中

<ProgressBar 
     android:id="@+id/progressBar" 
     style="?android:attr/progressBarStyle" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_gravity="bottom|center_horizontal" 
     android:visibility="gone" 
     android:indeterminate="true" > 
    </ProgressBar> 

在* java文件

ProgressBar progress = (ProgressBar) findViewById(R.id.progressBar); 
// Control the size 
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(80,80); 
// Control position 
params.gravity = Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL; 
// Set new parameters and visibility 
progress.setLayoutParams(params); 
progress.setVisibility(View.VISIBLE); 
// Set Color 
progress.getIndeterminateDrawable().setColorFilter(0xFFFF0000,android.graphics.PorterDuff.Mode.MULTIPLY); 
+0

感謝名單,但我嘗試使用ValueAnimator和我可以得到一個不錯的圓形問題是當我動畫時它 – Androidy

+0

不客氣,但我可以問什麼ValueAnimator會實現超過對象嗎? –

+0

1.如何實現? 2.我可以動態地控制它嗎?啓動,停止? – Androidy

相關問題