3

hello friends i want to change color of gradient like below image. m trying from so many days but no luck. can you help me? Thanks in advance!

enter image description here

i can draw a color with gradient LinearLayout but i want to change this color at runtime.

Maul
  • 1,179
  • 6
  • 13

2 Answers2

5

This can be achieved by frame by frame animation.In your activity write below code:

 @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();

 }

make 1 xml in drawable folder named as anim_fbyf.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>

you can set duration and add no of frames as per your need.

I have added 13 frames(or gif images) and set duration to 350 milliseconds.

output:

below is the image having 13 frames

sunset

Community
  • 1
  • 1
TheFlash
  • 5,997
  • 4
  • 41
  • 46
  • your solution work for me but its get jerking hoe can i stop that jerk? i want smooth effect @Indiandroid – Maul Nov 20 '13 at 11:03
  • @Maul can't get you...do you mean it is not animating smoothly ?If that is the case then you can increase the duration and also change your images. – TheFlash Nov 20 '13 at 11:06
  • 1
    you can set the duration to minimum for more smoothing he mentioned already in his answer @Maul – TheLittleNaruto Nov 20 '13 at 11:08
0

You can try this:

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);

On getView() overloaded function (ListAdapter)

kai
  • 534
  • 4
  • 12