Android圆形图片和圆角图片的绘制
来源:互联网 发布:电脑看书软件 编辑:程序博客网 时间:2024/05/16 00:32
转载请标明出处:http://blog.csdn.NET/lmj623565791/article/details/24555655 ////////圆角图片绘制
FROM GA_studio http://blog.csdn.net/tianjian4592 //////////drawBitmap讲解
自定义ImageView
import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.RectF;import android.graphics.Bitmap.Config;import android.util.AttributeSet;import android.util.Log;import android.util.TypedValue;import android.view.View;import android.view.View.MeasureSpec;import com.aynu.circleimageview.R;/** * * @author zhy * */public class CustomImageView extends View{ /** * TYPE_CIRCLE / TYPE_ROUND */ private int type; private static final int TYPE_CIRCLE = 0; private static final int TYPE_ROUND = 1; /** *bitmap */ private Bitmap mSrc; /** *半径 */ private int mRadius; /** * �ؼ�长度� */ private int mWidth; /** * �高度� */ private int mHeight; public CustomImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomImageView(Context context) { this(context, null); } /** * �获取属性值�� * * @param context * @param attrs * @param defStyle */ public CustomImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyle, 0); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.CustomImageView_src://获取图片,自定义属性,设置图片并获取 mSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0)); break; case R.styleable.CustomImageView_type://获取图片要设置圆形还是圆角图片 type = a.getInt(attr, 0); break; case R.styleable.CustomImageView_borderRadius://半径值 mRadius = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics())); break; } } a.recycle(); } /** * ����ؼ��测量� */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec, heightMeasureSpec); /** * ���获取宽度测量模式和大小� */ int specMode = MeasureSpec.getMode(widthMeasureSpec); int specSize = MeasureSpec.getSize(widthMeasureSpec); if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate精确的 { mWidth = specSize; } else { // � int desireByImg = getPaddingLeft() + getPaddingRight() + mSrc.getWidth();//图片的大小,和测量出来的大小比较 if (specMode == MeasureSpec.AT_MOST)// wrap_content { mWidth = Math.min(desireByImg, specSize); } } /*** * ��获取高度的测量模式� */ specMode = MeasureSpec.getMode(heightMeasureSpec); specSize = MeasureSpec.getSize(heightMeasureSpec); if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate { mHeight = specSize; } else { int desire = getPaddingTop() + getPaddingBottom() + mSrc.getHeight(); if (specMode == MeasureSpec.AT_MOST)// wrap_content { mHeight = Math.min(desire, specSize); } } setMeasuredDimension(mWidth, mHeight); } /** * ���� */ @Override protected void onDraw(Canvas canvas) { switch (type) { // ���圆形图片 case TYPE_CIRCLE: int min = Math.min(mWidth, mHeight); /** * 使得当前的图片的宽和高一致Bitmap */ mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false); //绘制圆角图片 canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null); break; case TYPE_ROUND: //圆角图片 canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null); break; } } /** * * @param source * @param min * @return */ private Bitmap createCircleImage(Bitmap source, int min) { //画笔 final Paint paint = new Paint(); paint.setAntiAlias(true); //创建Bitmap对象 Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888); /** * 绘制� */ Canvas canvas = new Canvas(target); /** * 画圆 */ canvas.drawCircle(min / 2, min / 2, min / 2, paint); /** * ʹ��设置mode���˵�� */ paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); /** * ���画图像 */ canvas.drawBitmap(source, 0, 0, paint); return target; } /** * �� * * @param source * @return */ private Bitmap createRoundConerImage(Bitmap source) { final Paint paint = new Paint(); paint.setAntiAlias(true); //创建bitmap Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888); //绘制图像 Canvas canvas = new Canvas(target); RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight()); canvas.drawRoundRect(rect, 50f, 50f, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //图片,大小,绘制的地方 canvas.drawBitmap(source, 0, 0, paint); return target; }}
activity_main.xml文件:
<LinearLayout 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" xmlns:zhy="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:src="@mipmap/icon" /> <com.aynu.circleimageview.CustomImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" zhy:src="@mipmap/icon" zhy:type="circle" /> <com.aynu.circleimageview.CustomImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" zhy:borderRadius="10dp" zhy:src="@mipmap/icon" zhy:type="round" /> <com.aynu.circleimageview.CustomImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" zhy:borderRadius="20dp" zhy:src="@mipmap/icon" zhy:type="round" /></LinearLayout>
res/attrs文件
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="borderRadius" format="dimension" /> <attr name="type"> <enum name="circle" value="0" /> <enum name="round" value="1" /> </attr> <attr name="src" format="reference"></attr> <declare-styleable name="CustomImageView"> <attr name="borderRadius" /> <attr name="type" /> <attr name="src" /> </declare-styleable> </resources>
代码下载地址:
http://download.csdn.net/detail/lmj623565791/7269005
0 0
- Android圆形图片和圆角图片的绘制
- Android中绘制圆形和圆角图片
- android绘制圆形圆角图片
- android 绘制圆形图片
- Android 绘制圆形图片
- Android 绘制圆形图片
- Android绘制圆形图片的方法总结
- Android绘制圆形图片的方法总结
- Android 绘制圆角图片(圆形图片,圆角矩形图片,圆角正方形图片)
- Android自定义圆形和圆角图片
- android实现图片圆角和圆形
- Android Drawable 自定义(圆角图片和圆形图片)
- Android圆角图片和圆形图片实现总结
- Android圆角图片和圆形图片实现
- android中用Fresco实现圆角图片和圆形图片
- Android Picasso实现圆形图片和圆角图片
- android中用Fresco实现圆角图片和圆形图片
- Android 圆形/圆角图片的方法
- 利用SSE求取水平与垂直梯度图(笔记)
- 04-【解答疑惑篇】什么情况下需要构建网络数据集?
- View或Layout快照保存、截屏功能
- 为Navigation Header上的控件添加监听事件
- 如何在火狐浏览器添加Poster工具
- Android圆形图片和圆角图片的绘制
- Android Butterknife 8.4.0 使用方法总结
- 软件项目文档及其必要性
- 带新手走进神秘的HTTP协议
- 管理员权限程序以普通用户的权限运行不需要管理员权限的程序
- linux的nohup命令的用法
- 大型网站架构之分布式消息队列
- 个人站长之路,痛下决心换云服务器
- HTTP与HTTPS握手的那些事