1. 程式人生 > 程式設計 >Android實現圖片一邊的三角形邊框效果

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實現圖片一邊的三角形邊框效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!