Android 繪製圓角圖片(圓形圖片,圓角矩形圖片,圓角正方形圖片)【轉】
我們在Android應用中經常要繪製圓角的圖片來展示一些背景圖,但是每次都製作圓角的圖片很麻煩,而且重複使用率不高。所以我們最好的就是在應用中根據已有的圖片,動態的繪製所需要的圓角圖片用於顯示。話不多說,讓我們先看看效果圖:
這是例子中使用到的圖片:
怎麼樣,效果很好吧。
實現繪製圓角圖片的原理很簡單,就是在程式中動態生成一張Bitmap,然後再用Paint在這張Bitmap中繪製所需的圖形(如圓形),再根據paint的setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN))屬性設定在Bitmap中畫的圓形和所要顯示的圖片的交集,並取圖片部分,那樣就生成了圓形的圖片。“
其中Mode取值有如下一些值:
- 1.PorterDuff.Mode.CLEAR
- 所繪製不會提交到畫布上。
- 2.PorterDuff.Mode.SRC
- 顯示上層繪製圖片
- 3.PorterDuff.Mode.DST
- 顯示下層繪製圖片
- 4.PorterDuff.Mode.SRC_OVER
- 正常繪製顯示,上下層繪製疊蓋。
- 5.PorterDuff.Mode.DST_OVER
- 上下層都顯示。下層居上顯示。
- 6.PorterDuff.Mode.SRC_IN
- 取兩層繪製交集。顯示上層。
-
7.PorterDuff.Mode.DST_IN
- 取兩層繪製交集。顯示下層。
- 8.PorterDuff.Mode.SRC_OUT
- 取上層繪製非交集部分。
- 9.PorterDuff.Mode.DST_OUT
- 取下層繪製非交集部分。
- 10.PorterDuff.Mode.SRC_ATOP
- 取下層非交集部分與上層交集部分
- 11.PorterDuff.Mode.DST_ATOP
- 取上層非交集部分與下層交集部分
- 12.PorterDuff.Mode.XOR
- 取兩層繪製非交集。兩層繪製非交集。
- 13.PorterDuff.Mode.DARKEN
- 上下層都顯示。變暗
-
14.PorterDuff.Mode.LIGHTEN
- 上下層都顯示。變數
- 15.PorterDuff.Mode.MULTIPLY
- 取兩層繪製交集
- 16.PorterDuff.Mode.SCREEN
- 上下層都顯示。
原理就是這樣,接下來我們看看示例程式碼怎麼應用:
第一:先看佈局檔案activity_main.xml
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:id="@+id/source"
- android:layout_width="100dip"
- android:layout_height="100dip"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="20dip"
- android:layout_marginTop="20dip"
- android:src="@drawable/demo"/>
- <ImageView
- android:id="@+id/circle"
- android:layout_width="100dip"
- android:layout_height="100dip"
- android:layout_centerHorizontal="true"
- android:layout_below="@id/source"
- android:layout_marginBottom="20dip"/>
- <ImageView
- android:id="@+id/rect"
- android:layout_width="100dip"
- android:layout_height="100dip"
- android:layout_below="@id/circle"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="20dip"/>
- <ImageView
- android:id="@+id/squre"
- android:layout_width="100dip"
- android:layout_height="100dip"
- android:layout_below="@id/rect"
- android:layout_centerHorizontal="true"/>
- </RelativeLayout>
這裡定義了四個Imageview,第一個為原圖,第二個為圓形圖片,第三個為圓角矩形,第四個為圓角正方形。
第二:為了節省空間,我就把生成圖片的程式碼寫在了MainActivity中,如下,註釋都很清楚:
- package com.demo.imagetype;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.PorterDuffXfermode;
- import android.graphics.RectF;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.widget.ImageView;
- publicclass MainActivity extends Activity {
- private ImageView circle;
- private ImageView rect;
- private ImageView squre;
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- circle = (ImageView)findViewById(R.id.circle);
- rect = (ImageView)findViewById(R.id.rect);
- squre = (ImageView)findViewById(R.id.squre);
- /** 獲取圖片資源並轉換為 Bitmap型別 */
- Drawable drawable = getResources().getDrawable(R.drawable.demo);
- BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
- Bitmap bitmap = bitmapDrawable.getBitmap();
- circle.setImageBitmap(getCircleBitmap(bitmap));
- rect.setImageBitmap(drawRect(bitmap, 80));
- squre.setImageBitmap(drawSqureRect(bitmap, 80));
- }
- /**
- * 繪製圓形
- * @param source
- * @return
- */
- publicstatic Bitmap getCircleBitmap(Bitmap source)
- {
- // 以最小的邊為圓的半徑
- int min = 0;
- if(source.getWidth() < source.getHeight())
- {
-
相關推薦
realloc 使用詳解(分析realloc invalid pointer、指針無效等錯誤)【轉】
strong 開源 堆區 找到 失敗 temp idt googl 發生 來源:http://www.cnblogs.com/ladd/archive/2012/06/30/2571420.htmlrealloc函數用來為ptr重新分配大小為size的一塊內存,看似很簡單,
Android 繪製圓角圖片(圓形圖片,圓角矩形圖片,圓角正方形圖片)【轉】
我們在Android應用中經常要繪製圓角的圖片來展示一些背景圖,但是每次都製作圓角的圖片很麻煩,而且重複使用率不高。所以我們最好的就是在應用中根據已有的圖片,動態的繪製所需要的圓角圖片用於顯示。話不多說,讓我們先看看效果圖: 這是例子中使用到的圖
Android--利用相機或相簿擷取使用者頭像(解決了miui無法擷取,以及部分機型拍照無返回Uri)【上】
宣告:本文的Demo可用於從本地獲取使用者頭像時使用,解決了有些手機系統相機拍照後獲取不到拍攝照片的問題,以及解決小米miui系統呼叫系統裁剪圖片功能camera.action.CROP後崩潰或重新開啟app的問題。 主活動檔案ChooseImageMain
Android--利用相機或相簿擷取使用者頭像(解決了miui無法擷取,以及部分機型拍照無返回Uri)【下】
宣告 本文的Demo可用於從本地獲取使用者頭像時使用,解決了有些手機系統相機拍照後獲取不到拍攝照片的問題,以及解決小米miui系統呼叫系統裁剪圖片功能camera.action.CROP後崩潰或重新開啟app的問題。 修改了部分機型拍照後返回的是縮圖的臨時
Source Insight 中文註釋為亂碼解決辦法(完美解決,一鍵搞定)【轉】
ash save sys lan sim edit ext pan character 轉自:http://blog.csdn.net/bjarnecpp/article/details/70174752 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 S
oracle(使用TRUACTE刪除數據時,提示資源正忙)【轉】
ESS 正常 HERE sign order by where terminal spi ora 查詢記錄的鎖定對象,select * from v$locked_object(身份必須是DBA權限),然後找到對應的sid和serial#殺掉了會話;
Android 屬性動畫(Property Animation) 完全解析 (上)【轉】
轉載請標明出處:http://blog.csdn.net/lmj623565791/article/details/38067475 1、概述 Android提供了幾種動畫型別:View Animation 、Drawable Animation 、Property Anima
linux驅動由淺入深系列:PBL-SBL1-(bootloader)LK-Android啟動過程詳解之一(高通MSM8953啟動例項)【轉】
本文轉載自:https://blog.csdn.net/radianceblau/article/details/73229005 對於嵌入式工程師瞭解晶片啟動過程是十分有必要的,在分析、除錯各種問題的時候都有可能涉及到這方面的知識。同時這部分知識也是比較複雜的,因為其中涉及到晶片內部架構,啟動各個階段軟體
【Ubuntu】Windows 遠端桌面連線ubuntu及xrdp的一些小問題(遠端桌面閃退、連線失敗、tab補全功能,無選單欄,error - problem connecting )【轉】
轉:https://blog.csdn.net/u014447845/article/details/80291678 1、遠端桌面閃退,shell可以用的問題:(1)需要在該使用者目錄建立一個.xsession:touch .xsession(2)裡面寫“xfce4-session”一句話就行:echo
Android在標準linux基礎上對休眠喚醒的實現(一)(二)(三)【轉】
說明: 1. Based on linux 2.6.32 and android 2.2,only support SDR(mem). 2. 參考文章: 一、新增特性介紹 實際上,android仍然是利用了標準linux的休眠喚醒系統,只不過添加了一些使用
python編程(python開發的三種運行模式)【轉】
阻塞 data tail 驗證 目錄 pro 什麽 read bus 轉自:http://blog.csdn.net/feixiaoxing/article/details/53980886 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 目錄(?)[-]
Spark SQL編程指南(Python)【轉】
res 平臺 per 它的 split 執行 文件的 分組 不同 轉自:http://www.cnblogs.com/yurunmiao/p/4685310.html 前言 Spark SQL允許我們在Spark環境中使用SQL或者Hive SQL執行關系型查詢。它的核
100行代碼實現最簡單的基於FFMPEG+SDL的視頻播放器(SDL1.x)【轉】
工程 全屏 升級版 gin avcodec ive 系列文章 相同 hello 轉自:http://blog.csdn.net/leixiaohua1020/article/details/8652605 版權聲明:本文為博主原創文章,未經博主允許不得轉載。
常用牛人主頁鏈接(計算機視覺、模式識別、機器學習相關方向,陸續更新。。。。)【轉】
short psu works charles 貝葉斯 learning 數學 ocr 相關 轉自:http://blog.csdn.net/goodshot/article/details/53214935 目錄(?)[-] The Kalman
git代碼倉庫遷移(從github到oschina)【轉】
mit tail name usr done git blank com detail 轉自:http://blog.csdn.net/a5244491/article/details/44807937 版權聲明:本文為博主原創文章,未經博主允許不得轉載。
Linux內核中斷引入用戶空間(異步通知機制)【轉】
sso mis 文件 ack 文件的 ssi dsm 進行 log 轉自:http://blog.csdn.net/kingdragonfly120/article/details/10858647 版權聲明:本文為博主原創文章,未經博主允許不得轉載。
linux 系統函數之 (dirname, basename)【轉】
bsp spa csdn 跨平臺 dirname 轉載 nbsp 描述 dir 轉自:http://blog.csdn.net/peter_cloud/article/details/9308333 版權聲明:本文為博主原創文章,未經博主允許不得轉載。
C++ operator(重載操作符) 【轉】
整體 比較大小 對象的比較 剛才 ret 使用方法 運算符 等等 內置 轉自:http://www.cnblogs.com/xiangxiaodong/archive/2012/02/12/2348144.html operator是C++的關鍵字,它和運算符
ArcGIS Server10.1之服務新特性(WMTS1.0.0) 【轉】
class href 知識 技術分享 restful cgi art 存在 alt http://blog.csdn.net/esrichinacd/article/details/7825587 ArcGIS Server10.1正式支持OGC的WMTS1.0.0版
【前端小小白的學習之路】整理幾道面試題之(HTTP協議)【轉】
ase 賬號 檢測 提交數據 大型數據集 tor 添加 描述 分享 轉自:http://www.cnblogs.com/ranyonsue/p/5984001.html HTTP簡介 HTTP協議是Hyper Text Transfer Protocol(超文本傳輸