2016-01-24 97 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