溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android如何實(shí)現(xiàn)圖片一邊的三角形邊框效果

發(fā)布時(shí)間:2021-04-16 09:40:38 來源:億速云 閱讀:183 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章主要介紹了Android如何實(shí)現(xiàn)圖片一邊的三角形邊框效果,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

在每一個(gè)圖片的某一側(cè)都可以展示出一個(gè)三角形的邊框視圖,就是咱們的三角形標(biāo)簽視圖。這個(gè)視圖在電商類APP當(dāng)中比較常用,使用過ebay的同學(xué)應(yīng)該都還記得有些商品的左上角或者右上角都會(huì)顯示一個(gè)三角形的邊框,用于給人一個(gè)直觀的商品正在促銷,或者剛剛上線的直觀感受。我們可以看看實(shí)現(xiàn)后的效果如下:

Android如何實(shí)現(xiàn)圖片一邊的三角形邊框效果

 在真實(shí)的APP當(dāng)中,我們還會(huì)加上一個(gè)SrcollView控件,這樣子才可以進(jìn)行不斷地上下瀏覽。我們這里主要是為了讓大家明白這個(gè)視圖是該如何實(shí)現(xiàn)的,就不演示SrcollView控件下的做法了,直接在線性布局下做一個(gè)簡(jiǎn)單的說明。由于在線性布局上面一共具有四張圖,因此咱們可以先單獨(dú)編寫每一個(gè)imageview的自定義view,然后<include>的語法將他們組合起來,這樣可以提高UI開發(fā)的效率,進(jìn)行協(xié)同工作與開發(fā)。首先咱們先實(shí)現(xiàn)左上角和右上角的triangle view.

在build.gradle文件當(dāng)中相應(yīng)地方添加如下代碼,導(dǎo)入相應(yīng)的maven庫:

allprojects {
    repositories {
      ...
      maven { url "https://jitpack.io" }
    }
}

之后在另一個(gè)build.gradle文件當(dāng)中添加庫:

dependencies {
      implementation 'com.github.shts:TriangleLabelView:1.1.2'
  }

咱們的前期工作就這樣做好啦,現(xiàn)在就開始正式編寫咱們的每一個(gè)三角形邊框視圖啦,首先是第一個(gè)位于左上角的視圖

一.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當(dāng)中顯示如下:

Android如何實(shí)現(xiàn)圖片一邊的三角形邊框效果

下面是位于右上角的視圖

二.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啦!整合后的主活動(dòng)的代碼為:

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

帥照:

Android如何實(shí)現(xiàn)圖片一邊的三角形邊框效果

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android如何實(shí)現(xiàn)圖片一邊的三角形邊框效果”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI