Android开发实现图片平移、缩放、倒影及旋转功能的方法
程序员文章站
2023-12-09 13:32:51
本文实例讲述了android开发实现图片平移、缩放、倒影及旋转功能的方法。分享给大家供大家参考,具体如下:
解析:
1)根据原来的图片创建新的图片
bitma...
本文实例讲述了android开发实现图片平移、缩放、倒影及旋转功能的方法。分享给大家供大家参考,具体如下:
解析:
1)根据原来的图片创建新的图片
bitmap modbm = bitmap.createbitmap(bm.getwidth()+20, bm.getheight()+20, bm.getconfig());
2)设置到画布
canvas canvas = new canvas(modbm);
3)使用矩阵进行平移…
matrix matrix = new matrix(); matrix.setrotate(90, bm.getwidth()/2, bm.getheight()/2);
4)把改变后图片设置到
canvas.drawbitmap(bm, matrix, paint); imageview2.setimagebitmap(modbm);
示例代码:
1、平移:
public class translateimageactivity extends activity { private imageview imageview1; private imageview imageview2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_image_rotate); initviews(); } private void initviews() { imageview1 = (imageview) findviewbyid(r.id.imageview1); imageview2 = (imageview) findviewbyid(r.id.imageview2); bitmap bm = bitmapfactory.decoderesource(getresources(), r.drawable.mm0); imageview1.setimagebitmap(bm); bitmap modbm = bitmap.createbitmap(bm.getwidth()+20, bm.getheight()+20, bm.getconfig()); canvas canvas = new canvas(modbm); paint paint = new paint(); paint.setcolor(color.black); paint.setantialias(true); matrix matrix = new matrix(); //matrix.setrotate(90, bm.getwidth()/2, bm.getheight()/2); matrix.settranslate(20, 20); matrix.posttranslate(20, 20); canvas.drawbitmap(bm, matrix, paint); imageview2.setimagebitmap(modbm); } }
2、缩放
public class scaleimageactivity extends baseactivity { private static final int double = 2; private imageview iv1, iv2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_scale_iamge); iv1 = (imageview) findviewbyid(r.id.imageview1); iv2 = (imageview) findviewbyid(r.id.imageview2); bitmap b1 = bitmapfactory.decoderesource(getresources(), r.drawable.p1); iv1.setimagebitmap(b1); bitmap b2 = bitmap.createbitmap(b1.getwidth()*double, b1.getheight(), config.argb_8888); canvas canvas = new canvas(b2); matrix matrix = new matrix(); matrix.setvalues(new float[]{ double * 1.0f, 0, 0,//x 0, 1.0f, 0,//y 0, 0, 1.0f//z }); canvas.drawbitmap(b1, matrix, new paint()); iv2.setimagebitmap(b2); } }
3、倒影
public class shadeimageactivity extends activity { private imageview imageview1; private imageview imageview2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_image_rotate); initviews(); } private void initviews() { imageview1 = (imageview) findviewbyid(r.id.imageview1); imageview2 = (imageview) findviewbyid(r.id.imageview2); bitmap bm = bitmapfactory.decoderesource(getresources(), r.drawable.mm3); imageview1.setimagebitmap(bm); bitmap modbm = bitmap.createbitmap(bm.getwidth(), bm.getheight(), bm.getconfig()); canvas canvas = new canvas(modbm); paint paint = new paint(); paint.setcolor(color.black); paint.setantialias(true); matrix matrix = new matrix(); //matrix.setrotate(90, bm.getwidth()/2, bm.getheight()/2); //matrix.settranslate(20, 20); //镜子效果 matrix.setscale(1, -1); matrix.posttranslate(0, bm.getheight()); canvas.drawbitmap(bm, matrix, paint); imageview2.setimagebitmap(modbm); } }
4、旋转
public class rotateimageactivity extends activity { private imageview imageview1; private imageview imageview2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_image_rotate); initviews(); } private void initviews() { imageview1 = (imageview) findviewbyid(r.id.imageview1); imageview2 = (imageview) findviewbyid(r.id.imageview2); bitmap bm = bitmapfactory.decoderesource(getresources(), r.drawable.mm0); imageview1.setimagebitmap(bm); bitmap modbm = bitmap.createbitmap(bm.getwidth(), bm.getheight(), bm.getconfig()); canvas canvas = new canvas(modbm); paint paint = new paint(); paint.setcolor(color.black); paint.setantialias(true); matrix matrix = new matrix(); matrix.setrotate(90, bm.getwidth()/2, bm.getheight()/2); canvas.drawbitmap(bm, matrix, paint); imageview2.setimagebitmap(modbm); } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。