zoukankan      html  css  js  c++  java
  • 通过学习制作长微博工具来了解水印的制作,及EditText中的内容在图片中换行显示

    长微博工具非常有用,140字的要求可能阻止你写更多的内容,于是长微博工具应运而生,虽然网上有很多长微博工具,但是我都不是很满意,所以自己想做一个,通过做这个长微博工具,我学习到了很多东西,有两个难点,一个是怎么制作水印,另一个是水印制作成功了,怎么让水印文字多行显示!废话不多说了,先上效果图吧!


    生成之后的图片如下:

    在这里我就不多解释了,也不多说了,代码中解释非常详细,已经写好了,相信大家肯定能看明白。关键代码如下:

    /**
    	 * 将文字添加到图片上
    	 */
    	private void makeTxtToImage() {
    		String content = weiboContentTv.getText().toString();
    		// 获取到背景图片
    		Bitmap photo = BitmapFactory.decodeResource(this.getResources(),
    				R.drawable.weibo_bg);
    
    		// 获得layoutImage宽度,这里其实就是屏幕的宽度
    		int totalWidth = layoutImage.getWidth();
    		// 获得背景图片的高度
    		int totalHeight = photo.getHeight();
    		// 得到文本框的宽度
    		int textWidth = weiboContentTv.getWidth();
    
    		// 计算文字的起点
    		int xWidth = (totalWidth - textWidth) / 2;
    		// 建立一个空的Bitmap
    		Bitmap icon = Bitmap.createBitmap(totalWidth, totalHeight,
    				Bitmap.Config.ARGB_8888);
    		// 初始化画布绘制的图像到icon上
    		Canvas canvas = new Canvas(icon);
    
    		Paint photoPaint = new Paint(); // 建立画笔
    		// Dither(图像的抖动处理,当每个颜色值以低于8位表示时,对应图像做抖动处理可以实现在可显示颜色总数比较低(比如256色)时还保持较好的显示效果
    		photoPaint.setDither(true); // 获取更清晰的图像采样
    		// 过滤
    		photoPaint.setFilterBitmap(true);// setDither()和setFilterBitmap()的具体含义不是很清楚,但是只要记住:设置上这两个方法,就可以是图像更清晰就行!
    		// 创建一个指定的新矩形的坐标
    		Rect src = new Rect(0, 0, totalWidth, totalHeight);
    		// 创建一个指定的新矩形的坐标
    		Rect dst = new Rect(0, 0, totalWidth, totalHeight);
    		// 将photo缩放或扩大到dst使用的填充区photoPaint
    		canvas.drawBitmap(photo, src, dst, photoPaint);
    		// 设置文字画笔
    		TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG
    				| Paint.DEV_KERN_TEXT_FLAG);
    		// 字体大小
    		textPaint.setTextSize(22.0f);
    		// 采用默认的宽度
    		textPaint.setTypeface(Typeface.DEFAULT);
    		// 文字画笔采用的颜色
    		textPaint.setColor(Color.BLACK);
    		// 设置阴影,这里不用阴影
    		// textPaint.setShadowLayer(3f, 1, 1,
    		// this.getResources().getColor(android.R.color.background_dark));//
    
    		/**
    		 * StaticLayout中参数的解释:
    		 * 
    		 * 1.字符串子资源
    		 * 
    		 * 2 .画笔对象
    		 * 
    		 * 3.layout的宽度,字符串超出宽度时自动换行。
    		 * 
    		 * 4.layout的样式,有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE 三种。
    		 * 
    		 * 5.相对行间距,相对字体大小,1.5f表示行间距为1.5倍的字体高度。
    		 * 
    		 * 6.相对行间距,0表示0个像素。
    		 * 
    		 * 实际行间距等于这两者的和。
    		 * 
    		 * 7.还不知道是什么意思,参数名是boolean includepad。
    		 */
    		StaticLayout layout = new StaticLayout(content, textPaint, textWidth,
    				Alignment.ALIGN_NORMAL, 1.2F, 0.0F, true);// 这个StaticLayout是让文字在图片中多行显示的关键,android之所以强大就是它已经帮你封装好了,通过对StaticLayout的设置就可以让EditText中的文字多行显示
    		canvas.translate(xWidth, 0);
    		layout.draw(canvas);
    		saveMyBitmap(icon);
    	}
    
    	/**
    	 * 保存图片至SD卡中
    	 * 
    	 * @param bitmap
    	 */
    	public void saveMyBitmap(Bitmap bitmap) {
    		FileOutputStream fos = null;
    		try {
    			File file = new File(Environment.getExternalStorageDirectory()
    					.getAbsoluteFile() + File.separator + "longweibo");
    			if (!file.exists()) {
    				file.mkdirs();
    			}
    			fos = new FileOutputStream(new File(file, "longweibo"
    					+ System.currentTimeMillis() + ".jpg"));
    			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    			Toast.makeText(MainActivity.this, "长微博生成失败!", Toast.LENGTH_SHORT)
    					.show();
    		} finally {
    			if (fos != null) {
    				try {
    					fos.flush();
    					fos.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    			Toast.makeText(MainActivity.this, "长微博生成成功!", Toast.LENGTH_SHORT)
    					.show();
    		}
    
    	}
    转载请注明出处:http://blog.csdn.net/loongggdroid/article/details/17591977

    非著名程序员可能是东半球最好的技术分享公众号。每天,每周定时推送一些有关移动开发的原创文章和教程,微信号:smart_android。
  • 相关阅读:
    【L.M.W.Y.D】Scrum Meeting 5
    【L.M.W.Y.D】Scrum Meeting 4
    多喝热水 实验十 团队作业6:团队项目用户验收&Beta冲刺
    多喝热水【Beta】Scrum meeting 4
    多喝热水【Beta】Scrum meeting 3
    多喝热水【Beta】Scrum meeting 2
    多喝热水【Beta】Scrum meeting 1
    多喝热水 实验九 团队作业5:团队项目编码与Alpha冲刺
    多喝热水 [Alpha] Scrum Meeting 7
    多喝热水 [Alpha] Scrum Meeting 6
  • 原文地址:https://www.cnblogs.com/loonggg/p/4981827.html
Copyright © 2011-2022 走看看