How can I apply an alpha mask to an Android drawable? - android

In my application, I need to change the background color of a button in response to certain events. That works fine by setting creating a drawable, setting its paint and using that drawable as the button background:
ShapeDrawable drawable = new ShapeDrawable(roundRectShape);
drawable.getPaint().setColor(color);
b.setBackground(drawable);
Now, I want to overlay an alpha mask onto that drawable, creating a "striped" button. Here's how it should look like for a blue and a black button (assuming white as the background color, but these sections should really be 100% transparent):
I made this alpha mask in Inkscape, and successfully imported it as a vector asset. I might need to convert it to a bitmap of sorts, but I'm not sure.
I've read a bunch of articles and SO questions mentioning PorterDuff matrices to apply, but haven't been able to translate these into solving my current problem.
Anyone know how to do this?
Here's another visualization, hopefully making things clearer. The background color of the parent view of the button is pale pink here, and the button border is outlined in red (but should really be invisible in the end product):

//This will help.
ImageView img = (ImageView) findViewById(R.drawable.yourimage);
img.setAlpha(100);
//Transparent is between 0 and 255;
//If you want to use Bitmap
Bitmap b = BitmapFactory.decodeResources(getResources(), R.drawable.yourimage);
img.setImageBitmap(b);
img.setAlpha(100);

So, I figured out how to do this, albeit not using an SVG - too many issues there.
First, I switched from using a Button to ImageButton. Then I applied the following style:
<style name="stripedButton">
<item name="android:layout_gravity">left</item>
<item name="android:layout_height">match_parent</item>
<item name="android:stateListAnimator">#null</item>
<item name="android:clickable">true</item>
<item name="android:elevation">0dp</item>
<item name="android:longClickable">true</item>
<item name="layout_widthPercent">70%</item>
<item name="android:src">#drawable/fade_in_bars</item>
<item name="android:tint">#color/RED</item>
<item name="android:background">#null</item>
<!-- Stretch image to fill entire button. -->
<item name="android:scaleType">fitXY</item>
</style>
fade_in_bars is just the png version of the alpha mask shown in the original question. The color can be set in XML or programmatically using the tint attribute. I don't quite understand why I need to set the background to null, but it looks strange otherwise. Finally, setting the scaleType to fitXY is needed so that all the stripes are shown, regardless of the size of the button on a particular display.
Here's what it looks like:

Related

Make transparent background in xml

I have android scanner project and its totally working to me .I ask and search a lot but it is not the thing that i want. What i want to do is to make my layout background Transparent like this
https://i.stack.imgur.com/jFovP.png
They suggest to me to use :
android:background="#android:color/transparent"
but what happen is it just make my background White(#ffff), you can see here in this picture https://i.stack.imgur.com/gCkoi.jpg my backgroud is Blue but what I'm trying to do is the first picture above .
Thank you in advance for the answer.
If your xml Layout Background is Transparent it's Still not enough to make the Background of an Activity Transparent. You Need to Add these to your Style:
<style name="Transparent" parent = "Theme.AppCompat.Light.NoActionBar">
...
<item name = "android:windowIsTranslucent"> true </item>
<item name = "android:windowBackground"> #color / transparent </item>
...
</style>

Customize Button (ANDROID)

I want to fully customize button in android, with keeping all its juicy material design styles.
Without a custom drawable png for button, just applying a color to button, i am able to change the color of the button and keep the Elevation, Ripple Effect, Rounded corner and it looks nice.
The code i used for this is
<resources>
<style name="AppTheme.NoActionBar">
<item name="android:colorButtonNormal">#8126f3</item>
</style>
</resources>
Then i wanted to put my own drawable into the button and keep the Material Design effects . so i created a drawable (V21) and assigned it as background to the button.
button_ripple.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="#drawable/take_ride_map_btn"/>
</ripple>
But now the problem is that it does not have a elevation. And the ripple effect does not start at the point of touch.
I have looked into this, this, and this SO Question but these are not helpful with using the custom drawable into button.
Please do suggest how can i achieve this -
Elevation while having the custom drawable.
Start the Ripple Effect at the point where user touches.
you can always have the elevation if you set the button to wrap_content both ways i.e height and width and for the ripple effect you should use RippleDecoratorView.
You don't need to cast it to anything just wrap the button inside it in xml and your good to go, plus its highly customizeable too

Actionbarsherlock Spinner background image stretched

I am trying to change the color of the little triangle on the Actionbar spinner (sherlock). I have done so using the following code:
<style name="MyTheme" parent="Theme.Sherlock.Light">
<item name="windowActionBarOverlay">true</item>
<item name="homeAsUpIndicator">#drawable/ic_drawer</item>
<item name="actionDropDownStyle">#style/MyActionBarSpinnerStyle</item>
</style>
<style name="MyActionBarSpinnerStyle" parent="#style/Widget.Sherlock.Light.Spinner.DropDown.ActionBar">
<item name="background">#drawable/actionbar_spinner_background</item>
</style>
The icon triangle is changed but it seems to stretch the image to the whole size of the spinner.
image http://dodsonsolutions.com/screens/screen1.png
I am using the patch-9 drawable file that everyone says to use:
spinner http://dodsonsolutions.com/screens/spinner_background_disabled.png
If this is not the correct dimensions of the file then does anyone know what it should be?
Or how do I get it not to stretch and just sit in the bottom right hand corner.
Thank you.
are you using the image that has correct extension?
check the extension of the png file you are using. if it is not ".9.png", change it into .9.png
or if it is .9.png?
there must be a build error of your project. try to rebuild & run. that must be solved.
1.try to define pixel in the area that you want to re-size not all around the image and check pixel more than 3 mostly declaring 1 pixel not working
2.check your xml file to make sure layout_width and layout_height is wrap_content
3.To use 9patch :the image file extension should be png and while you declare area extension become .9.png

Compound drawable in a TextView is stretching beyond the image size

I am creating a menu in an Android app with several list items and corresponding icons. The problem that I'm having is that the icons are being stretched beyond the image size for some reason. I have created a menu like this before without such a problem, and I essentially copied the code over to this new project, primarily only changing the image names and string names.
Here is the code from the menu layout for a single list item:
<TextView
android:id="#+id/listItemCardTransactions"
style="#style/NavigationMenuItem"
android:drawableLeft="#drawable/ic_card_transaction_menu_icon_normal"
android:text="#string/card_transactions" />
Here is the NavigationMenuItem style:
<style name="NavigationMenuItem">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingTop">#dimen/menu_list_item_padding</item>
<item name="android:paddingBottom">#dimen/menu_list_item_padding</item>
<item name="android:paddingLeft">#dimen/menu_list_item_padding</item>
<item name="android:drawablePadding">#dimen/menu_list_item_left_padding</item>
<item name="android:gravity">center_vertical</item>
<item name="android:clickable">true</item>
<item name="android:background">#drawable/selector_navigation_item</item>
<item name="android:textSize">#dimen/text_size_menu_item</item>
</style>
Here are the dimensions:
<dimen name="text_size_menu_item">18sp</dimen>
<dimen name="menu_list_item_padding">6dip</dimen>
<dimen name="menu_list_item_left_padding">12dip</dimen>
And here is what the menu looks like right now (I know there are several other problems but those aren't my concern right now). You can see the stretching problem best when you drag-and-drop the image to get the full view:
And what the icon is supposed to look like (this one is grey since I wasn't sure how to easily make the white one visible on a white background):
Anyone know what might be causing the image to stretch past its dimensions like this?
I would follow Barry's advise in the comments, ie use an ImageView + TextView inside a LinearLayout for each item. However, to solve the "the height of the list item is smaller", you could try adding the following attribute to the ImageView:
android:adjustViewBounds="true"
If I'm not mistaken, this should effectively make sure that the view bounds is actually based on the content of the image, and not the aspect ratio (ie, it won't change the height beyond the maximum height of the image itself due to "scaling").
Hope that helps (and also makes a bit sense).

Button scales to background image instead of content

What I need to do is have the background image just fill up the natural size of the button, as when no background is specified and the default grey background image is shown. But instead, my button scales up to the size of the background image rather than the text.
There was a previous question, but nobody had a solution.
The button:
<Button
android:id="#+id/morebtn"
style="#style/CustomButton"
android:text="More" />
I have this custom button style (greybutton is a 9-patch):
<resources>
<style name="CustomButton" parent="#android:style/TextAppearance">
<item name="android:textColor">#FFFFFF</item>
<item name="android:layout_marginLeft">10dip</item>
<item name="android:layout_marginRight">10dip</item>
<item name="android:layout_marginBottom">10dip</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_centerInParent">true</item>
<item name="android:background">#drawable/greybutton</item>
<item name="android:textSize">20sp</item>
</style>
</resources>
Right now it fills up the screen widthwise, despite being told to wrap_content. When I remove android:background from the Button style, the button shrinks down to wrap the text as expected.
Does anyone see why my background image isn't behaving like the default Button background?
Why don't you use ImageButton instead:
<ImageButton android:src="#drawable/greybutton"
android:scaleType="fitCenter" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
then you can adjust the scaling with the scaleType attribute.
The only downside is you can't set any text.
Damnit. I had an hdpi drawable that hadn't been converted to 9patch, and for some reason that was getting applied instead of the one in the mdpi drawables folder even on my mdpi screen. The background scaling actually does work as expected after all. Sorry!
(So if you're browsing this question with a similar problem, check your drawables folder to make sure you're drawing what you think you are. Also: may wish to be sure the content boundaries in your nine-patch are of the appropriate size. Note that they aren't the same as the stretchable boundaries.)
You could try hard coding the height and width of the buttons using dip. That will be an issue if you need the Text on the buttons to change but it should work to restrain the button from expanding to the size of the image. Of course a large image will make the button larger.. the width / height is wrap_content, and the image is clearly part of the content... try hardcoding width / height parameters.
Add these two items to your CustomButton style
<item name="android:minWidth">0dp</item>
<item name="android:minHeight">0dp</item>

Categories

Resources