服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现图片一边的三角形边框效果

Android实现图片一边的三角形边框效果

2022-11-10 15:54Geeksongs Android

这篇文章主要介绍了Android实现图片一边的三角形边框效果,本文图文并茂通过实例代码讲解的非常详细,需要的朋友可以参考下

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

Android实现图片一边的三角形边框效果

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

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

?
1
2
3
4
5
6
allprojects {
    repositories {
      ...
      maven { url "https://jitpack.io" }
    }
}

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

?
1
2
3
dependencies {
      implementation 'com.github.shts:TriangleLabelView:1.1.2'
  }

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

一.card_left_top.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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>

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

Android实现图片一边的三角形边框效果

下面是位于右上角的视图

二.card_right_top.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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>

三.card_right_buttom.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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>

四.card_left_buttom.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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>

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

五.activity_main.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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实现图片一边的三角形边框效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://www.cnblogs.com/geeksongs/archive/2019/12/09/12012524.html

延伸 · 阅读

精彩推荐
  • Android新浪微博第三方登录界面上下拉伸图片之第三方开源PullToZoomListViewEx(二)

    新浪微博第三方登录界面上下拉伸图片之第三方开源PullToZoomLi

    这篇文章主要介绍了新浪微博第三方登录界面上下拉伸图片之第三方开源PullToZoomListViewEx(二) 的相关资料,需要的朋友可以参考下...

    Z29402021-04-19
  • AndroidAndroid Activity的启动过程源码解析

    Android Activity的启动过程源码解析

    这篇文章主要介绍了Android Activity的启动过程源码解析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    singwhatiwanna6382022-02-17
  • AndroidViewPager滑动灵敏度调整的方法实力

    ViewPager滑动灵敏度调整的方法实力

    这篇文章主要介绍了ViewPager滑动灵敏度调整的方法实力,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    wenson1235772022-08-11
  • Android浅谈Android Service服务的高级技巧

    浅谈Android Service服务的高级技巧

    这篇文章主要介绍了浅谈Android 服务的高级技巧,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    deniro8692022-03-09
  • AndroidAndroid启动相机拍照并返回图片

    Android启动相机拍照并返回图片

    Android启动相机拍照并返回图片,事先定义拍照方法,在启动拍照之前先判断内存是否可用,通过重写onactivityresult()方法,获取拍好的图片。对下文感兴趣的...

    Android开发网4632021-04-07
  • AndroidAndroid中多个EditText输入效果的解决方式

    Android中多个EditText输入效果的解决方式

    这篇文章主要给大家介绍了关于Android中多个EditText输入效果的解决方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价...

    Android开发架构11322022-09-22
  • AndroidAndroid开发实现的图片点击切换功能示例

    Android开发实现的图片点击切换功能示例

    这篇文章主要介绍了Android开发实现的图片点击切换功能,涉及Android ImageView组件创建、布局及实现图形切换相关操作技巧,需要的朋友可以参考下...

    水中鱼之19998532022-10-14
  • Androidandroid 定位的4种方式介绍

    android 定位的4种方式介绍

    开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方...

    Android开发网11542021-03-04