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

在每一个图片的某一侧都可以展示出一个三角形的边框视图,就是咱们的三角形标签视图。这个视图在电商类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实现图片一边的三角形边框效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

(0)

相关推荐

  • Android TextView设置背景色与边框的方法详解

    1.在drawable文件夹下面创建setbar_bg.xml 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 背景色 -->    <solid android:color="#FFE4B5&q

  • Android 去掉自定义dialog的白色边框的简单方法

    在value目录下,创建styles.xml文件 复制代码 代码如下: <?xml version="1.0" encoding="UTF-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android"> <style        name="dialog"        parent="@androi

  • Android实现圆角边框对话框的方法

    前言 最近要实现个圆角边框的对话框设计图,查了网上很多种实现,都差不多,从中得到灵感,实现了另一种方式,利用layer-list: 先来看看实现的效果如下: 首先在drawable目录下定义好圆角背景文件dialog_corner_bg.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/r

  • Android TextView(圆弧)边框和背景实例详解

     Android TextView 圆弧 效果图: 布局代码: <TextView android:id="@+id/product_tag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:singleLine="true" androi

  • Android使用LinearLayout设置边框

    找到好多人的,都是文章随便copy,自己都不验证下,特别说LinearLayout中可以设置他的divider属性的,我在Android Studio中试了,根本显示不出来,这边是csdn上一个朋友回答的,我收藏了,放到这里,后面备用. 1.定一个underline的xml文件,把它放到drawable下 underline.xml <?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:a

  • android shape的使用及渐变色、分割线、边框、半透明阴影

    shape使用.渐变色.分割线.边框.半透明.半透明阴影效果. 首先简单了解一下shape中常见的属性.(详细介绍参看 api文档) <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "

  • Android Studio实现带边框的圆形头像

    本文实例为大家分享了Android Studio实现带边框的圆形头像的具体代码,供大家参考,具体内容如下 效果显示: (没有边框的) (有边框的) 1.创建自定义ImagView控件 (1).没有边框的 package chenglong.activitytest.pengintohospital.utils; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitma

  • Android实现代码画虚线边框背景效果

    实现如下边框效果: 虚线画效果,可以使用Android中的xml来做.下面话不多说,直接上代码: <RelativeLayout android:id="@+id/coupon_popup" android:layout_width="320dp" android:layout_height="200dp" android:layout_margin="20dp" android:gravity="center

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

    在每一个图片的某一侧都可以展示出一个三角形的边框视图,就是咱们的三角形标签视图.这个视图在电商类APP当中比较常用,使用过ebay的同学应该都还记得有些商品的左上角或者右上角都会显示一个三角形的边框,用于给人一个直观的商品正在促销,或者刚刚上线的直观感受.我们可以看看实现后的效果如下: 在真实的APP当中,我们还会加上一个SrcollView控件,这样子才可以进行不断地上下浏览.我们这里主要是为了让大家明白这个视图是该如何实现的,就不演示SrcollView控件下的做法了,直接在线性布局下做一个

  • Android实现图片浮动随意拖拽效果

    本文实例为大家分享了Android实现图片浮动拖拽效果的具体代码,供大家参考,具体内容如下 实现步骤 1.先自定义一个浮动工具类 public class MoveImage extends ImageView { /** * * 浮动工具类 * */ private int lastX = 0; private int lastY = 0; private int dx; private int dy; private float movex = 0; private float movey

  • Android实现图片点击预览效果(zoom动画)

    参考:https://developer.android.google.cn/training/animation/zoom.html 1.创建Views 下面的布局包括了你想要zoom的大版本和小版本的view. 1.ImageButton是小版本的,能点击的,点击后显示大版本的ImageView. 2.ImageView是大版本的,可以显示ImageButton点击后的样式. 3.ImageView一开始是不可见的(invisible),当ImageButton点击后,它会实现zoom动画,

  • Android实现三角形气泡效果方式汇总

    在开发过程中,我们可能会经常遇到这样的需求样式: 这张图是截取京东消息通知的弹出框,我们可以看到右上方有个三角形的气泡效果,这只是其中一种,三角形的方向还可以是上.下.左.右. 通过截图可以发现,气泡由正三角形和圆角长方形组成,于是可以通过组合来形成三角形气泡的效果,下面我们通过三种方式进行实现. 实现方式: 1.通过.9图进行实现: 2.通过shape方式实现: 3.通过自定义view的方式实现: 实现逻辑: 1.通过.9图进行实现 这种方式就不用说了吧,找你们UI小姐姐切一个.9图,使用即可

  • jquery给图片添加鼠标经过时的边框效果

    一哥们儿要给图片添加鼠标经过时的边框效果,可惜出发点错了,直接加在了IMG外的A标签上致使 鼠标经过时图片产生塌陷,实则应该将边框控制直接加在IMG标签上即可 错误代码如下:注意红色部分设置 (出发点就错了) 复制代码 代码如下: <html> <head> <script type="text/javascript" src="http://www.w3school.com.cn/jquery/jquery.js"></s

  • Android实现动态向Gallery中添加图片及倒影与3D效果示例

    本文实例讲述了Android实现动态向Gallery中添加图片及倒影与3D效果的方法.分享给大家供大家参考,具体如下: 在Android中gallery可以提供一个很好的显示图片的方式,实现上面的效果以及动态添加数据库或者网络上下载下来的图片资源.我们首先实现一个自定义的Gallery类. MyGallery.java: package nate.android.Service; import android.content.Context; import android.graphics.Ca

  • Android 利用ViewPager实现图片可以左右循环滑动效果附代码下载

    首先给大家展示靓照,对效果图感兴趣的朋友可以继续往下阅读哦. ViewPager这个小demo实现的是可以左右循环滑动图片,下面带索引,滑到最后一页在往右滑动就要第一页,第一页往左滑动就到最后一页,上面是效果图,用美女图片是我一贯的作风,呵呵  1.    首先看一些layout下的xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=&qu

  • android实现图片反转效果

    可能有些同学不明白,为啥要图片反转(不是旋转哦),我们在游戏开发中,为了节省图片资源(空间) 有可能会使用到图片反转,例如,一个人物图片,面向左,或右,如果不能实现图片反转的情况下,就需要两张图片了,废话少说,看效果上代码: 在上图中,实际两个人物使用的是一张图片,只是针对一张图片做了处理而已. 详细代码: public class ImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback{ public Bit

  • Android仿微信图片点击全屏效果

    废话不多说,先看下效果: 先是微信的 再是模仿的 先说下实现原理,再一步步分析 这里总共有2个Activity一个就是主页,一个就是显示我们图片效果的页面,参数通过Intent传送,素材内容均来自网络,(感谢聪明的蘑菇) 图片都是Glide异步下的,下的,下的重要的事情说三次,然后就是用动画做放大操作然后显示出来了(并没有做下载原图的实现,反正也是一样 下载下来Set上去而且动画都不需要更简便). OK,我们来看分析下 obj,目录下分别创建了2个对象,一个用来使用来处理显示页面的图片尺寸信息以

  • Android实现图片轮播效果的两种方法

    大家在使用APP的过程中,经常会看到上部banner图片轮播的效果,那么今天我们就一起来学习一下,android中图片轮询的几种实现方法: 第一种:使用动画的方法实现:(代码繁琐) 这种发放需要:两个动画效果,一个布局,一个主类来实现,不多说了,来看代码吧: public class IamgeTrActivity extends Activity { /** Called when the activity is first created. */ public ImageView image

随机推荐