Layout Background partially transparent - android

I want to set a transparent background. Maybe 20% of gray
but i also want to have a small part of the background being completely transparent (a rectangle or a circle in the layout):
Is that possible? later i want the rectangle (or the circle) to be moveable and scaleable.

This is easily achievable with a custom drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="rectangle"
android:thicknessRatio="1.9"
android:useLevel="false">
<solid android:color="#android:color/transparent"/>
<stroke
android:width="125dp"
android:color="#CC000000"/>
</shape>
When you overlay that drawable on top of an image the results would looks something like this:
By creating that drawable dynamically in code you can adjust it at runtime. For example:
ShapeDrawable drawable = new ShapeDrawable(new RectShape());
drawable.getPaint().setColor(0xCC000000);
drawable.getPaint().setStyle(Paint.Style.STROKE);
drawable.getPaint().setStrokeWidth(dpToPixel(125.0f));
someView.setBackground(drawable);
You can influence the shape of the hole in the middle by combining multiple layers in a LayerDrawable.
You can also influence the general shape of the hole by setting the android:shape tag of the <shape /> element in the custom drawable.

Related

How to set background color of an image, but within the border?

My goal is to display a circular image, and allow the user to set its foreground and background colors respectively, for example:
My attempt was to create an image asset with transparent background, then use ImageView::setColorFilter to change its foreground, and use ImageView::setBackgroundColor to set its background. The image asset looks like this:
My problem is that pixels outside of what we humans call 'border' are also transparent, so the result looks like this:
How do people usually deal with this issue? Although I was doing Android development, but any ideas or code snippets in any language are appreciated!
at the first, you must create a new drawable file and write below code for creating the circle with yellow color with blue border:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#cab816" />
<size android:width="50dp"
android:height="50dp"/>
<stroke android:width="2dp"
android:color="#color/darkBlue"/>
</shape>
and now you must set this drawable to the background of your imageview and set your image with src.
if your image covered all of this circle, use padding for decrease image size.

How to reuse drawable

I have a number of background drawables that the only different is the color of the border or the color of the background.
Is there a way to define a general drawable and then add (or change) the attribute I need.
For example here is a rectangular drawable
XML file
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="#dimen/radius_small"/>
<solid android:color="#color/simple_black" />
<stroke android:width="2dip" android:color="#color/simple_white" />
<padding
android:left="#dimen/two_dp"
android:right="#dimen/two_dp"/>
</shape>
and I would like to change the solid color or the stroke color, and not to create a separate drawable for each one.
This assumes that you want to do it programatically. If all you need is to change the background and/or stroke color, here is how you can do it:
Obtain a reference to the shape
GradientDrawable drawable = (GradientDrawable)context.getResources().getDrawable(R.drawable.shape_id)
To change color
drawable.setColor(Color.RED)
To change stroke color and width (notice that you can only change both of them together, so I recommend keeping a variable with the stroke's width in px)
drawable.setStroke(Util.dpToPx(context, 2), Color.YELLOW)

Android 9 patch images are only useful for single coloured square shape images?

Android 9 patch images are only useful for single coloured square shape images. I have button with circle shape,
how to use 9 patch for this image?
You need to create a shape drawable in the drawable folder that looks something like:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
android:angle="270"/>
</shape>
Modify color code that you want.
(For this example I have saved the drawable as circle.xml and it will have a gradient fill)
Then in your layout you need to define a view and set the shape as the background:
<View android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/circle"/>
The View defines the size and dimensions of the shape.
Edit - Screenshots of the result of the code

Android enlarge transparent background of icon drawable

I have many different dynamic icons in my Android app and I would like to put a rectangular frame around all of them. I tried to use LayerDrawable but I think it scales the smaller drawable to the size of the larger one so in the end the icons overlap with the frame instead of within it. (the icon drawables are 64x64 while the frame drawable is 96x96). Is there an way to enlarge the transparent background of the icon drawables to the same size as the frame drawable without scaling the actual icon?
Any help would be appreciated!
You should be able to use a LayerDrawable for this; just tested it myself and it seems to work just fine. Define the frame first, then add another item with the specified insets.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000"/>
</shape>
</item>
<item android:left="20dp" android:top="20dp" android:bottom="20dp" android:right="20dp">
<bitmap android:src="#drawable/ic_launcher"/>
</item>
</layer-list>

Border texture for a View

Is there any way to set border texture for LinearLayout or any other View? I can't just set an image as the background because of different screen sizes.
Here is the sample of the texture I'm talking about:
I tried to use Nine-patch, but it stretches the white lines that are supposed to be repeated.
Screenshot from Draw 9-patch tool:
You can use shape drawable like this:
<shape android:shape="rectangle">
<solid android:color="#FAFAD2" />
<stroke android:width="1sp" android:color="#000000"
android:dashWidth="7sp" android:dashGap="5sp" />
</shape>
and this will result you something like this:
You can change color, width, border etc according to your requirements.

Categories

Resources