这篇文章主要介绍了Android实现图片一边的三角形边框效果,本文图文并茂通过实例代码讲解的非常详细,需要的朋友可以参考下
在每一个图片的某一侧都可以展示出一个三角形的边框视图,就是咱们的三角形标签视图。这个视图在电商类APP当中比较常用,使用过ebay的同学应该都还记得有些商品的左上角或者右上角都会显示一个三角形的边框,用于给人一个直观的商品正在促销,或者刚刚上线的直观感受。我们可以看看实现后的效果如下:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
之后在另一个build.gradle文件当中添加库:
dependencies {
implementation 'com.github.shts:TriangleLabelView:1.1.2'
}
咱们的前期工作就这样做好啦,现在就开始正式编写咱们的每一个三角形边框视图啦,首先是第一个位于左上角的视图
一.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="QGRyYXdhYmxlL3NfaW1hZ2VfMg=="
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>
编写好后在preview当中显示如下:
<?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="QGRyYXdhYmxlL3NfaW1hZ2VfNA=="
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>
三.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="QGRyYXdhYmxlL3NfaW1hZ2VfMw=="
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>
四.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="QGRyYXdhYmxlL3NfaW1hZ2VfMQ=="
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>
最后咱们整合一下就OK啦!整合后的主活动的代码为:
五.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>
完事儿!github源码可以在https://github.com/shts/TriangleLabelView处进行阅读!!!
帅照:
总结
以上所述是小编给大家介绍的Android实现图片一边的三角形边框效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程学习网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
本文标题为:Android实现图片一边的三角形边框效果


- Android实现轮询的三种方式 2023-02-17
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- Android实现监听音量的变化 2023-03-30
- Android studio实现动态背景页面 2023-05-23
- 详解flutter engine 那些没被释放的东西 2022-12-04
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- Flutter实现底部和顶部导航栏 2022-08-31
- iOS 对当前webView进行截屏的方法 2023-03-01
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14