package com.lib.andlib_libtemp.example.dir.exam1; import android.graphics.Bitmap;import android.graphics.Matrix;import android.view.View; import com.lib.andlib_libtemp.utils.LogUtil; /** * Created by envy15 on 2016/6/16. * 根据View的比例剪切图片大小工具类 * 剪切模式:中间、上、下 */public class BitmapCatUtil { public static Bitmap imageCatByCenter(View mView, Bitmap source) { //先获取View的高度,获取失败将返回原图 getImageViewPixSize(mView); /** * 获取图片Bitmap的宽高 */ int suWidth = source.getWidth(); int suHeight = source.getHeight(); if(ivWidth == 0 || ivHeight == 0) { LogUtil.infoE("获取View宽高失败:ivHeight=" + ivHeight +" ivWidth=" + ivWidth); return source; } /** * Y方向上应该截取的长度 */ int yAxisAdd = ivHeight * suWidth / ivWidth; if (yAxisAdd == suHeight) { //!!! //裁剪的宽高与图片的宽高一样时,bitmap的返回的引用是source,因此不能回收,直接返回 //回收抛异常:java.lang.IllegalStateException: Transformation SizeTransformation returned input Bitmap but recycled it return source; } else { //按比例截取多余的高度 if (yAxisAdd < suHeight) //截取的高度需小于源图片的高度 { //(起点左边,截取宽度,截取高度) Bitmap bitmap = Bitmap.createBitmap(source, 0, suHeight / 2 - yAxisAdd / 2, suWidth,yAxisAdd); source.recycle(); return bitmap; } else { //否则需按比例截取宽度 int xAxisAdd = ivWidth * suHeight / ivHeight; Bitmap bitmap = Bitmap.createBitmap(source, suWidth / 2 - xAxisAdd / 2, 0, xAxisAdd, suHeight); source.recycle(); return bitmap; } } } /** * 返回图片控件表示的宽高像素大小 * 必须指定图片的大小,wrap_content/match_parent均无法获取宽高 * 参考 * http://blog.csdn.net/crazy1235/article/details/41806079 */ private static int ivHeight; private static int ivWidth; private static void getImageViewPixSize(View imageView) { //获取宽高OK ivHeight = imageView.getLayoutParams().height; ivWidth = imageView.getLayoutParams().width; } /** * 获取Bitmap缩放比例 */ public static float catScale(View mView, Bitmap bitmap) { return mView.getLayoutParams().height*1.0f/bitmap.getHeight(); } /** * Bitmap放大/缩小的方法 * @param bitmap * @return bitmap */ public static Bitmap bitmapScaling(Bitmap bitmap,float scale) { Matrix matrix = new Matrix(); matrix.postScale(scale,scale); //长和宽放大缩小的比例 Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true); return resizeBmp; }} +++++++++++++++++++++++++++++++
package com.lib.andlib_libtemp.example.dir.exam1; import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.view.View; /** * Created by envy15 on 2016/6/16. * 位图的绘制工具类:圆角 * */public class BitmapShapeUtil { /**四个圆角的圆角图片 * 四个圆角,roundRadius = bitmap.getWidth()/2 绘制为圆形图像 * @param bitmap 位图 * @param roundRadius 圆角大小 * @return 圆角图片 */ public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundRadius) { // 创建一个指定宽度和高度的空位图对象 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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 = roundRadius; // 消除锯齿 paint.setAntiAlias(true); // 画布背景色 canvas.drawARGB(0, 0, 0, 0); // 设置画笔颜色 paint.setColor(color); // 绘制圆角矩形 canvas.drawRoundRect(rectF, roundPx, roundPx, paint); // 未知 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // 把该图片绘制在该圆角矩形区域中 canvas.drawBitmap(bitmap, rect, rect, paint); // 最终在画布上呈现的就是该圆角矩形图片,然后我们返回该Bitmap对象 return output; } /**可实现动态绘制圆角 * 生成四个圆角的背景 * @param mView 根据View计算Drawable大小 * @param roundRadius 绘制半径 * @return Drawable * exam imageView.setBackground(BitmapShapeUtil.getRoundedCornerDrawable(imageView,imageView. * getLayoutParams().width/5, Color.GRAY)); */ public static Drawable getRoundedCornerDrawable(View mView, float roundRadius,int color) { int hight = mView.getLayoutParams().height; int width = mView.getLayoutParams().width; Bitmap rounder = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(rounder); Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG); xferPaint.setColor(color); canvas.drawRoundRect(new RectF(0, 0, width, hight), roundRadius, roundRadius, xferPaint); xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); canvas.drawBitmap(rounder, 0, 0, null); canvas.drawBitmap(rounder, 0, 0, xferPaint); Drawable drawable = new BitmapDrawable(rounder); return drawable; }}
+++++++++++++++++++++++++++++++
@Override public void initDatas() { LogUtil.infoD(this, "initDatas"); //OK Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.image_girl_test1); LogUtil.infoE(this,bitmap.getHeight()+" "+bitmap.getWidth()); //裁剪图片 bitmap = BitmapCatUtil.imageCatByCenter(imageView,bitmap); LogUtil.infoE(this,bitmap.getHeight()+" "+bitmap.getWidth()); LogUtil.infoE(this,imageView.getLayoutParams().height+" "+imageView.getLayoutParams().width); //缩放处理 bitmap = BitmapCatUtil.bitmapScaling(bitmap,BitmapCatUtil.catScale(imageView,bitmap)); //圆角处理 Bitmap roundedCornerBitmap = BitmapShapeUtil.getRoundedCornerBitmap( bitmap,bitmap.getWidth()/2); imageView.setImageBitmap(roundedCornerBitmap); // imageView.setBackground(BitmapShapeUtil.getRoundedCornerDrawable(imageView,imageView.// getLayoutParams().width/5, Color.GRAY)); }
时间: 2024-10-29 12:49:35