在每一个图片的某一侧都可以展示出一个三角形的边框视图,就是咱们的三角形标签视图。这个视图在电商类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. [考试反思]0714/0716,NOIP模拟测试3/4

    这几天时间比较紧啊(其实只是我效率有点低我在考虑要不要坐到后面去吹空调) 但是不管怎么说,考试反思还是要写的吧. 第三次考试反思没写总感觉缺了点什么,但是题都刷不完... 一进图论看他们刷题好快啊为什 ...

  2. 2019.11.11 洛谷月赛t3

    题目背景 由于Y校的老师非常毒瘤,要求\(zhouwc\)在\(csp\)考前最后\(3\)天参加期中考,\(zhouwc\)非常生气,决定消极考试,以涂完卡但全错为目标.现在\(retcarizy\ ...

  3. 【Leetcode 做题学算法周刊】第三期

    首发于微信公众号<前端成长记>,写于 2019.11.13 背景 本文记录刷题过程中的整个思考过程,以供参考.主要内容涵盖: 题目分析设想 编写代码验证 查阅他人解法 思考总结 目录 35 ...

  4. Android9.0 SystemUI 网络信号栏定制修改

    前情提要 Android 8.1平台SystemUI 导航栏加载流程解析 9.0 改动点简要说明 1.新增 StatusBarMobileView 替代 SignalClusterView,用以控制信 ...

  5. java基础阶段几个面试题

    1.说出你对面向对象的理解 在我理解,面向对象是向现实世界模型的自然延伸,这是一种“万物皆对象”的编程思想.在现实生活中的任何物体都可以归为一类事物,而每一个个体都是一类事物的实例.面向对象的编程是以 ...

  6. 淘宝小练习#css

    * { margin: 0; padding: 0; } a { text-decoration: none; } .box { background: #f4f4f4; } /* 头部样式STAR ...

  7. mybatis的parameterType为map,map里带有多个list

    我写这个主要是为了解决sql注入 原sql有sql注入, 结果:select req_msg_id from account_message_info where req_msg_id in ('12 ...

  8. QQ是怎样创造出来的?——解密好友系统的设计

    本篇介绍笔者接触的第一个后台系统,从自身见闻出发,因此涉及的内容相对比较基础,后台大牛请自觉略过. 什么是好友系统? 简单的说,好友系统是维护用户好友关系的系统.我们最熟悉的好友系统案例当属QQ,实际 ...

  9. thinkphp6.0 多应用模块下提示控制器不存在

    thinkphp6.0 多应用模块下提示控制器不存在 在项目根目录下使用Composer composer require topthink/think-multi-app 参考链接

  10. 自制反汇编逆向分析工具 与hopper逆向输出对比

    经过一个阶段5次迭代之后,本逆向分析工具功能基本成形.工具的基本功能介绍请参看前面的posts. 现在就和hopper的逆向函数伪代码的功能对比一下效果.在这里并非定胜劣,因为差异可以拿来对比参照,通 ...