当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现图片一边的三角形边框

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

2019年12月10日  | 移动技术网IT编程  | 我要评论

配股用鑫东财配资,阿衰,西昌贺雪梅

在每一个图片的某一侧都可以展示出一个三角形的边框视图,就是咱们的三角形标签视图。这个视图在电商类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'
    }

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

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

 

下面是位于右上角的视图

 

 

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

 

 

 

<?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>
<?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>
<?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处进行阅读!!!

帅照:

 

 

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网