How can I do this? There's a setAlpha but no getAlpha.
Here ya go View.ALPHA.get(View)
You can use this with an if statement to get a Boolean value and check for the current alpha state of a view.
In case anyone just need get and set a controls visibility, getVisibility and setVisibility may be an alternative you can use:
if(vw.getVisibility() == View.VISIBLE)
{
// do something
}
and you can set the visibility:
vw.setVisibility(View.INVISIBLE);
There is no easy way to do this. This is because an ImageView might have been set with a Bitmap, a StateListDrawable, a ColorDrawable, or something else entirely. Only one of those classes has a single color; on any other drawable the alpha might be different for each pixel (pixels are in ARGB format, w/ 1 byte each for alpha, red, green, and blue). I'm pretty sure the setAlpha() method only works on drawables that support it, as mentioned by Sephy above.
What exactly do you need to know about the image's transparency? If you know what the ImageView will be filled with ahead of time, then perhaps you can extract the alpha beforehand. If you don't have direct access to the alpha, but you are able to determine the color, then the alpha will be equal to color >>> 24.
Actually, you need to use getOpacity() because get alpha existe only for ColorDrawable, Transformation and a few others. and you need to use it on the drawable of the ImageView not the view itself.
You can't directly. You can workaround, provided you're setting the alpha programmatically, is to keep the alpha value when you set it. E.g.:
private int mCurrentAlpha;
private void setAlpha(int newAlpha){
mCurrentAlpha=newAlpha;
ImageView imageView=...
imageView.setAlpha(mCurrentAlpha);
}
Related
I have problem with padding in StateListDrawable.
If for some in my styles i define reference on some <selector> with image resources, it set some wrong padding for my 9path images. By the way i set particular image - all is ok. But otherwise, android create StateListDrawable for my <selector> and (as i saw by using debugger on sources) it get padding by use method:
Rect getConstantPadding();
and return wrong values (in my case it not null or 0).
This method use mVariablePadding variable:
if (mVariablePadding) {
return null;
}
But i can't set it false in resources (maybe i did something wrong).
Does someone know solution for this problem? Thanks!
The problem was in 9-path images. For selector it calculate padding from right-bottom border (content) of nine-path drawable.
Beside there is variables in selector android:variablePadding, that calculate that padding (choose the biggest, etc). So it can be still non 0, even if in mostly cases images doesn't have padding.
Say I have this code:
int color = tv.getCurrentTextColor();
How would I change only the Alpha on this color?
For Example:
if color is 0x00ffffff, how would I change it to 0xffffffff ?
Is there a method for that or I need to do some hex/int manipulations?
Well, the straight answer to your question of "if color is 0x00ffffff, how would I change it to 0xffffffff ?" to simply to use compound bitwise or:
color |= 0xff000000;.
But I reckon maybe some co-workers would rather you didn't do this kind of thing on the java-side of Android development: you have a wonderful helper class called graphics.Color provided by Android! It does just the same things underneath, but'll really help your code readibility, especially considering that an int type color in android isn't necessarily a hex color value but could also point to an artbirary id of a resource in the color xml. Argh.
An eg would be:
int color = tv.getCurrentTextColor();
int newColor = Color.argb(yourNewAlphaVal, Color.red(color), Color.green(color), Color.blue(color))
Not fast, but I'm guessing you don't need it to be.
Of course, this is a personal opinion about what to use here, and I hope this helps!
Short question:
Suppose I have a TextView, and I wish that the text itself (not the backround) will have a drawable set to it (and not just a solid color), how should I do it?
The only solution I've found is this one , but it's only for gradient colors, and I wish to be able to use any drawable.
Ok, I think I've found a possible solution, but I have some notes about it:
It's not for any drawable. It's for a bitmap, so you will need to somehow convert it to a bitmap.
It requires that you know the size of the textView (which you can use this for this purpose)
It requires that you have enough memory for the scaled bitmap that is in the same size of the textView.
Not sure how to make it work per character or per line.
Here's the code:
public static void setBitmapOnTextView(final TextView tv, final Bitmap bitmap) {
final TileMode tile_mode = TileMode.CLAMP;
final int height = tv.getHeight();
final int width = tv.getWidth();
final Bitmap temp = Bitmap.createScaledBitmap(bitmap, width, height, true);
final BitmapShader bitmapShader = new BitmapShader(temp, tile_mode, tile_mode);
tv.getPaint().setShader(bitmapShader);
}
I hope that there is a better solution for this.
edited:
I don't believe it's possible to change the actual stroke used on the text, the best closest option you probably have is to use a different font.
In android you can "draw" any text using any *.ttf font.
for that you must include the file in your assets folder and call from code (maybe there's a way using XML, but I don't know how) this:
TypeFace mFont = TypeFace.createFromAsset(mContext.getAssets(), "my_font.ttf");
mTextView.setTypeFace(mFont);
I reckon that is the closes you'll get to have a different stroke on the text.
original answer:
TextView have the following properties:
Drawable Top
Drawable Bottom
Drawable Left
Drawable Right
that you set on the XML. Or you can call
setCompoundDrawables(left, top, right, bottom)
from code!
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
So I am trying to adjust the alpha of a view programmatically. As of API 11 there is View.setAlpha(alpha) which works great. My app otherwise supports back to API 4 so is there another way to set the alpha for a view?
Apply an AlphaAnimation to the view.
Try View.setBackgroundColor with a color that includes transparency as the first set of hex values before the RGB values (for fully visible red that would be #FFFF0000 - FF for alpha and FF0000 for RGB).
How to set background color of a View