图片加水印

来源:互联网 发布:qq邮箱的imap端口 编辑:程序博客网 时间:2024/06/05 06:03

//image  需要显示的view

//photo  需要加水印的图片

//writeText   水印的文字

private void addWater(ImageView image,Bitmap photo,String writeText){

 
//        ImageView image = (ImageView) this.findViewById(ImageView);
//        Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
        int width = photo.getWidth(), hight = photo.getHeight();
        System.out.println("宽"+width+"高"+hight);
        Bitmap icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); //建立一个空的BItMap
        Canvas canvas = new Canvas(icon);//初始化画布绘制的图像到icon上


        Paint photoPaint = new Paint(); //建立画笔
        photoPaint.setDither(true); //获取跟清晰的图像采样
        photoPaint.setFilterBitmap(true);//过滤一些


        Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());//创建一个指定的新矩形的坐标
        Rect dst = new Rect(0, 0, width, hight);//创建一个指定的新矩形的坐标
        canvas.drawBitmap(photo, src, dst, photoPaint);//将photo 缩放或则扩大到 dst使用的填充区photoPaint


        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//设置画笔
        textPaint.setTextSize(8.0f);//字体大小
        textPaint.setTypeface(Typeface.DEFAULT);//采用默认的宽度
        textPaint.setColor(Color.RED);//采用的颜色
        //textPaint.setShadowLayer(3f, 1, 1,this.getResources().getColor(android.R.color.background_dark));//影音的设置
        canvas.drawText(writeText, 3, photo.getHeight()-20, textPaint);//绘制上去字,开始未知x,y采用那只笔绘制
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        image.setImageBitmap(icon);
    }
0 0
原创粉丝点击