Xfermode實現圓角矩形或者圓角圖片,xfermode圓角

來源:互聯網
上載者:User

Xfermode實現圓角矩形或者圓角圖片,xfermode圓角

最近一段時間學了很多關於圓角矩形或者圓角圖片製作的文章,寫的都很好,但是每次一學完都會做,但是過段時間又不記得該怎麼寫代碼了,反思了一下,是自己只是在看,並沒有真正的消化,所以還不算自己的東西,於是今天又把大神的代碼又看了看,自己總結,自己再寫一遍,算是明白了吧,也想寫個文章記錄一下,方便下次尋找,也可以分享出來。

Xfermode是android畫筆Paint可以設定的一種畫筆屬性,具體就是可以把兩張圖片進行組合,根據設定的形式,可以組合多種形式,具體大家看圖:

這裡用的就是SrcIn模式,大致思路就是先繪製一個圓或者圓角矩形,然後再繪製我們的圖片,然後根據上面的SrcIn模式,最終就繪製出了圓形圖片或者圓角矩形。android裡面把先繪製的圖片視為Src,後繪製的是Dst。

具體代碼如下,裡面說的很詳細了。

package com.fanxl.roundview;import android.annotation.SuppressLint;import android.content.Context;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.Mode;import android.graphics.PorterDuffXfermode;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;public class RoundView extends View {private Bitmap src;private Bitmap out;private int width;private int height;private int type;  private static final int TYPE_CIRCLE = 0;  private static final int TYPE_ROUND = 1; public RoundView(Context context) {this(context, null);}public RoundView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RoundView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);//自己計算控制項的寬高int widthSize = MeasureSpec.getSize(widthMeasureSpec);int widthMode = MeasureSpec.getMode(widthMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);if (widthMode == MeasureSpec.EXACTLY) {width = widthSize;} else {int imgWidth = src.getWidth() + getPaddingLeft()+ getPaddingRight();if (widthMode == MeasureSpec.AT_MOST) {width = Math.min(widthSize, imgWidth);} else {width = imgWidth;}}if (heightMode == MeasureSpec.EXACTLY) {height = heightSize;} else {int imgHeight = src.getHeight() + getPaddingTop()+ getPaddingBottom();if (heightMode == MeasureSpec.AT_MOST) {height = Math.min(heightSize, imgHeight);} else {height = imgHeight;}}//根據要繪製的類型設定最終自訂控制項的寬高switch (type) {case TYPE_CIRCLE:int min = Math.min(width, height);setMeasuredDimension(min, min);break;case TYPE_ROUND:setMeasuredDimension(width, height);break;}}@SuppressLint("NewApi")private void initView() {// 禁止硬體加速,硬體加速會有一些問題,這裡禁用掉setLayerType(LAYER_TYPE_SOFTWARE, null);src = BitmapFactory.decodeResource(getResources(), R.drawable.dd);//設定類型,是圓角圖片還是圓角矩形type = TYPE_CIRCLE;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);xmodeImage();//把畫好畫的畫布放到自訂的畫板上面canvas.drawBitmap(out, 0, 0, null);}private void xmodeImage() {//根據原始的圖片建立一個畫布out = Bitmap.createBitmap(width, height, Config.ARGB_8888);//建立一個畫板,在畫布的基礎上Canvas canvas = new Canvas(out);//建立一個畫筆Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);switch (type) {case TYPE_ROUND://開始在有畫板的畫布上用畫筆作畫了,這裡畫了一個圓角矩形canvas.drawRoundRect(new RectF(0, 0, width, height),60, 60, paint);break;case TYPE_CIRCLE://畫圓,取寬高的最小值作為圓的直徑int min = Math.min(width, height);//開始畫圓canvas.drawCircle(min/2, min/2, min/2, paint);break;}//設定Xfermode畫筆模式為SRC_INpaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));//然後有畫了一個圖片,最終實現兩個映像的疊加canvas.drawBitmap(src, 0, 0, paint);}}

最終效果:



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.