Android實現圖片一邊的三角形邊框

来源:https://www.cnblogs.com/geeksongs/archive/2019/12/09/12012524.html
-Advertisement-
Play Games

在每一個圖片的某一側都可以展示出一個三角形的邊框視圖,就是咱們的三角形標簽視圖。這個視圖在電商類APP當中比較常用,使用過ebay的同學應該都還記得有些商品的左上角或者右上角都會顯示一個三角形的邊框,用於給人一個直觀的商品正在促銷,或者剛剛上線的直觀感受。我們可以看看實現後的效果如下: 在真實的AP ...


在每一個圖片的某一側都可以展示出一個三角形的邊框視圖,就是咱們的三角形標簽視圖。這個視圖在電商類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處進行閱讀!!!

帥照:

 

 

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 環境:VS2010,.NET Framework 4.0,Oracle.ManagedDataAccess 在最近做一個項目中,用到了Oracle資料庫,使用Oracle.ManagedDataAccess來進行資料庫連接。但是在執行事務的時候拋出異常 “'OracleInternal.MTS.DT ...
  • https://sqlserver.code.blog/2019/12/09/an-example-of-polybase-for-oracle/ ...
  • SQLite 的 UPDATE 語句用於修改表中已有的記錄。可以使用帶有 WHERE 子句的 UPDATE 查詢來更新選定行,否則所有的行都會被更新。 基本語法:UPDATE table_name SET column1 = value1, column2 = value2...., columnN ...
  • Sql模糊查詢,Like預設是不區分大小寫的 使用Like時,怎麼支持大小寫呢? upper、lower,只能模糊所有的內容,不能區分內容中的大小寫。 sqlite資料庫對text欄位預設是大小寫敏感的,但是唯獨在模糊查詢時不起作用。 解決方法是:查詢前先執行PRAGMA case_sensitiv ...
  • 開心一刻 我:嗨,老闆娘,有冰紅茶沒 老闆娘:有 我:多少錢一瓶 老闆娘:3塊 我:給我來一瓶,給,3塊 老闆娘:來,你的冰紅茶 我:玩吶,我要冰紅茶,你給我個瓶蓋乾哈? 老闆娘:這是再來一瓶,我家賣完了,你去隔壁家換一下 問題背景 對於 MySQL 的 JOIN,不知道大家有沒有去想過他的執行流程 ...
  • DECLARE @ESQL VARCHAR(1000);DECLARE FCursor CURSOR --定義游標FOR (SELECT 'ALTER TABLE '+O.name+' DROP CONSTRAINT '+F.name+';' AS CommandSQL from SYS.FOREI ...
  • 安裝VC++2013 若是以上方法不能解決,需要下載安裝VC++2013,這是微軟官網的鏈接 https://www.microsoft.com/zh cn/download/confirmation.aspx?id=40784 點擊 下載自己對應的版本,安裝,一般可以解決。 ...
  • 轉載請標明出處:https://www.cnblogs.com/tangZH/p/12013685.html 在項目過程中,出現了一個需求,軟鍵盤要頂起部分控制項,而另一部分控制項不動。 關於這種需求,我們需要明確佈局方式: 1、線性佈局是行不通的,即使被頂上去也是全部被頂上去,因為線性佈局中裡面的控制項 ...
一周排行
    -Advertisement-
    Play Games
  • Timer是什麼 Timer 是一種用於創建定期粒度行為的機制。 與標準的 .NET System.Threading.Timer 類相似,Orleans 的 Timer 允許在一段時間後執行特定的操作,或者在特定的時間間隔內重覆執行操作。 它在分散式系統中具有重要作用,特別是在處理需要周期性執行的 ...
  • 前言 相信很多做WPF開發的小伙伴都遇到過表格類的需求,雖然現有的Grid控制項也能實現,但是使用起來的體驗感並不好,比如要實現一個Excel中的表格效果,估計你能想到的第一個方法就是套Border控制項,用這種方法你需要控制每個Border的邊框,並且在一堆Bordr中找到Grid.Row,Grid. ...
  • .NET C#程式啟動閃退,目錄導致的問題 這是第2次踩這個坑了,很小的編程細節,容易忽略,所以寫個博客,分享給大家。 1.第一次坑:是windows 系統把程式運行成服務,找不到配置文件,原因是以服務運行它的工作目錄是在C:\Windows\System32 2.本次坑:WPF桌面程式通過註冊表設 ...
  • 在分散式系統中,數據的持久化是至關重要的一環。 Orleans 7 引入了強大的持久化功能,使得在分散式環境下管理數據變得更加輕鬆和可靠。 本文將介紹什麼是 Orleans 7 的持久化,如何設置它以及相應的代碼示例。 什麼是 Orleans 7 的持久化? Orleans 7 的持久化是指將 Or ...
  • 前言 .NET Feature Management 是一個用於管理應用程式功能的庫,它可以幫助開發人員在應用程式中輕鬆地添加、移除和管理功能。使用 Feature Management,開發人員可以根據不同用戶、環境或其他條件來動態地控制應用程式中的功能。這使得開發人員可以更靈活地管理應用程式的功 ...
  • 在 WPF 應用程式中,拖放操作是實現用戶交互的重要組成部分。通過拖放操作,用戶可以輕鬆地將數據從一個位置移動到另一個位置,或者將控制項從一個容器移動到另一個容器。然而,WPF 中預設的拖放操作可能並不是那麼好用。為瞭解決這個問題,我們可以自定義一個 Panel 來實現更簡單的拖拽操作。 自定義 Pa ...
  • 在實際使用中,由於涉及到不同編程語言之間互相調用,導致C++ 中的OpenCV與C#中的OpenCvSharp 圖像數據在不同編程語言之間難以有效傳遞。在本文中我們將結合OpenCvSharp源碼實現原理,探究兩種數據之間的通信方式。 ...
  • 一、前言 這是一篇搭建許可權管理系統的系列文章。 隨著網路的發展,信息安全對應任何企業來說都越發的重要,而本系列文章將和大家一起一步一步搭建一個全新的許可權管理系統。 說明:由於搭建一個全新的項目過於繁瑣,所有作者將挑選核心代碼和核心思路進行分享。 二、技術選擇 三、開始設計 1、自主搭建vue前端和. ...
  • Csharper中的表達式樹 這節課來瞭解一下表示式樹是什麼? 在C#中,表達式樹是一種數據結構,它可以表示一些代碼塊,如Lambda表達式或查詢表達式。表達式樹使你能夠查看和操作數據,就像你可以查看和操作代碼一樣。它們通常用於創建動態查詢和解析表達式。 一、認識表達式樹 為什麼要這樣說?它和委托有 ...
  • 在使用Django等框架來操作MySQL時,實際上底層還是通過Python來操作的,首先需要安裝一個驅動程式,在Python3中,驅動程式有多種選擇,比如有pymysql以及mysqlclient等。使用pip命令安裝mysqlclient失敗應如何解決? 安裝的python版本說明 機器同時安裝了 ...