Android自訂view實現圓形、圓角和橢圓圖片(BitmapShader圖形渲染)_Android

來源:互聯網
上載者:User

一、前言

Android實現圓角矩形,圓形或者橢圓等圖形,一般主要是個自訂View加上使用Xfermode實現的。實現圓角圖片的方法其實不少,常見的就是利用XfermodeShader。本文直接繼承ImageView,使用BitmapShader方法來實現圓形、圓角和橢圓的繪製,等大家看我本文的方法後,其他的類似形狀也就都能舉一反三來來畫出來了。

二、效果圖:

三、BitmapShader簡介

BitmapShaderShader的子類,可以通過Paint.setShader(Shader shader)進行設定、

我們這裡只關注BitmapShader,構造方法:

mBitmapShader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);

參數1:bitmap

參數2,參數3:TileMode;

TileMode的取值有三種:

     CLAMP 展開

     REPEAT 重複

     MIRROR 鏡像

如果大家給電腦螢幕設定屏保的時候,如果圖片太小,可以選擇重複、展開、鏡像;

重複:就是橫向、縱向不斷重複這個bitmap

鏡像:橫向不斷翻轉重複,縱向不斷翻轉重複;

展開:這個和電腦屏保的模式應該有些不同,這個展開的是圖片最後的那一個像素;橫向的最後一個橫行像素,不斷的重複,縱項的那一列像素,不斷的重複;

public   BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)

調用這個方法來產生一個畫有一個位元影像的渲染器(Shader)。

bitmap   在渲染器內使用的位元影像

tileX      The tiling mode for x to draw the bitmap in.   在位元影像上X方向花磚模式

tileY     The tiling mode for y to draw the bitmap in.    在位元影像上Y方向花磚模式

TileMode:(一共有三種)

CLAMP  :如果渲染器超出原始邊界範圍,會複製範圍內邊緣染色。

REPEAT :橫向和縱向的重複渲染器圖片,平鋪。

MIRROR :橫向和縱向的重複渲染器圖片,這個和REPEAT 重複方式不一樣,他是以鏡像方式平鋪。

四、自訂圓形、圓角和橢圓的圖片View的實現

1. 測量View的大小

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  // TODO Auto-generated method stub  super.onMeasure(widthMeasureSpec, heightMeasureSpec);  // 如果是繪製圓形,則強制寬高大小一致  if (mType == TYPE_CIRCLE) {   mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());   mRadius = mWidth / 2;   setMeasuredDimension(mWidth, mWidth);  } }

2、設定BitmapShader和畫筆Paint

/**  * 設定BitmapShader  */ private void setBitmapShader() {  Drawable drawable = getDrawable();  if (null == drawable) {   return;  }  Bitmap bitmap = drawableToBitmap(drawable);  // 將bitmap作為著色器來建立一個BitmapShader  mBitmapShader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);  float scale = 1.0f;  if (mType == TYPE_CIRCLE) {   // 拿到bitmap寬或高的小值   int bSize = Math.min(bitmap.getWidth(), bitmap.getHeight());   scale = mWidth * 1.0f / bSize;  } else if (mType == TYPE_ROUND || mType == TYPE_OVAL) {   // 如果圖片的寬或者高與view的寬高不匹配,計算出需要縮放的比例;縮放後的圖片的寬高,一定要大於我們view的寬高;所以我們這裡取大值;   scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight());  }  // shader的變換矩陣,我們這裡主要用於放大或者縮小  mMatrix.setScale(scale, scale);  // 設定變換矩陣  mBitmapShader.setLocalMatrix(mMatrix);  mPaint.setShader(mBitmapShader); }

3.最後就是繪製出來圓角、圓形和橢圓的圖片,肯定在onDraw裡面啦,根本原理就是使用了上面mBitmapShader渲染的畫筆來繪製

@Override protected void onDraw(Canvas canvas) {  if (null == getDrawable()) {   return;  }  setBitmapShader();  if (mType == TYPE_CIRCLE) {   canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);  } else if (mType == TYPE_ROUND) {   mPaint.setColor(Color.RED);   canvas.drawRoundRect(mRect, mRoundRadius, mRoundRadius, mPaint);  }else if(mType == TYPE_OVAL){   canvas.drawOval(mRect, mPaint);  } }

五、視圖布局實現

這個很簡單,就是3個自訂的view

<ScrollView xmlns: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" tools:context=".MainActivity" > <LinearLayout  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:gravity="center_horizontal"  android:layout_marginTop="5dp"  android:layout_marginBottom="25dp"  android:orientation="vertical" >  <com.czm.viewdrawtest.XCRoundAndOvalImageView   android:id="@+id/cicleImageView"   android:layout_width="200dp"   android:layout_height="200dp"   android:src="@drawable/img1" />  <com.czm.viewdrawtest.XCRoundAndOvalImageView   android:id="@+id/roundRectImageView"   android:layout_width="200dp"   android:layout_height="240dp"   android:layout_marginTop="5dp"   android:src="@drawable/img2" />  <com.czm.viewdrawtest.XCRoundAndOvalImageView   android:id="@+id/ovalImageView"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_marginTop="5dp"   android:src="@drawable/img3" /> </LinearLayout></ScrollView>

六、使用和測試自訂View

上面直接繪製的自訂View寫完了,下面就是使用這個View了,使用方法和普通的ImageView一樣,當作普通控制項使用即可。

package com.czm.viewdrawtest;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;/** * 使用自訂ImageView * @author caizhiming * */public class MainActivity extends Activity { private XCRoundAndOvalImageView circleImageView;//圓形圖片 private XCRoundAndOvalImageView roundRectImageView;//圓角矩形圖片 private XCRoundAndOvalImageView ovalImageView;//橢圓圖片 @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  //設定無標題   requestWindowFeature(Window.FEATURE_NO_TITLE);   //設定全屏   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,     WindowManager.LayoutParams.FLAG_FULLSCREEN);   setContentView(R.layout.activity_main);    initViews(); } /**  * 初始化Views  */ private void initViews(){  circleImageView = (XCRoundAndOvalImageView)findViewById(R.id.cicleImageView);  roundRectImageView = (XCRoundAndOvalImageView)findViewById(R.id.roundRectImageView);  ovalImageView = (XCRoundAndOvalImageView)findViewById(R.id.ovalImageView);    roundRectImageView.setType(XCRoundAndOvalImageView.TYPE_ROUND);  roundRectImageView.setRoundRadius(100);    ovalImageView.setType(XCRoundAndOvalImageView.TYPE_OVAL);  ovalImageView.setRoundRadius(50);   }}

七、總結

以上就是本文的全部內容,希望這篇文章的內容對大家開發Android能有所協助。

聯繫我們

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