I read about app:srcCompat on stackoverflow and also from other resources that the main purpose of this attribute is to support for vector-drawable. But I got confused when android:src is also working fine with vector-drawable.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M10.18,9"/>
<path
android:fillColor="#FF000000"
android:pathData="M21,16v-2l-8,-5V3.5c0,-0.83 -0.67,-1.5 -1.5,-1.5S10,2.67 10,3.5V9l-8,5v2l8,-2.5V19l-2,1.5V22l3.5,-1 3.5,1v-1.5L13,19v-5.5l8,2.5z"/>
</vector>
I saved the above code in abc.xml file in drawable directory, then I use it with android:src
<ImageView
android:id="#+id/imageView"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/abc" />
It is working fine. Then where app:srcCompat is helpful or is there any difference between them? or I understand it wrong?
Yes, there is a difference. There is an article here about it.
Basically app:srcCompat will work on older APIs when loading up vector drawables. You’ll find directly referencing vector drawables outside of app:srcCompat will fail prior to Lollipop, you are probably running the android:src on Lollipop or higher.
Related
I have generated a vector asset (based on an own svg file) via Android Studio.
The layout code of my image view:
<ImageView
android:id="#+id/iv_drink"
android:layout_width="21dp"
android:layout_height="30dp"
android:layout_marginStart="24dp"
android:layout_marginBottom="4dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/ic_smoothie_icon" />
The issue is, that this small version of the image looks "curvy" or blurry. Below is a screenshot with increased size (so you can see the cury edges of the images).
My compileSdkVersion is 28 and minSdkVersion is 21.
I tried to solve this with the use of the following:
imageview: android:scaleType="fitXY"
imageview: app:srcCompat instead of app:src
build.gradle: vectorDrawables.useSupportLibrary = true
tried different sizes within the asset xml
What could cause this issue?
EDIT:
This is the content of the svg (image is based on a file from freepik.com):
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="Ebene_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="-318 422.4 85.6 122.6" style="enable-background:new -318 422.4 85.6 122.6;" xml:space="preserve">
<g>
<path d="M-232.4,445l-6.1,100h-49.9l-6.1-100h20.7c-0.6-5.7-1-9.9-1.1-11.7c-0.3-4.8-1.6-6.1-2.1-6.5c-0.4-0.3-0.9-0.6-0.9-0.6
l-0.3-0.2H-318v-3.1c0,0,14.6-0.5,26.9-0.5c8.4,0,15.8,0.2,16.8,1c2.3,1.8,3.7,4.8,4,9.8c0.1,1.8,0.5,6.1,1.1,11.8L-232.4,445
L-232.4,445z"/>
</g>
</svg>
Vector xml:
<vector android:height="12.3dp" android:viewportHeight="123"
android:viewportWidth="86" android:width="8.6dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#000000" android:pathData="M85.6,22.6l-6.1,100h-49.9l-6.1,-100h20.7c-0.6,-5.7 -1,-9.9 -1.1,-11.7c-0.3,-4.8 -1.6,-6.1 -2.1,-6.5c-0.4,-0.3 -0.9,-0.6 -0.9,-0.6l-0.3,-0.2L0,3.6v-3.1c0,0 14.6,-0.5 26.9,-0.5c8.4,0 15.8,0.2 16.8,1c2.3,1.8 3.7,4.8 4,9.8c0.1,1.8 0.5,6.1 1.1,11.8L85.6,22.6L85.6,22.6z"/>
</vector>
Here i have made your vector using more scale friendly dimension and angles and remember that we can't draw half a pixel there will be anti-aliasing involved but we can mitigate by tweaking out shapes. when working with vector and animated vectors you can use shape-shifter to edit your path and make it android friendly or even animate it.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M9,21L7,6L12,6C12,6,12.013,4.071,12,3.5C,11.987,2.929,12.081,3,11.5,3,L4,3L4,2C4,2,11.505,2.006,12,2,C12.26,2.015,12.506,2.125,12.691,2.309,C12.875,2.494,12.985,2.74,13,3L13,6L18,6L16,21Z"/>
</vector>
I think you're running up against the limits of how smooth a sloped line can look, at low resolution. The blurriness is due to anti-aliasing, which is an attempt to smooth out the jagged edge that would result from using only black or white pixels.
If you want to reduce the "curvy" effect, you can try changing the slope of the sides of the cup. Since your cup sides are almost vertical, there are a few large "steps" on each side, so the curviness is more noticeable. Make them completely vertical, or more slanted. A vertical line will have no steps. A more slanted line will have more steps, but smaller ones, so they should be less bumpy.
I think your problem is because of scaleType. Of course your vector width and height have problem too. Please try this:
<ImageView
android:id="#+id/iv_drink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginBottom="4dp"
app:srcCompat="#drawable/ic_smoothie_icon" />
On my side moving to ConstraintLayout instead of using LinearLayout or FrameLayout removes the blurry quality on vector drawable.
For my Android app I've created such layer list from two vector Drawable.
The xml-code
I want to put this image into ImageView, but I have a problem with paddings which I can't remove (marked in red).
To solve this issue I've already applied follow steps:
Explicitly set paddings to 0 value
Set "AdjustViewBound" to true
Played with "fitType" attributes.
None of this hadn't been worked for me. Please help me to resolve this problem.
You should get a whole drawable with the circle and person in one vector. But if you really need these two files to be separated, you must change the path in the XML code.
Accordingly to this
https://stackoverflow.com/a/50114171/3368784 you can resize your path by rewriting your primary_dark_color_circle.xml
as:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<group
android:pivotX="12"
android:pivotY="12"
android:scaleX="1.2"
android:scaleY="1.2">
<path
android:fillColor="#0088CC"
android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0" />
</group>
</vector>
I'm trying to figure out how to create this background in xml,
couldn't find anything that helped me.
Ignore the circle image of the kid and the text of course, I am talking about the background itself, is there a way to create something like that in xml?
Thanks in advance for any help.
cheers!
I'm guessing you moved on, but you can do this using Vector Drawables.
The following is not accurate (the line is not completely straight), but should give you a good start:
New XML file:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="100dp"
android:width="100dp"
android:viewportHeight="100"
android:viewportWidth="100">
<path
android:name="dark_triangle"
android:fillColor="#color/blue"
android:pathData="M 0,100 L 0,80 60,70 100,80 100,100 z" />
<path
android:name="light_triangle"
android:fillColor="#color/color_light_blue"
android:pathData="M 60,70 L 100,65 100,80 z" />
</vector>
In your layout:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
app:srcCompat="#drawable/test"
/>
You might need to add this to your layout as well:
xmlns:app="http://schemas.android.com/apk/res-auto"
Notes:
Some more resources if someone will be interested: 1 2.
There's a preview for Vector Drawables in Android Studio.
Vector Drawables has been introduced in API 21, but should be supported in older APIs.
There TextView and I want him to do VectorDrawable background. I do vector.xml
<vector android:height="24dp" android:viewportHeight="114.0"
android:viewportWidth="494.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFF"
android:pathData="M12,86.61L2.19,76.51L12,67.18L12,8C12,3.58 15.59,0 20.01,0L483.99,0C488.42,0 492,3.58 492,8L492,102C492,106.42 488.41,110 483.99,110L20.01,110C15.58,110 12,106.42 12,102L12,86.61Z"
android:strokeColor="#00000000" android:strokeWidth="1"/>
</vector>
and this VectorDrawable on android 4.4 has a fuzzy edge (not clear), but on android 5.0+ everything looks good (as needed).
Help solve the problem with android 4.4
Use this when using ImageView
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_add" /> <- This may help
Vector Drawables are also supported in cases like TextView's drawableLeft property
(with custom Textview to handle vector for PreLollipop version).
I'm trying to import some icons from Material Vector package in Vector Asset Studio.
But they come with padding.
Why does this happen and how can I remove it?
This is inconvenient because this means if I want my icon to be 17dp x 17dp in XML, then I need to set it more than 17x17 to make up for the padding.
Android Vector Asset
You are able to scale a vector that will remove additional space. This is possible using group tag. Just modify your vector xml file.
From
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,4l-1.41,1.41L16.17,11H4v2h12.17l-5.58,5.59L12,20l8,-8z" />
</vector>
to
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<group
android:pivotX="12"
android:pivotY="12"
android:scaleX="1.5"
android:scaleY="1.5">
<path
android:fillColor="#FF000000"
android:pathData="M12,4l-1.41,1.41L16.17,11H4v2h12.17l-5.58,5.59L12,20l8,-8z" />
</group>
</vector>
As result
You can adjust for any "implicit" padding that may be contained within a VectorDrawables source image (.SVG, .PSD) by setting your ImageViews android:scaleType to the appropriate value so it can handle the padding that is secretly contained in the VectorDrawables source image. You will also need to set android:adjustViewBounds="true".
For example, lets say your VectorDrawable has some really annoying padding at the start of the image when you display it. You have no idea why it's there because you aren't setting any android:paddingStart on the ImageView... what you need to do is set the ImageViews android:scaleType to fitStart and android:adjustViewBounds to true.
tl;dr
Adjust your ImageViews android:scaleType to handle any "implicit" padding that is contained in your VectorDrawables source file (.SVG, .PSD). Also set android:adjustViewBounds="true".
Quick Example:
<ImageView android:id="#+id/vectorDrawable_imageView"
<!--Other ImageView settings-->
android:adjustViewBounds="true"
android:scaleType="fitStart"
app:srcCompat="#drawable/vector_with_implicit_padding_at_start"
/>
This will remove that annoying "implicit" padding that was at the start of your VectorDrawable.
Note: Adjust the android:scaleType according to your rendering needs.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="28"
android:viewportHeight="28">
<group
android:translateX="2"
android:translateY="2">
<path
android:fillColor="#8A333333"
android:pathData="M13.12,2.06L7.58,7.6c-0.37,0.37 -0.58,0.88 -0.58,1.41V19c0,1.1 0.9,2 2,2h9c0.8,0 1.52,-0.48 1.84,-1.21l3.26,-7.61C23.94,10.2 22.49,8 20.34,8h-5.65l0.95,-4.58c0.1,-0.5 -0.05,-1.01 -0.41,-1.37 -0.59,-0.58 -1.53,-0.58 -2.11,0.01zM3,21c1.1,0 2,-0.9 2,-2v-8c0,-1.1 -0.9,-2 -2,-2s-2,0.9 -2,2v8c0,1.1 0.9,2 2,2z" />
</group>
</vector>
android:viewportWidth += android:translateX * 2 (padding start / end)
android:viewportHeight += android:translateY * 2 (padding top / bottom)
This padding is on some icons so that all of the icons can align properly. For example, if in that dialog, you pick ic_3d_rotation_24dp, you'll see the icon goes all the way to the edge of the bounds.
PS if you aim to have all your sizes be a multiple of 8dp, things will line up nicely and look great.