效果图:

第一步:新建RoundView自定义控件继承View

packagecom.rong.activity;importcom.rong.test.R;importandroid.content.Context;importandroid.graphics.Bitmap;importandroid.graphics.Bitmap.Config;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.PorterDuff;importandroid.graphics.PorterDuffXfermode;importandroid.util.AttributeSet;importandroid.widget.ImageView;/***利用Xfermode制作一个圆形图片**@author徐荣**/publicclassRoundViewextendsImageView{/***画笔*/privatePaintmPaint;/***原型图*/privateBitmapsrc;/***遮罩层*/privateBitmapmask;publicRoundView(Contextcontext,AttributeSetattrs){super(context,attrs);//初始化画笔mPaint=newPaint();mPaint.setAntiAlias(true);//拿到原型图src=BitmapFactory.decodeResource(getResources(),R.drawable.ml);}@OverrideprotectedvoidonSizeChanged(intw,inth,intoldw,intoldh){super.onSizeChanged(w,h,oldw,oldh);//图片的遮罩,为什么要在这里面初始化遮罩层呢?因为在这个方法里Width()和Height()才被初始化了mask=Bitmap.createBitmap(getMeasuredWidth(),getMeasuredHeight(),Config.ARGB_4444);}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);//设置画布的颜色为透明canvas.drawColor(Color.TRANSPARENT);//划出你要显示的圆Canvascc=newCanvas(mask);cc.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,getMeasuredHeight()/2,mPaint);//这个方法相当于PS新建图层,下面你要做的事就在这个图层上操作canvas.saveLayer(0,0,getMeasuredWidth(),getMeasuredHeight(),null,Canvas.ALL_SAVE_FLAG);//先绘制遮罩层canvas.drawBitmap(mask,0,0,mPaint);//设置混合模式mPaint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_IN));//再绘制src源图canvas.drawBitmap(src,0,0,mPaint);//还原混合模式mPaint.setXfermode(null);//还原画布,相当于Ps的合并图层canvas.restore();}}

第二步 新建布局文件Activity_main.xml

<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffffff"android:orientation="vertical"><com.rong.activity.RoundViewandroid:layout_width="160dp"android:layout_height="160dp"android:layout_centerInParent="true"/></RelativeLayout>


Run

更多相关文章

  1. 安卓(Android)九宫格布局介绍
  2. 一个Android实现图片的倒影效果的代码示例
  3. android之PULL解析xml文档
  4. Android向Http服务器发送Http请求异常-UnknowHostException
  5. android -------- MVP+DataBinding 的使用
  6. Android使用Fragment嵌套Fragment的模式实现界面滑动
  7. 新建android opengl工程
  8. android Draw Rect 坐标图示
  9. Watchdog 工作机制解析

随机推荐

  1. 【转】Android -- 探究Android的多分辨率
  2. 拥抱Android之前世今生
  3. Android(安卓)修改现有ROM资源文件如何实
  4. Android实现ssl双向验证
  5. Android日志输出
  6. 饿了么点餐源码、今日头条源码 等
  7. Android进度条ProgressBar颜色的渐变设置
  8. iOS Tableview侧滑删除和移动cell的实现
  9. 【 Android(安卓)】RecyclerView 使用方
  10. Android 学习记录三:广播