Dynamically changing the border of View - android

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

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.

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

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

Related

Android, make a rather complicated animation?

I know how to make a typical animation using Animation API in Android. But I don't know how to make this:
It is simply a textview inside a ring while the ring is animating and the textview is just in a fix state. Note that the ring thickness is not changing while scaling.
Assuming you have your ring as a view already, you simply create an AnimatorSet with two ObjectAnimators that changes the scaleX and scaleY of the ring view. How far to scale depends on how far you want the ring to extend out or in.
Make sure you set the pivotX and pivotY to be the center of the view.
Then, simply have it repeat.

Change color of a view dynamically depends on what color of background the view is in

In Android, is there a way to dynamically change the color of the view depending on what kind of background it is in?
For example, if I have a floating TextView with a white text color on a ListView with a black background, the text color will be visible clearly. However, as I scroll down, if the next section of ListView has a white background the texts in the TextView won't be visible anymore since it is white on white.
Is there any way in Android to change the text color of this TextView as it reaches the background with similar/same color?
Well, it depends on the type of background.
If you are talking about a background as in a view(linearlayout, surfaceview, etc) wit ha color, you have to get the position the textview has on the screen, and set a color based on the background color at that point. But it depends on the type of color. A bitmap allows you to get a specific pixel:
bmp.getPixel(x, y);
And then you can determine the color based on that.
However, with a SurfaceView it becomes harder because there is no native method for that.
Further, a gradient-filled background makes it harder to get at a particular point. But if you use a view and have set the backgroundcolor, you can:
if (view.getBackground() == Color.RED){//View = the view with a background set using setBackground or android:background="#color or #drawable or whatever"
tv.setTextColor(Color.WHITE); //then you adjsut the textview color
}
Please be aware that the above example only works if the view has a background defined as android:background or setBackground and there is a color. When you use a bitmap, you can get the pixel and the color based on that pixel
not hard stuff
if view.getBackground() == .RED{
yourStaff.text.setTextColor(Color.WHITE);
}

Set background behind image

I got a picture that I want to use.
I set it as following:
ImageView menu = new ImageView(this);
menu.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
menu.setImageResource(R.drawable.menu);
They annoying thing is that I get white pixels on the sides of it cause I want to keep the aspect of the pic.
I can stretch the image by using menu.setScaleType(ScaleType.FIT_XY); but that will make the person on it look really fat. The picture is dark and the pixels are white so they do show quite well. :/
Is there I way I can first apply black color and then the image above that color?
To set a background color to any view on android you can use android:background attribute in layout xml or by calling setBackgroundColor(int id) in java code.
But if you really want just to set the image in bounds you can give a try to android:scaletype="centerCrop"
Using set BackgroundColor will also remove any padding, borders and what not attached to the object.
If that is the result you want, that is a reasonable approach.
If blasting the objects current style will cause problems, I would look to use CSS to set the background color and change the css styles through code if things are happening after page load.
Consider using a border around the image that is colored the way you want to hide what is underneath?

setBackgroundColor() and setBackground() are mutually exclusive in android

I'm trying to make a circle of one color on a background of another.
background = new ShapeDrawable(new OvalShape());
background.getPaint().setColor(main.getResources().getColor(R.color.XXX));
view.SetBackground(background);
will work for the colored circle, and
view.setBackgroundColor(getResources().getColor(R.color.XXX));
will work for the background, but they're mutually exclusive. It just ends up with what I did last. Is there a way to make the circle on another overlapping view or something like that?
setBackgroundColor() is basically a short cut for changing the view's background to a colour drawable.
To do what you want you could try one of the 2 things described below:
Put a view in a FrameLayout, set the background colour in the FrameLayout, and put the shape in the view.
You could also try to use ImageView, which can have a background and another drawable with setImageDrawable() method.

How to force android view to redraw it's background-drawable? / probably an animation is blocking it to work properly

Is there a way to force an android view to redraw it's background?
I have set a drawable shape with a gradient as background which works fine but after changing the view's height you can see those ugly gradient steps.
What can I do?
I tried view.invalidate() and view.refreshDrawableState() without any visible difference.
Thank you!
//EDIT:
Here are some more details and my code:
After your answers I think the banding is a result of my probably bad code. Here is what I'm doing:
Scaling a view
setting its new width and height because the scaled view does not accept user input in other areas then the "old" one (see android weird (at least for me) behaviour of control after scaling / doesn't accept input in it's whole area )
trying to set the background again (which contains a gradient and a rectangle with rounded corners)
Here is my code:
public void onAnimationEnd(Animation animation)
{
MyView view = (MyView) ((ExtendedScaleAnimation) animation).getView();
view.getLayoutParams().width = toWidth;
view.getLayoutParams().height = toHeight;
view.clearAnimation();
view.requestLayout();
view.refreshBackground(); // background will be changed
}
On the device you can see that the background drawable is changed but there is a flickering which seems to me like the backround change is applied before the animation is over. Programatically it should be over! Or am I wrong?
Thank you again!
Can you try after resize:
v.setBackgroundResource(null);
v.setBackgroundResource(...my drawable...);

Categories

Resources