安卓中遮罩圖片的處理

來源:互聯網
上載者:User

標籤:widget   graph   pop   form   XML   資源檔   重要   nts   部分   

安卓開發中必不可少的就是各種圖片的圓角,遮罩等等。

曾經我都是用shape處理的。發現背景圖的圓角要設定成和介面父元素背景一樣才幹看不出現紕漏。

當遇到背景多變的情況,比方listview隔行背景顏色不同的情況就鬱悶了。又要加一堆代碼處理。

如今有一個方法能夠統一處理,就是用畫布Canvas,代碼和方法例如以下:

方法步驟說明:

1、畫布Canvas

2、在畫布上把要顯示的圖畫上去

3、把遮罩圖片畫上去,蓋在要顯示的圖片上面

4、對這個遮罩圖片設定一個非常重要的屬性PorterDuff.Mode.DST_IN。意思就是。圖片重疊的部分顯示以下的圖片。

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));mCanvas.drawBitmap(mask, 0, 0, paint);

實現代碼:

1、配置attrs.xml

<?

xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MaskImage"> <!-- Identifier for the image that represents the Imageview‘s content. --> <attr name="image" format="reference" /> <!-- crop Imageview‘s content same as mask --> <attr name="mask" format="reference" /> </declare-styleable></resources>

2、建立一個MaskImage類繼承ImageView

package com.xzw.mask.widget;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.util.AttributeSet;import android.widget.ImageView;public class MaskImage extends ImageView{int mImageSource=0;int mMaskSource=0; RuntimeException mException;public MaskImage(Context context, AttributeSet attrs) {super(context, attrs);TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MaskImage, 0, 0);mImageSource = a.getResourceId(R.styleable.MaskImage_image, 0);mMaskSource = a.getResourceId(R.styleable.MaskImage_mask, 0);if (mImageSource == 0 || mMaskSource == 0) {mException = new IllegalArgumentException(a.getPositionDescription() + ": The content attribute is required and must refer to a valid image.");}if (mException != null) throw mException;       /**        * 主要代碼實現        *///擷取圖片的資源檔Bitmap original = BitmapFactory.decodeResource(getResources(), mImageSource);//擷取遮罩層圖片Bitmap mask = BitmapFactory.decodeResource(getResources(), mMaskSource);Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);     //將遮罩層的圖片放到畫布中Canvas mCanvas = new Canvas(result);Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));//疊加反覆的部分,顯示以下的mCanvas.drawBitmap(original, 0, 0, null);mCanvas.drawBitmap(mask, 0, 0, paint);paint.setXfermode(null);setImageBitmap(result);setScaleType(ScaleType.CENTER); a.recycle();}}

3、在layout布局檔案裡使用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:maskimage="http://schemas.android.com/apk/res/com.xzw.mask"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="下面為正常圖片" />    <!-- 無遮罩圖片 -->    <ImageView        android:id="@+id/imageview1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/icon_t" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="下面為遮罩圖片變成圓形的圖片" />        <!-- 有遮罩圖片 -->     <com.xzw.mask.widget.MaskImage        android:id="@+id/imageview_id"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        maskimage:image="@drawable/icon_t"        maskimage:mask="@drawable/circle" /></LinearLayout>
注意布局中間中添加的命名空間:xmlns:maskimage="http://schemas.android.com/apk/res/com.xzw.mask"


以下附上使用到的圖片和遮罩圖片,原理想必大家看到以下兩個圖片就都明確了。









安卓中遮罩圖片的處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.