返回顶部
首页 > 资讯 > 移动开发 >android Bitmap圆角与倒影的具体实现代码
  • 582
分享到

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

bitmapAndroid 2022-06-06 10:06:56 582人浏览 泡泡鱼
摘要

[html] 代码如下:     public static Bitmap getRoundedCornerBitmap(Bitmap bitma

[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,
           & nbsp;        (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;
    }

下面是效果图:

您可能感兴趣的文章:android显示TextView文字的倒影效果实现代码Android实现动态向Gallery中添加图片及倒影与3D效果示例Android中使用Matrix控制图形变换和制作倒影效果的方法Android编程滑动效果之倒影效果实现方法(附demo源码下载)Android应用开发之简易、大气音乐播放器实现专辑倒影效果Android 轻松实现图片倒影效果实例代码Android 图像处理(类型转换,比例缩放,倒影,圆角)的小例子Android 倒影算法的实现代码Android自定义TextView实现文字倾斜效果Android实现文字翻转动画的效果Android实现文字和图片混排(文字环绕图片)效果Android编程实现文字倒影效果的方法


--结束END--

本文标题: android Bitmap圆角与倒影的具体实现代码

本文链接: https://lsjlt.com/news/27561.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作