ImageView圆角
来源:互联网 发布:淘宝卖自家产大米 编辑:程序博客网 时间:2024/06/18 15:06
activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:CustomImageView="http://schemas.android.com/apk/res/com.example.customview05imageview" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <com.example.customview05imageview.RoundImageView android:layout_width="wrap_content" android:layout_margin="5dp" android:layout_height="wrap_content" android:src="@drawable/icon" /> <ImageView android:id="@+id/imageview" android:layout_margin="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" /> <com.example.customview05imageview.view.CustomImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" CustomImageView:src="@drawable/icon" CustomImageView:type="circle" /> <com.example.customview05imageview.view.CustomImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" CustomImageView:borderRadius="10dp" CustomImageView:src="@drawable/icon" CustomImageView:type="round" /> <com.example.customview05imageview.view.CustomImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" CustomImageView:borderRadius="20dp" CustomImageView:src="@drawable/icon" CustomImageView:type="round" /> </LinearLayout></ScrollView>
MainActivity
package com.example.customview05imageview;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.PorterDuff.Mode;import android.graphics.PorterDuffXfermode;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.widget.ImageView;public class MainActivity extends Activity{private ImageView imageview;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageview = (ImageView) findViewById(R.id.imageview);Drawable drawable = getResources().getDrawable(R.drawable.icon); BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; Bitmap bitmap = bitmapDrawable.getBitmap(); Bitmap roundCorner = toRoundCorner(bitmap, 30); imageview.setImageBitmap(roundCorner);}public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { //创建bitmap对象 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); //创建画笔、画布 Canvas canvas = new Canvas(output); final Paint paint = new Paint(); final int color = 0xff424242; //创建矩形-位置 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); //圆角 final float roundPx = pixels; paint.setAntiAlias(true); //透明 canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } }CustomImageView
package com.example.customview05imageview.view;import android.content.Context;import android.content.res.TypedArray;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;import android.graphics.PorterDuffXfermode;import android.graphics.RectF;import android.util.AttributeSet;import android.util.TypedValue;import android.view.View;import com.example.customview05imageview.R;public class CustomImageView extends View {private int type;private static final int TYPE_CIRCLE = 0;private static final int TYPE_ROUND = 1;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);}public CustomImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);TypedArray a = context.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:// 默认为Circle--0type = a.getInt(attr, 0);break;case R.styleable.CustomImageView_borderRadius:// 转变为标准尺寸的一个函数-- 默认为10DIPmRadius = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f,getResources().getDisplayMetrics()));break;}}a.recycle();}/** * 计算控件的高度和宽度 */@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {/** * 参数:它们指明控件可获得的空间以及关于这个空间描述的元数据 设置宽度 */int specMode = MeasureSpec.getMode(widthMeasureSpec);int specSize = MeasureSpec.getSize(widthMeasureSpec);/** * MeasureSpec.EXACTLY是精确尺寸,当我们将控件的layout_width或layout_height指定为具体数值时如andorid:layout_width="50dip" * 或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。 */if (specMode == MeasureSpec.EXACTLY)// match_parent{mWidth = specSize;} else {// 由图片决定的宽int desireByImg = getPaddingLeft() + getPaddingRight()+ mSrc.getWidth();if (specMode == MeasureSpec.AT_MOST)// wrap_content{/** * 此时控件尺寸只要不超过父控件允许的最大尺寸即可 */mWidth = Math.min(desireByImg, specSize);} elsemWidth = desireByImg;}/*** * 设置高度 */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);} elsemHeight = desire;}setMeasuredDimension(mWidth, mHeight);}/** * 绘制 */@Overrideprotected void onDraw(Canvas canvas) {switch (type) {case TYPE_CIRCLE:int min = Math.min(mWidth, mHeight);// Bitmap 创建 一个新的,缩放后的 BitmapmSrc = 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;}}/** * 根据原图和变长绘制圆形图片 */private Bitmap createCircleImage(Bitmap source, int min) {final Paint paint = new Paint();paint.setAntiAlias(true);//该函数创建一个带有特定宽度、高度和颜色格式的位图Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);//产生一个同样大小的画布Canvas canvas = new Canvas(target);//首先绘制圆形-原心xy坐标以及半径canvas.drawCircle(min / 2, min / 2, min / 2, paint);//取两层绘制交集。显示上层paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//绘制图片canvas.drawBitmap(source, 0, 0, paint);return target;}/** * 根据原图添加圆角 */private Bitmap createRoundConerImage(Bitmap source) {final Paint paint = new Paint();paint.setAntiAlias(true);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, mRadius, mRadius, paint);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));canvas.drawBitmap(source, 0, 0, paint);return target;}}RoundImageView
package com.example.customview05imageview; import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.RectF;import android.util.AttributeSet;import android.widget.ImageView; public class RoundImageView extends ImageView { public RoundImageView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public RoundImageView(Context context) { super(context); init(); } private void init() { maskPaint.setAntiAlias(true); maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // zonePaint.setAntiAlias(true); zonePaint.setColor(Color.RED); // float density = getResources().getDisplayMetrics().density; rect_adius = rect_adius * density; } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); int w = getWidth(); int h = getHeight(); roundRect.set(0, 0, w, h); } private final RectF roundRect = new RectF(); private float rect_adius = 20; private final Paint maskPaint = new Paint(); private final Paint zonePaint = new Paint(); @Override public void draw(Canvas canvas) { canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG); //产生一个红色的圆角矩形 canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint); // canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG); super.draw(canvas); canvas.restore(); } }
0 0
- 圆角ImageView
- 圆角imageview
- ImageView圆角
- ImageView圆角
- 圆角ImageView
- 圆角Imageview
- 圆角ImageView
- Android 圆角ImageView
- 圆形 圆角ImageView
- Android:圆角ImageView
- 自定义ImageView 圆角shape
- 圆角Imageview--RoundedImageView
- 处理imageView圆角
- android自定义 圆角Imageview
- Android 圆角ImageView
- 自定义View圆角ImageView
- 自定义View圆角ImageView
- ImageView
- C ++ 11 New Feature Notes
- 人生第一面——腾讯
- CMD 常用命令
- 9、图
- [LeetCode] Pow(x, n)
- ImageView圆角
- USACO3.3--Shopping Offers
- 测试文章
- Java学习日记之类和对象继续深入:继承
- 对象归档
- 苹果最重要的男人,如何实现职业的三次跳跃?
- eclipse我用到的技巧收集
- Qt学习记录3——登陆窗口的实现
- 堆栈的顺序存储结构实现2——栈满 栈空 入栈 出栈