user1357880
user1357880

Reputation:

Dynamically changing the border of View

I am just planning to implement a view whose border color keeps on changing dynamically at run time based on a timer. For example initially the view's border will be in green color(timer=20sec) and every sec the green color disappears and once it goes of more than half(timer=10sec) of the view... the border color should change to orange and once it reaches the end(timer=5sec) of timer it should be in red color and phone should vibrate (easy can be done).... If somebody is familiar with zynga poker its the same that i want to implement.
Now one way of implementing it could be have a backend timer and based on that have "n" number of drawables and keep on changing the background of the view every sec. But this requires many images and i dont think this is the optimal way of implementing it.
just wondering if i can write a custom view and implement the surface view and do something around it so that this can be done. Anybody has any idea on how to achieve this? can this be done using any animations around customviews? Any ideas around this are greatly appreciated.

Thanks in advance

Upvotes: 0

Views: 646

Answers (3)

Renard
Renard

Reputation: 6929

My suggestion is to make use of the animation framework from android. If your app targets versions below honeycomb use NineOldAndroids.

As already suggested set the border by assigning shapeDrawable to your view

view.setBackground(R.drawable.border_style_green);

you start the animator like this. (this code will animate from currentColor to red in 1000ms)

    ObjectAnimator animator = ObjectAnimator.ofFloat(this, "color", currentColor,Color.RED);
    animator.setEvaluator(new ArgbEvaluator());
    animator.setDuration(1000);
    animator.start();

Then in your activity implement:

public void setColor(int color){
            currentColor = color;
    view.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}

Upvotes: 1

Uttesh Kumar
Uttesh Kumar

Reputation: 290

if view is html page : Use Jquery for this implementation. by ajax you can call backend as well. Jquery delay function

Upvotes: 0

nithinreddy
nithinreddy

Reputation: 6197

Run a timertask, and change the border of the view by setting the background value of the view to a style. Like,

view.setBackground(R.drawable.border_style_green);

So in the timer task, keep checking the time, and change the border.

Note - border_style_green is a XML file which has the style where you can set the border, the rounded corner to a view, etc.

Upvotes: 2

Related Questions