获得圆角图片和带倒影的图片

来源:互联网 发布:电子地图数据下载 编辑:程序博客网 时间:2024/04/29 16:40
<span style="font-family:Times New Roman;font-size:18px;"></span><pre name="code" class="java">

<span style="font-family:Times New Roman;font-size:18px;">//圆角图片round_image=(ImageView)findViewById(R.id.reflection_image);Bitmap round_image_bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.top_img);round_image_bmp = getRoundedCornerBitmap(round_image_bmp,100);        round_image .setImageBitmap(round_image_bmp);</span>
<span style="font-family:Times New Roman;font-size:18px;"></span>
<span style="font-family:Times New Roman;font-size:18px;"></span><pre name="code" class="java"><span style="font-size:18px;">// 获得圆角图片的方法public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {//根据参数创建新位图 Bitmap.Config ARGB_4444 16 每个像素 占四位   Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_4444);// Paint 就是画笔   Bitmap    就是画布  Canvas   就是画家Canvas canvas = new Canvas(output);final int color = Color.RED;final Paint paint = new Paint();final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());final RectF rectF = new RectF(rect);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, rectF, paint);return output;}</span>


<span style="font-family:Times New Roman;font-size:18px;"></span><pre name="code" class="java"><img src="http://img.blog.csdn.net/20150523175218476?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHViZWlxaWFubmlhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="270" height="480" alt="" />
//带阴影图片reflection_image=(ImageView)findViewById(R.id.reflection_image);Bitmap reflection_image_bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.top_img);reflection_image_bmp = createReflectionImageWithOrigin(reflection_image_bmp);reflection_image .setImageBitmap(reflection_image_bmp);

//获得带倒影的图片方法    public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){        final int reflectionGap = 4;        int width = bitmap.getWidth();        int height = bitmap.getHeight();                    Matrix matrix = new Matrix();        matrix.preScale(1, -1);                    Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height/4, width, height/4, matrix, false);                    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/4), Config.ARGB_4444);                    Canvas canvas = new Canvas(bitmapWithReflection);        canvas.drawBitmap(bitmap, 0, 0, null);        Paint deafalutPaint = new Paint();        canvas.drawRect(0, height,width,height + reflectionGap,        deafalutPaint);               canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);                     Paint paint = new Paint();        LinearGradient shader = new LinearGradient(0,        bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);        paint.setShader(shader);        // Set the Transfer mode to be porter duff and destination in        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));        // Draw a rectangle using the paint with our linear gradient        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);             return bitmapWithReflection;    }  


0 0
原创粉丝点击