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.
Related
I have an ImageView with the source being an ImageAsset.
My image is a circle with a plus and I am looking to colour the inside of the circle only. How do I do that?
Using setBackgroundColor colours the whole of the view thus giving a square background like this:
.
You can apply a color tint to your ImageView. This will not affect your background if it's transparent, just the colored part (which is the circle and the plus)
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR),
android.graphics.PorterDuff.Mode.MULTIPLY);
Update
Using android.graphics.PorterDuff.Mode.SRC_IN will also work (if you don't need to multiply the source and destination pixels)
Update 2
Since the destination part is not colored than the best solution is to use VectorDrawable.
You can use VectorChildFinder to find inner parts of your SVG resource and change its color.
VectorChildFinder vector = new VectorChildFinder(this, R.drawable.my_vector, imageView);
VectorDrawableCompat.VFullPath path1 = vector.findPathByName("path1");
path1.setFillColor(Color.RED);
imageView.invalidate();
to create your SVG, follow these steps :
Just click right button on folder(drawable for ex.) and choose:
then choose:
Android SDK provides the following icons.
Is there a way to set a color to those .. and if possible, how to do so?
<ImageView
android:id="#+id/share_button"
style="#style/IconNav"
android:src="#android:drawable/ic_menu_share"/>
<ImageView
android:id="#+id/bookmark_button"
style="#style/IconNav"
android:src="#android:drawable/ic_input_get"/>
UPDATE
After doing a complete refresh of the project, it turns out the tint attribute in Xml did the trick.
For the short answer
.. this is the solution that worked for me - adding the property to the ImageView xml:
android:tint="#color/grass_dark"
The answer from #goldenb is a thorough run through of the different ways to solve for this, so am marking that one as the answer.
You can indeed use a tint as a way of changing an ImageView's colour, BUT you should be reminded that the android:tint will always be applied on top of the original colour.
as stated by blogger danlew
ImageView's tint mixes the tint color with the original asset. What you want is for the tint color to take over entirely; instead it
applies the tint on top of the existing color. So, for example, if the
source asset is black, and you want it to be #77FFFFFF (a translucent
shade of white), you'll actually end up getting that shade of white
with a black background beneath it.
android:tint is limited to ImageView. You want to be able to tint any Drawable in any View.
One possible alternative would be for you to use android ColorFilter
According to the official documentation:
A color filter can be used with a Paint to modify the color of each pixel drawn with that paint. This is an abstract class that should never be used directly.
There are lots of more or less complex things you can do with ColorFilter but how can you apply this then?
One simple example from another so question is:
//White tint
imageView.setColorFilter(Color.argb(255, 255, 255, 255));
or
imageView.setColorFilter(ContextCompat.getColor(context,R.color.COLOR_YOUR_COLOR))
Or a more complete answer here in SO from here
ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);
// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );
You should check these links if you want to learn more
SO - What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?
setColorFilter()
Fast Android asset theming with ColorFilter
SO-Modifying the color of an android drawable
You can use a bitmap with a tint. Add this to your drawables folder.
ic_input_get_colored.xml :
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#android:drawable/ic_input_get"
android:tint="#color/yourDesiredColor"/>
From this developer guide about Layer-list drawables,
All drawable items are scaled to fit the size of the containing View,
by default. Thus, placing your images in a layer list at different
positions might increase the size of the View and some images scale as
appropriate.
In the first sentence, they say that the items are scaled to fit the
container view (and Not that the view is scaled according to the
size of the items contained in it). Then they say that the size of
the container view might increase (which means that the View is
being scaled, right?). So doesn't the second sentence contradict
the first one? Can somebody explain what is meant there?
android:drawable
Drawable resource. Required. Reference to a drawable resource.
...
To avoid scaling items in the list, use a
element inside the element to specify the drawable...
...
For example, the following defines an item that scales to
fit its container View:
<item android:drawable="#drawable/image" />
To avoid scaling, the following example uses a element with centered gravity:
<item>
<bitmap android:src="#drawable/image"
android:gravity="center" />
</item>
Again, they say that android:drawable is a required attribute, and then they give an example which does not use this attribute. What is correct?
To avoid scaling items in the list, use a <bitmap> element inside the
<item> element to specify the drawable and define the gravity to
something that does not scale, such as "center"
How is gravity scalable and how is center as its value make it unscalable?
It seems that layer-list items are effectively stretched in both X & Y dimensions to fit the container.
You need a drawable for each item. The bitmap child element effectively becomes the drawable for that item.
If you have an empty item (with no drawable resource), it will cause an error when you try to load the layer-list.
Gravity has several options (like FILL, which is probably the item's default gravity, or FILL_HORIZONTAL that stretch the item to fill its container).
Additionally, you can set android:gravity="center" on an item tag itself (without a <bitmap> child) and it seems to have the same effect (in API 23, at least).
In addition to what #Cristopher_Boyd said. The screen density is also important, so different bitmaps should be created for different screen densities. If there is a single image inside the drawable folder directly, it may not scale properly. Hence, generate different images, or place a single image in drawables-xxhdpi folder, for example (but not on drawables). See this answer.
Hope this helps someone,
Xavi
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?
I was just wondering if there was a way to change the opacity of the background image for a View (ie. TextView, etc.).
I know that I can set the background image like this:
android:background="#drawable/my_drawable_image"
Or I can set a specific background colour with an alpha setting like this:
android:background="#10f7f7f7"
Is there a way I can control the opacity (set the alpha) if I'm setting the background as a drawable image? And I want to do this in the XML Layout. I already know that I could grab the Drawable object and programmatically set the alpha, but I want to see if I can do it in the layout.
I ended up just going with the programmatical solution, since it doesn't look like it can be done via the XML layouts.
Drawable rightArrow = getResources().getDrawable(R.drawable.green_arrow_right_small);
// setting the opacity (alpha)
rightArrow.setAlpha(10);
// setting the images on the ImageViews
rightImage.setImageDrawable(rightArrow);
This might make your Work simpler
View backgroundimage = findViewById(R.id.background);
Drawable background = backgroundimage.getBackground();
background.setAlpha(80);
Alpha Values 0-255, 0 means fully transparent, and 255 means fully opaque
from: This Answer
You can also use XML to change the transparency:
android:alpha = "0.7"
The value of alpha ranges from 0 to 1
You can embed the image in xml, so you'll be able to see it in the Graphical Layout
<LinearLayout
style="#style/LoginFormContainer"
android:id="#+id/login_layout"
android:orientation="vertical"
android:background="#drawable/signuphead">
And change the code like this to make it transparent:
Drawable loginActivityBackground = findViewById(R.id.login_layout).getBackground();
loginActivityBackground.setAlpha(127);
The answer you gave didn't exactly answer the question you asked. Here's what I did.
Drawable login_activity_top_background = getResources().getDrawable(R.drawable.login_activity_top_background);
login_activity_top_background.setAlpha(127);
LinearLayout login_activity_top = (LinearLayout) findViewById(R.id.login_activity_top);
login_activity_top.setBackgroundDrawable(login_activity_top_background);