圆形ImageView
2016-05-25 20:40
239 查看
自定义控件实现圆形ImageView
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; /** * 作者:${孙淇文} on 2016/6/16 14:16 * 邮箱:nice_sqw@163.com */ public class MyRoundedImageView extends ImageView{ // // //中间值用来设置用户头像的颜色 public int BACK_DEFAULT = Color.parseColor("#95d3f5"); //当前的用户的名字 public String username = ""; //设置当前的字段内容 public void addTile(String username) { this.username = username; invalidate(); } //设置字体颜色的 public void addTile(String username, int namecolor) { this.username = username; this.BACK_DEFAULT = namecolor; invalidate(); } public MyRoundedImageView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyRoundedImageView(Context context, AttributeSet attrs) { super(context, attrs); } public MyRoundedImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { Paint line = new Paint(); line.setColor(Color.WHITE); line.setTextSize(getWidth() / 2); line.setTextAlign(Paint.Align.CENTER); Paint.FontMetrics fontMetrics = line.getFontMetrics(); //计算文字高度 float fontHeight = fontMetrics.bottom - fontMetrics.top; //计算文字baseline float textBaseY = getHeight() - (getHeight() - fontHeight) / 2 - fontMetrics.bottom; canvas.drawText("", getWidth() / 4, getHeight() / 2 + getWidth() / 4, line); int w = getWidth(), h = getHeight(); //画圆圈的画笔 Paint circle = new Paint(); circle.setColor(BACK_DEFAULT); circle.setStrokeWidth(w / 2); //设置圆环的宽度 circle.setAntiAlias(true); //消除锯齿 Drawable drawable = getDrawable(); if (drawable == null) { return; } if (getWidth() == 0 || getHeight() == 0) { return; } if (!"".equals(username) && null != username) { //画自定义的圆 canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2, circle); if (username.length() > 0) { Rect targetRect = new Rect(50, 50, 1000, 200); float strwidth = line.measureText(username.substring(0, 1)); Paint.FontMetrics fm = line.getFontMetrics(); float hight = (float) Math.ceil(fm.descent - fm.ascent); if (username.substring(0, 1).matches("^[a-zA-Z]*")) { if (username.length() >= 2) { strwidth = line.measureText(username.substring(0, 1)); if (username.substring(0, 2).matches("^[a-zA-Z]*")) { // strwidth = line.measureText(username.substring(0, 2)); // canvas.drawLine(0, (float) ((getHeight()) / 2 - (hight / 2) * 0.7), getWidth(), (float) ((getHeight()) / 2 - (hight / 2) * 0.7), line); // canvas.drawLine(0, (getHeight()) / 2, getWidth(), (getHeight()) / 2, line); // canvas.drawLine(0, (float) ((getHeight()) / 2 + (hight / 2) * 0.7), getWidth(), (float) ((getHeight()) / 2 + (hight / 2) * 0.7), line); canvas.drawText(username.substring(0, 1), (getWidth()) / 2, (float) ((getHeight() - hight) / 2 + hight * 0.7), line); } else { canvas.drawText(username.substring(0, 1), getWidth() / 2, (float) ((getHeight() - hight) / 2 + hight * 0.7), line); } } else { canvas.drawText(username.substring(0, 1), (getWidth()) / 2, (float) ((getHeight() - hight) / 2 + hight * 0.7), line); } } else { canvas.drawText(username.substring(0, 1), (getWidth()) / 2, (getHeight()) / 2 + hight / 4, line); } } else { canvas.drawText("", getWidth() / 4, (float) ((getHeight() / 4) * 2.8), line); } } else { BitmapDrawable bd = (BitmapDrawable) drawable; Bitmap b = bd.getBitmap(); Bitmap bitmap = b.copy(Config.ARGB_8888, true); Bitmap roundBitmap = getCroppedBitmap(bitmap, w); canvas.drawBitmap(roundBitmap, 0, 0, null); canvas.drawText("", getWidth() / 4, getHeight() / 2 + getWidth() / 4 - 20, line); } } public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) { Bitmap sbmp; if (bmp.getWidth() != radius || bmp.getHeight() != radius) sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false); else sbmp = bmp; Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffa19774; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight()); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.parseColor("#BAB399")); canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(sbmp, rect, rect, paint); return output; } //获取文字的高度的方法 public int getHigh(float fontSize) { Paint paint = new Paint(); paint.setTextSize(fontSize); Paint.FontMetrics fm = paint.getFontMetrics(); return (int) Math.ceil(fm.descent - fm.ascent); } //获取文字的宽度的方法 public float getWidth(String displayText) { Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(Color.WHITE); // Define the string. // Measure the width of the text string. float textWidth = mTextPaint.measureText(displayText); return textWidth; } }
相关文章推荐
- Android打造带透明圆弧的ImageView
- flex 控件的重要属性
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- Android编程UI设计之GridView和ImageView的用法
- Android编程之Button控件用法实例分析
- Android控件之CheckBox、RadioButton用法实例分析
- 在Android开发中使用自定义组合控件的例子
- 一款超酷的Android自定义加载控件
- 基于Android实现随手指移动的ImageView
- Android使用ImageView 制作透明圆弧实例代码
- Android重写View实现全新的控件
- Android中ImageView用法实例分析
- MFC中动态创建控件以及事件响应实现方法
- WinForm自定义函数FindControl实现按名称查找控件
- Android控件之ProgressBar用法实例分析
- WinForm拖拽控件生成副本的解决方法
- ASP.NET动态添加用户控件的方法