欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android Bitmap圆角与倒影的具体实现代码

程序员文章站 2023-12-01 20:24:22
[html]复制代码 代码如下:/**      * 画一个圆角图      * ...

[html]

复制代码 代码如下:

/**
     * 画一个圆角图
     * 
     * @param bitmap
     * @param roundpx
     * @return
     */
    public static bitmap getroundedcornerbitmap(bitmap bitmap, float roundpx) {
        bitmap output = bitmap.createbitmap(bitmap.getwidth(),
                bitmap.getheight(), config.argb_8888);
        canvas canvas = new canvas(output);
        final int color = 0xff424242;
        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, rect, paint);
        return output;
    }

    /**
     * 创建倒影效果
     * 
     * @return
     */
    public boolean createreflectedimages() {
        // 倒影图和原图之间的距离
        final int reflectiongap = 4;
        int index = 0;
        for (gallerywith3ddata imageid : mimageids) {
            // 返回原图解码之后的bitmap对象
            bitmap originalimage = bitmapfactory.decoderesource(
                    mcontext.getresources(), imageid.getinteger());
            int width = originalimage.getwidth();
            int height = originalimage.getheight();
            // 创建矩阵对象
            matrix matrix = new matrix();
            // 指定矩阵(x轴不变,y轴相反)
            matrix.prescale(1, -1);
            // 将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图
            bitmap reflectionimage = bitmap.createbitmap(originalimage, 0,
                    height / 2, width, height / 2, matrix, false);
            // 创建一个宽度不变,高度为原图+倒影图高度的位图
            bitmap bitmapwithreflection = bitmap.createbitmap(width,
                    (height + height / 2), config.argb_8888);
            // 将上面创建的位图初始化到画布
            canvas canvas = new canvas(bitmapwithreflection);
            canvas.drawbitmap(getroundedcornerbitmap(originalimage, 20), 0, 0,
                    null);
            int len = imageid.getstr().length();
            double lenweght = len * 50 * 0.9;
            int ban = width / 2;
            int ban1 = (int) (lenweght / 2);
            int hua = ban - ban1;
            if (imageid.getflagrecommend()) {
                canvas.rotate(30);
                canvas.drawtext(mstrrecommend, hua - 20, 150,
                        createpaint(color.red));
                canvas.rotate(-30);
            }
            paint deafaultpaint = new paint();
            deafaultpaint.setantialias(false);
            canvas.drawbitmap(getroundedcornerbitmap(reflectionimage, 20), 0,
                    height + reflectiongap, null);
            paint paint = new paint();
            paint.setantialias(false);
            /**
             * 参数一:为渐变起初点坐标x位置, 参数二:为y轴位置, 参数三和四:分辨对应渐变终点, 最后参数为平铺方式,
             * 这里设置为镜像gradient是基于shader类,所以我们通过paint的setshader方法来设置这个渐变
             */
            lineargradient shader = new lineargradient(0,
                    originalimage.getheight(), 0,
                    bitmapwithreflection.getheight() + reflectiongap,
                    0x70ffffff, 0x00ffffff, tilemode.mirror);
            // 设置阴影
            paint.setshader(shader);
            paint.setxfermode(new porterduffxfermode(
                    android.graphics.porterduff.mode.dst_in));
            // 用已经定义好的画笔构建一个矩形阴影渐变效果
            canvas.drawrect(0, height, width, bitmapwithreflection.getheight() + reflectiongap, paint);
            canvas.drawtext(imageid.getstr(), hua, 430,
                    createpaint(color.white));
            // 创建一个imageview用来显示已经画好的bitmapwithreflection
            imageview imageview = new imageview(mcontext);
            imageview.setimagebitmap(bitmapwithreflection);
            // 设置imageview大小 ,也就是最终显示的图片大小
            imageview.setlayoutparams(new gallerywith3d.layoutparams(150, 250));
            // imageview.setscaletype(scaletype.matrix);
            mimages[index++] = imageview;
        }
        return true;
    }

下面是效果图:android Bitmap圆角与倒影的具体实现代码