边框突出效果android,Android实现图片一边的三角形边框效果

在每一个图片的某一侧都可以展示出一个三角形的边框视图,就是咱们的三角形标签视图。这个视图在电商类APP当中比较常用,使用过ebay的同学应该都还记得有些商品的左上角或者右上角都会显示一个三角形的边框,用于给人一个直观的商品正在促销,或者刚刚上线的直观感受。我们可以看看实现后的效果如下:

7c40858e4f4d4147fb4d8d581979086b.png

在真实的APP当中,我们还会加上一个SrcollView控件,这样子才可以进行不断地上下浏览。我们这里主要是为了让大家明白这个视图是该如何实现的,就不演示SrcollView控件下的做法了,直接在线性布局下做一个简单的说明。由于在线性布局上面一共具有四张图,因此咱们可以先单独编写每一个imageview的自定义view,然后的语法将他们组合起来,这样可以提高UI开发的效率,进行协同工作与开发。首先咱们先实现左上角和右上角的triangle view.

在build.gradle文件当中相应地方添加如下代码,导入相应的maven库:

allprojects {

repositories {

...

maven { url "https://jitpack.io" }

}

}

之后在另一个build.gradle文件当中添加库:

dependencies {

implementation 'com.github.shts:TriangleLabelView:1.1.2'

}

咱们的前期工作就这样做好啦,现在就开始正式编写咱们的每一个三角形边框视图啦,首先是第一个位于左上角的视图

一.card_left_top.xml:

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/image"

android:scaleType="centerCrop"

android:src="@drawable/s_image_2"

android:layout_width="match_parent"

android:layout_height="match_parent" />

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" />

编写好后在preview当中显示如下:

a12475a875916a5a7daf00a0124a376c.png

下面是位于右上角的视图

二.card_right_top.xml:

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/image"

android:scaleType="centerCrop"

android:src="@drawable/s_image_4"

android:layout_width="match_parent"

android:layout_height="match_parent" />

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" />

三.card_right_buttom.xml:

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/image"

android:scaleType="centerCrop"

android:src="@drawable/s_image_3"

android:layout_width="match_parent"

android:layout_height="match_parent" />

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" />

四.card_left_buttom.xml:

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/image"

android:src="@drawable/s_image_1"

android:scaleType="centerCrop"

android:layout_width="match_parent"

android:layout_height="match_parent" />

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" />

最后咱们整合一下就OK啦!整合后的主活动的代码为:

五.activity_main.xml:

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">

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:orientation="horizontal">

android:layout_height="match_parent"

android:layout_weight="1"

android:layout_margin="2dp"

android:id="@+id/left_top" layout="@layout/card_left_top" />

android:layout_height="match_parent"

android:layout_weight="1"

android:layout_margin="2dp"

android:id="@+id/right_top" layout="@layout/card_right_top" />

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:orientation="horizontal">

android:layout_height="match_parent"

android:layout_weight="1"

android:layout_margin="2dp"

android:id="@+id/left_bottom" layout="@layout/card_left_bottom" />

android:layout_height="match_parent"

android:layout_weight="1"

android:layout_margin="2dp"

android:id="@+id/right_bottom" layout="@layout/card_right_bottom" />

帅照:

8a5de589459a0f5494226b8c9520df88.png

总结

以上所述是小编给大家介绍的Android实现图片一边的三角形边框效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值