It is possible to create contour around or shadow under VISIBLE pixels in ImageView.
In case this image in ImageView (outside from green figure is transparent):
I need next pic with border:
Please any code or references.
Dunno you just want to add a color as stroke to the background then you can do like this
If you are using color then you can create an xml file in the drawable in the res of your project
and create a drawable like this
Image.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="2dp"
android:color="#007cea" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
<solid android:color="#007cea" />
<corners android:radius="4dp" />
</shape>
And to your imageview you can call the xml file as
android:background="#drawable/login_button"
In the xml file you specify like this
<stroke
android:width="2dp"
android:color="#007cea" />
the android:color="#007cea" defines which color you want to give as border
I don't think you will find exactly what you want.
If it was drawable possibly you could use layer-list option.
But it won't work with Imageview.
Related
All the view classes on Android are define in rectangle shape.When you touches it detects in the rectangle zone,when you set image it fills the rectangle.How to make a class which is round by default?
Features I am looking for:
Support all the ImageView functions
Square Image input with setImageBitmap or onDraw are converted to round.
Dectect circular touch zone instead of sqaures
Small in size and overhead.
3rd Party is fine but prefer just use interal library.
Create a shape inside your drawable package.
// res/drawable/circle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:useLevel="false" >
<solid android:color="#android:color/transparent" />
<stroke
android:width="10dp"
android:color="#android:color/white" />
</shape>
Then create a layer list inside the drawable
// res/drawable/img.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/circle"/>
<item android:drawable="#drawable/ic_launcher"/>
</layer-list>
Then create the image view with layer list
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/img"/>
How can I create this exact drawable in java code only?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="1dp" />
<solid android:color="#ff0000" />
<padding
android:bottom="-15px"
android:top="-5px" />
</shape>
I need the padding values to be variable so I can't do it as an xml.
If you wanna draw something custom, you should open a Canvas and make your drawing there.
Read this:
https://developer.android.com/training/custom-views/custom-drawing.html
And it seems a simple shape. So you can add a layout view (View,TextView, ImageView, etc.) set background with this drawable and adjust padding through this view.
I recently did some test on android drawable and I found that the android:src behaviors to padding is extremely weird compare the android:background.
Here is my drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/colorAccent"/>
<padding
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
android:left="1dp"
/>
</shape>
</item>
<item android:top = 2>
<shape android:shape="oval">
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>
Inside of the mainActivity layout I have another image view which I used to test. I set the image view to 40dp width and height. Then I tested the android:src vs the android:background attribute by setting them to my drawable above. Here is the result:
Android:src
Android:background
The android:background as you can see is the expected behavior because I move the white circle down 2 and it has padding that obeys the pink oval padding. Now the android:src is straight up unexpected because 1st of all the white oval which is the layer that is above the pink oval is not shown. Second of all, the shapes change to a flatten oval.
Any clue to why this is the case??????
src works with android:scaleType where you can give different options ScaleType
whereas background always takes scaleType of FitXY covering full region for that view.
if you want the same behaviour as background with src,
put android:scaleType="FitXY". there are other options you should check also.
I Try to center a drawable bitmap within a shape with padding, using layer-list.
But i can't get padding around the image.
THis is not working:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="270"
android:centerColor="#ffffffff"
android:endColor="#ffbab8b9"
android:startColor="#ffcfccce" />
<stroke
android:width="1px"
android:color="#A0A0A0" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
</item>
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp">
<bitmap android:src="#drawable/logo"
android:gravity="center" />
</item>
</layer-list>
Does someone have an idea how to center a bitmap (for scaling) with padding?
Right now the bottom,left,... parameters are just ignored. The bitmap is scaled 100% within the Button.
I need it for a Button and can't use an ImageButton here.
Also the Button don't have text in that special case, so compoundDrawable will also not work. I need to do it with a Button.
Best regards,
Juergen
Ok, looks like theres no way using padding of a bitmap within layer-list. I just decreased the bitmap size and now it fits in the button perfectly. But i don't know if it is looking same sized on every device...
You can put icon straight in item element, like this:
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp"
android:drawable="#drawable/logo"
android:gravity="center"/>
In generally android buttons are rectangular and on the mxl file what should I do if I want to make ring shaped or any other shaped button? or is there any tool that assure these to android developers which is open source (for now I'm seeking a ring-shaped button ).
create a new xml file inside your drawables, and put this code:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="15dp"
android:thickness="10dp"
android:useLevel="false">
<solid android:color="#fff" />
<size
android:height="50dp"
android:width="50dp" />
</shape>
now, use this as your image in an imageButton
You can also take a look at this:
android-state-list-generator