像ScreenSaver一样改变渐变的颜色?

时间:2013-11-19 06:26:29

标签: android android-layout colors linear-gradients

你好朋友我想改变像图像下方的渐变颜色。我试了很多天但没有运气。你能帮助我吗?提前谢谢!

enter image description here

我可以使用渐变LinearLayout绘制颜色,但我想在运行时更改此颜色。

2 个答案:

答案 0 :(得分:5)

这可以通过逐帧动画来实现。在你的活动中写下代码:

 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity_layout);

    ImageView animation animation = (ImageView)findViewById(R.id.imageAnimation);

    animation.setBackgroundResource(R.drawable.anim_fbyf);  
 }

 @Override
 public void onWindowFocusChanged (boolean hasFocus)
 {
    super.onWindowFocusChanged(hasFocus);

    AnimationDrawable frameAnimation = 
        (AnimationDrawable) animation.getBackground();

    if(hasFocus)
        frameAnimation.start();
    else 
        frameAnimation.stop();

 }

在名为 anim_fbyf.xml 的可绘制文件夹中制作1 xml

 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
     android:oneshot="false">

<item android:drawable="@drawable/frame0" android:duration="350" />
<item android:drawable="@drawable/frame1" android:duration="350" />
<item android:drawable="@drawable/frame2" android:duration="350" />
<item android:drawable="@drawable/frame3" android:duration="350" />
<item android:drawable="@drawable/frame4" android:duration="350" />
<item android:drawable="@drawable/frame5" android:duration="350" />
<item android:drawable="@drawable/frame6" android:duration="350" />
<item android:drawable="@drawable/frame7" android:duration="350" />
<item android:drawable="@drawable/frame8" android:duration="350" />
<item android:drawable="@drawable/frame9" android:duration="350" />
<item android:drawable="@drawable/frame10" android:duration="350" />
<item android:drawable="@drawable/frame11" android:duration="350" />
<item android:drawable="@drawable/frame12" android:duration="350" />
<item android:drawable="@drawable/frame13" android:duration="350" />

 </animation-list>

您可以根据需要设置持续时间并添加任何帧数。

我添加了13帧(或gif图像)并将持续时间设置为350毫秒。

<强>输出:

下面的

是具有13帧的图像

sunset

答案 1 :(得分:0)

你可以试试这个:

int h = v.getHeight();
ShapeDrawable mDrawable = new ShapeDrawable(new RectShape());
mDrawable.getPaint().setShader(new LinearGradient(0, 0, 0, h, Color.parseColor("#330000FF"), Color.parseColor("#110000FF"), Shader.TileMode.REPEAT));
v.setBackgroundDrawable(mDrawable);

在getView()重载函数(ListAdapter)