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

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

<?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当中显示如下:

下面是位于右上角的视图

二.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>

三.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>

四.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>

最后咱们整合一下就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实现图片一边的三角形边框的更多相关文章

  1. Android 给图片 加边框

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  2. Android 给图片加边框

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  3. Android ImageView图片自适应 (转)

    网络上下载下来的图片自适应:android:adjustViewBounds="true"(其详细解释在下面)<ImageView     android:id=" ...

  4. Android ImageView图片自适应

    网络上下载下来的图片自适应:android:adjustViewBounds="true"(其详细解释在下面) <ImageView     android:id=" ...

  5. Android圆形图片自定义控件

    Android圆形图片控件效果图如下: 代码如下: RoundImageView.java package com.dxd.roundimageview; import android.content ...

  6. Android Imageview 图片居左居右,自定义圆角

    android:scaleType="fitStart"    图片靠左不变形显示, android:scaleType=”fitEnd”  图片靠右显示,不变形. 半透明andr ...

  7. Android圆形图片--自己定义控件

    Android圆形图片控件效果图例如以下: 代码例如以下: RoundImageView.java package com.dxd.roundimageview; import android.con ...

  8. Android压缩图片到100K以下并保持不失真的高效方法

    前言:目前一般手机的相机都能达到800万像素,像我的Galaxy Nexus才500万像素,拍摄的照片也有1.5M左右.这么大的照片上传到服务器,不仅浪费流量,同时还浪费时间. 在开发Android企 ...

  9. 仿优酷Android客户端图片左右滑动(自动滑动)

    最终效果: 页面布局main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...

随机推荐

  1. Python基础语法,他其实很贵!

    # -*- coding:utf-8 -*- # @Time :2019/7/19 11:26# @Author :ITester# @Email :1036881587@qq.com# @File ...

  2. js基础总结02--字符串操作

    1.字符串中对单个字符位置的操作 indexOf(char); 从左往右查找,返回匹配到的第一个字符的位置,没有匹配则返回-1 lastiIndexOf(char); 从右往左查找,返回匹配到的第一个 ...

  3. hadoop2.9.0之前的版本yarn RM fairScheduler调度性能优化

    对一般小公司来说 可能yarn调度能力足够了 但是对于大规模集群1000 or 2000+的话  yarn的调度性能捉襟见肘 恰好网上看到一篇很好的文章https://tech.meituan.com ...

  4. 从BIO到Netty的演变

    从BIO到Netty的演变 前言 计算机网络可以说是每个学计算机的都绕不过去的一道坎.计算机网络到底有多么重要,你走到大学图书馆的计算机部分,翻开那些什么<从零开始:黑客XXX>,< ...

  5. python——inspect模块

    inspect模块常用功能 import inspect # 导入inspect模块 inspect.isfunction(fn) # 检测fn是不是函数 inspect.isgenerator((x ...

  6. 易初大数据 spss 2019年10月31日 wangqingchao

    ---恢复内容开始--- 1.描述性统计分析方法是指应用分类.制表.图形及概括性数据指标来概括数据分析特征的方法. 2.而推断性统计分析方法则是通过随机抽样,应用统计方法把从样本数据得到的结论推广到总 ...

  7. javascript关于box2djs和matterjs之间的选择

    javascript关于box2djs和matterjs之间的选择box2djs资料少很多时候需要看c++版本资料 然后转化成js 还有转化成像素坐标不准确 matterjs文档丰富 上手容易 建议用 ...

  8. 玩转VSCode-完整构建VSCode开发调试环境

    随着VSCode的不断完善和强大,是时候将部分开发迁移到VS Code中了. 目前使用VS2019开发.NET Core应用,一直有一个想法,在VS Code中复刻VS的开发环境,同时迁移到VS Co ...

  9. BST的实现(二叉搜索树)

    void Inorder(struct Tree *T); //中序 void Preorder(struct Tree *T); //前序 void Postorder(struct Tree *T ...

  10. 你不知道的JS系列【1】- 什么是作用域

      几乎所有的编程语言都能够储存变量,并且能在之后对这个变量值进行访问或修改,正是储存和访问变量的能力将状态带给了程序,那么,这些变量储存在哪里呢?程序需要时又是如何找到他们?这些问题说明需要一套设计 ...