I have shape with background image and placing as background of ImageView - android

I have ImageView with shape but image is also visible to the outside of shape bounds.
This is my shape code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/dashboard_bg" />
<item>
<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" />
<padding
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp" />
<stroke
android:width="10dp"
android:color="#android:color/white" />
</shape>
</item>
</layer-list>
and this is my ImageView
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/layer_list_circl"/>
And this is the result i got..
what i want just squeeze image into shape. Thanks in advance.

First of all, the image you are loading seems large, so you need to scale it to fit the right size you want. since the image you are trying to wrap is rectangle it can't be wrapped in a ring shape.
Therefore, you can use an image loading lib like fresco or picasso and set the correct size you want, and the scaling/crop option.
After you did that, you can add the background as you created and it should fit the image.

You can try adding this to your ImageView:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/layer_list_circl"/>

Related

Angle Corners in Android

In Android, it's possible to create a Drawable that has rounded corners, which can then be used as the background of other controls in order to give them the appearance of having rounded corners.
However, I want my corners to be angular, like this:
And most importantly, I want the clipped corner to remain the same size regardless of the size of the object it's applied to (just like the rounded corners would), which means that I can't just use a vector drawable, which would stretch the shape if the control is not the same size as the drawable.
How would I accomplish the effect I'm going for (preferably a solution that doesn't involve any Java)?
I eventually achieved the effect I was going for using a layer list drawable:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="left" android:right="14.5dp" android:top="14.5dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="1dp"
android:color="#000000"
android:dashWidth="2dp"
android:dashGap="1.4dp" />
</shape>
</item>
<item android:gravity="right" android:bottom="14.5dp">
<rotate
android:fromDegrees="-45"
android:toDegrees="0"
android:pivotX="100%"
android:pivotY="0%">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="1dp"
android:color="#000000"
android:dashWidth="2dp"
android:dashGap="1.4dp" />
<size android:width="22.3dp" android:height="22.3dp" />
</shape>
</rotate>
</item>
</layer-list>
In order to display this drawable, I then used an ImageView:
<ImageView
android:layout_width="match_parent"
android:layout_height="16dp"
android:scaleType="fitXY"
android:layerType="software"
android:src="#drawable/fg_dotted_line" />

Prevent CornerOverlapping in linearLayout

an Imageview is a wrapped in a linearlayout. the linearlayout has a rounded background. I want the imageView not to overlap the rounded corners of the linearlayout. Cardview does something similar but I don't want all corners radius.
Here's my snippet.
<LinearLayout
android:background="#drawable/bottom_sheet_top_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/preview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="300dp"
android:scaleType="center" />
</LinearLayout>
bottom_sheet_top_stroke
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="0dp"
android:left="-6dp"
android:right="-6dp"
android:bottom="-32dp">
<shape android:shape="rectangle">
<solid android:color="#color/dark"/>
<stroke
android:width="5dp"
android:color="#color/white"/>
<corners android:radius="40dp"/>
</shape>
</item>
</layer-list>
how do I achieve making sure the image is within the defined radius of the parent and not overlap.
You have to make your Image with roundedCorners.
For Image loading libaries like Glide and picasso there is already lots of transformation available .
You have to use RoundedCornersTransformation
Glide.with(this).load(url)
.apply(RequestOptions.bitmapTransform(
new RoundedCornersTransformation(this, 10, 10))).into(mImageView);
For transformations you can take reference from glide-transformations .

round ImageButton full width background image

I am trying to set a backgroundimage to my ImageButton. The ImageButton should be full filled. Currently it looks like:
ImageButton
<ImageButton
android:id="#+id/imgBtn_OpenUserPicture"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center_horizontal|center_vertical"
android:src="#drawable/ic_camera"
android:background="#drawable/round_imagebutton"
android:scaleType="centerCrop"
android:tint="#android:color/transparent"
android:padding="20dp"/>
#drawable/round_imagebutton
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent"/>
<corners android:radius="90dp" />
<stroke
android:width="2dp"
android:color="#FFF"
/>
</shape>
</item>
</selector>
One problem could be the padding of 20dp. After removing it, the problem was the same. I think I have to set the image as the background of the shape. But how can I set the image dynamically - captured by the camera - to the shape background?
Use scale type as fit center.
android:scaleType="fitCenter"
There are Two way
1# Use rounded image instead of rectangle to set src
2# use this link instead of ImageButton
https://github.com/hdodenhof/CircleImageView

Resizing bitmap item in layerlist

I have a layerlist that I use as a background for a spinner. I'm trying to resize the bitmap that is one of the items in the layerlist. The problem is that there aren't any resizing traits that can be found. Please see code below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<stroke
android:width="2dp"
android:color="#80000000"/>
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp"/>
</shape>
</item>
<item>
<bitmap
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:src="#mipmap/ic_launcher" />
</item>
</layer-list>
Here is a visual of what's happening:
How can I resize this bitmap? I'm trying to scale the image down because its too big. I've tried changing the layout width and height to a certain number and also resizing the image in an image editing program but nothing happened. Any help would be appreciated.

Circular Text Views like Google Messenger

I would like to display letters inside of circles exactly the way Google Messenger does it, like this:
I tried using this drawable file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#color/primary_color" android:width="2dip"/>
<solid android:color="#color/primary_color"/>
as the background for a text view, but that went disastrously wrong. Any ideas?
You was almost close to answer.
Create a drawable resources file under drawable folder: circular_textview.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#4286F5"/>
Set this drawable as your textView's background.
<TextView
android:layout_width="55dp"
android:layout_height="55dp"
android:text="C"
android:fontFamily="sans-serif-thin"
android:textColor="#FFF"
android:textSize="32sp"
android:gravity="center"
android:id="#+id/textView"
android:background="#drawable/circular_textview"/>
That's it
This is what I came up with.
I used TextViews with a layer-list background. The background fits the size of the view it's loaded in, so in order to have a circle you can force the TextView to have the same width and height.
<TextView
android:id="#+id/view"
android:layout_width="65dp"
android:layout_height="65dp"
android:gravity="center"
android:textSize="40dp"
android:fontFamily="sans-serif-thin"
android:background="#drawable/background"
android:text="A"/>
I have hardcoded text, size, width and height, but you could manage them at runtime. fontFamily="sans-serif-thin" lets you use, if available, the thin typeface which might be better. The layer-list:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="1dp">
<shape android:shape="oval" >
<solid android:color="#321a1a1a"/>
</shape>
</item>
<item
android:top="1dp"
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<shape android:shape="oval" >
<solid android:color="#color/red_700"/>
</shape>
</item>
</layer-list>
First item is to get the fake shadow shown, but you can get rid of that. Also, you can remove its offset android:top="1dp". This way you will have a circular 1dp ring outside (while now shadow is cast only at the bottom and in both sides).
Again, I have hardcoded colors but you can change to what you want.
Try it that way.
Path res/drawable/oval.xml:
<?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="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
This code gets your textview and applies the shape as background to it:
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);
It's from: http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
Late but not least.
I'd suggest what I used in one of my projects by Amulya Khare.
https://github.com/amulyakhare/TextDrawable

Categories

Resources