Android implements the triangular border effect on one side of the picture
A triangle border view can be displayed on one side of each picture, which is our triangle label view. This view is commonly used in e-commerce apps. Students who have used eBay should remember that a triangular border will be displayed in the upper left or right corner of some goods to give people an intuitive feeling that the goods are being promoted or just launched. We can see the effect after implementation as follows:
In the real app, we will also add a srcollview control so that the child can browse up and down continuously. We are here mainly to let you understand how this view should be implemented. Instead of demonstrating the method under the srcollview control, we will directly make a simple description under the linear layout. Since there are four diagrams on the linear layout, we can write the custom view of each ImageView separately, and then combine them with the < include > syntax, which can improve the efficiency of UI development and carry out collaborative work and development. First, let's implement the triangle view in the upper left corner and the upper right corner
Add the following code in the corresponding place in the build.gradle file and import the corresponding Maven Library:
allprojects { repositories { ... maven { url "https://jitpack.io" } } }
Then add the library in another build.gradle file:
dependencies { implementation 'com.github.shts:TriangleLabelView:1.1.2' }
Our preliminary work has been done so well. Now we begin to formally write each triangle border view, the first view in the upper left corner
1、 Card_ left_ top.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:scaleType="centerCrop" android:src="@drawable/s_image_2" android:layout_width="match_parent" android:layout_height="match_parent" /> <jp.shts.android.library.TriangleLabelView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" app:backgroundColor="@color/yellow_900" app:corner="leftTop" app:labelBottomPadding="5dp" app:labelCenterPadding="0dp" app:labelTopPadding="10dp" app:primaryText="New" app:primaryTextColor="@color/yellow_500" app:primaryTextSize="16sp" app:secondaryText="01" app:secondaryTextColor="@color/yellow_100" app:secondaryTextSize="11sp" /> </RelativeLayout> </android.support.v7.widget.CardView>
After writing, it is displayed in the preview as follows:
Below is the view in the upper right corner
2、 Card_ right_ top.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:scaleType="centerCrop" android:src="@drawable/s_image_4" android:layout_width="match_parent" android:layout_height="match_parent" /> <jp.shts.android.library.TriangleLabelView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_alignParentTop="true" app:backgroundColor="@color/teal_900" app:corner="rightTop" app:labelBottomPadding="5dp" app:labelCenterPadding="0dp" app:labelTopPadding="10dp" app:primaryText="New" app:primaryTextColor="@color/teal_500" app:primaryTextSize="16sp" app:secondaryText="01" app:secondaryTextColor="@color/teal_100" app:secondaryTextSize="11sp" /> </RelativeLayout> </android.support.v7.widget.CardView>
3、 Card_ right_ buttom.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:scaleType="centerCrop" android:src="@drawable/s_image_3" android:layout_width="match_parent" android:layout_height="match_parent" /> <jp.shts.android.library.TriangleLabelView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" app:backgroundColor="@color/pink_900" app:corner="rightBottom" app:labelTopPadding="10dp" app:labelCenterPadding="5dp" app:labelBottomPadding="0dp" app:primaryText="New" app:primaryTextColor="@color/pink_500" app:primaryTextSize="16sp" app:secondaryText="01" app:secondaryTextColor="@color/pink_100" app:secondaryTextSize="11sp" /> </RelativeLayout> </android.support.v7.widget.CardView>
4、 Card_ left_ buttom.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:src="@drawable/s_image_1" android:scaleType="centerCrop" android:layout_width="match_parent" android:layout_height="match_parent" /> <jp.shts.android.library.TriangleLabelView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" app:backgroundColor="@color/blue_900" app:corner="leftBottom" app:labelTopPadding="10dp" app:labelCenterPadding="5dp" app:labelBottomPadding="0dp" app:primaryText="New" app:primaryTextColor="@color/blue_500" app:primaryTextSize="16sp" app:secondaryText="01" app:secondaryTextColor="@color/blue_100" app:secondaryTextSize="11sp" /> </RelativeLayout>
Finally, let's integrate it! The code of the integrated main activity is:
5、 Activity_ main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Fragment2"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <include android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_margin="2dp" android:id="@+id/left_top" layout="@layout/card_left_top" /> <include android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_margin="2dp" android:id="@+id/right_top" layout="@layout/card_right_top" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <include android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_margin="2dp" android:id="@+id/left_bottom" layout="@layout/card_left_bottom" /> <include android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_margin="2dp" android:id="@+id/right_bottom" layout="@layout/card_right_bottom" /> </LinearLayout> </LinearLayout>
It's done! GitHub source code can be found in https://github.com/shts/TriangleLabelView Read at!!!
Shuai Zhao:
summary
The above is what Xiaobian introduced to you. Android realizes the triangular border effect on one side of the picture. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!