上一篇已经介绍了Android种Bitmap和Canvas的使用,下面我们来写一个具体实例
http://blog.csdn.net/zhaoyazhi2129/article/details/32136179
运行效果:
主要代码
package com.example.guaguale; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.os.Bundle; import android.util.Log; import android.view.View; public class CanvaDemoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new CustomView(this)); } /** * 使用内部类 自定义一个简单的View * * @author zhaoyazhi * * 2014-6-18 */ class CustomView extends View { public CustomView(Context context) { super(context); } // 在这里我们将测试canvas提供的绘制图形方法 @Override protected void onDraw(Canvas canvas) { drawBitmap(canvas); drawBitmapPostScale(canvas); drapBitmapPostRotate(canvas); shuiyinBitmap(canvas); } /** * 在canvas放置图片 * * @param canvas */ private void drawBitmap(Canvas canvas) { // 获取图片资源 Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.e); // 将图片添加到canvas canvas.drawBitmap(bmp, 0, 0, null); } /** * 缩放图片 * * @param canvas */ private void drawBitmapPostScale(Canvas canvas) { // 获取图片资源 Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.e); // Matrix类进行图片处理(缩小或者旋转) Matrix matrix = new Matrix(); // 缩小一倍 matrix.postScale(0.5f, 0.5f); // 生成新的图片 Bitmap dstbmp = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(), bmp1.getHeight(), matrix, true); // 添加到canvas canvas.drawBitmap(dstbmp, 200, 0, null); } /** * 旋转图片 * * @param canvas */ private void drapBitmapPostRotate(Canvas canvas) { // 获取图片资源 Bitmap bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.e); // Matrix类进行图片处理(缩小或者旋转) Matrix matrix1 = new Matrix(); // 缩小 matrix1.postScale(0.8f, 0.8f); // 旋转 matrix1.postRotate(45); // 生成新的图片 Bitmap dstbmp1 = Bitmap.createBitmap(bmp2, 0, 0, bmp2.getWidth(), bmp2.getHeight(), matrix1, true); // 添加到canvas canvas.drawBitmap(dstbmp1, 130, 100, null); } /** * 加水印图片 * * @param canvas */ private void shuiyinBitmap(Canvas canvas) { Bitmap bmp = createBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.e), BitmapFactory.decodeResource(getResources(), R.drawable.c)); canvas.drawBitmap(bmp, 10, 200, null); } // 图片添加水印处理 private Bitmap createBitmap(Bitmap src, Bitmap watermark) { String tag = "createBitmap"; Log.d(tag, "create a new bitmap"); if (src == null) { return null; } int w = src.getWidth(); int h = src.getHeight(); int ww = watermark.getWidth(); int wh = watermark.getHeight(); // create the new blank bitmap Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图 Canvas cv = new Canvas(newb); // draw src into cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src // draw watermark into cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印 // save all clip cv.save(Canvas.ALL_SAVE_FLAG);// 保存 // store cv.restore();// 存储 return newb; } } }
代码中都有具体实例,大家自己看哦,有什么不清楚的能解答都帮大家解答
赵雅智_运用Bitmap和Canvas实现图片显示,缩小,旋转,水印,布布扣,bubuko.com
时间: 2024-10-14 01:47:39