Android實現(xiàn)圖片一邊的三角形邊框效果
在每一個圖片的某一側(cè)都可以展示出一個三角形的邊框視圖,就是咱們的三角形標(biāo)簽視圖。這個視圖在電商類APP當(dāng)中比較常用,使用過ebay的同學(xué)應(yīng)該都還記得有些商品的左上角或者右上角都會顯示一個三角形的邊框,用于給人一個直觀的商品正在促銷,或者剛剛上線的直觀感受。我們可以看看實現(xiàn)后的效果如下:
在真實的APP當(dāng)中,我們還會加上一個SrcollView控件,這樣子才可以進行不斷地上下瀏覽。我們這里主要是為了讓大家明白這個視圖是該如何實現(xiàn)的,就不演示SrcollView控件下的做法了,直接在線性布局下做一個簡單的說明。由于在線性布局上面一共具有四張圖,因此咱們可以先單獨編寫每一個imageview的自定義view,然后<include>的語法將他們組合起來,這樣可以提高UI開發(fā)的效率,進行協(xié)同工作與開發(fā)。首先咱們先實現(xiàn)左上角和右上角的triangle view.
在build.gradle文件當(dāng)中相應(yīng)地方添加如下代碼,導(dǎo)入相應(yīng)的maven庫:
allprojects { repositories { ... maven { url "https://jitpack.io" } } }
之后在另一個build.gradle文件當(dāng)中添加庫:
dependencies { implementation 'com.github.shts:TriangleLabelView:1.1.2' }
咱們的前期工作就這樣做好啦,現(xiàn)在就開始正式編寫咱們的每一個三角形邊框視圖啦,首先是第一個位于左上角的視圖
一.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)中顯示如下:
下面是位于右上角的視圖
二.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處進行閱讀?。?!
帥照:
總結(jié)
以上所述是小編給大家介紹的Android實現(xiàn)圖片一邊的三角形邊框效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
上一篇:使用AccessibilityService實現(xiàn)微信自動切換賬號功能
欄 目:Android
下一篇:Android Canvas的drawText()與文字居中方案詳解
本文標(biāo)題:Android實現(xiàn)圖片一邊的三角形邊框效果
本文地址:http://mengdiqiu.com.cn/a1/Android/9006.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實現(xiàn)雙擊返回鍵退出應(yīng)用實現(xiàn)方法詳解
- 01-10android實現(xiàn)記住用戶名和密碼以及自動登錄
- 01-10android實現(xiàn)簡單計算器功能
- 01-10Android 友盟第三方登錄與分享的實現(xiàn)代碼
- 01-10C++自定義API函數(shù)實現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實現(xiàn)點特效
- 01-10android實現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實現(xiàn)圓形漸變加載進度條


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實現(xiàn)雙擊返回鍵退出應(yīng)用實現(xiàn)方
- 01-10android實現(xiàn)簡單計算器功能
- 01-10android實現(xiàn)記住用戶名和密碼以及自動
- 01-10C++自定義API函數(shù)實現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實現(xiàn)代
- 01-10android實現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實現(xiàn)點特效
- 01-10Android實現(xiàn)圓形漸變加載進度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改