栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

[html]
复制代码 代码如下:

    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;
    }

   
    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);
           
            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;
    }

下面是效果图:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/165119.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号