Android中复制图片的实例代码
程序员文章站
2023-12-04 10:20:28
activity_main.xml中的配置
activity_main.xml中的配置
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".mainactivity" > <imageview android:id="@+id/iv_one" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <imageview android:id="@+id/iv_two" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout>
mainactivity中代码:
public class mainactivity extends activity { private imageview ivone; private imageview ivtwo; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //1.获取图片控件 ivone = (imageview) findviewbyid(r.id.iv_one); ivtwo = (imageview) findviewbyid(r.id.iv_two); //2.把tomcat.png 转换成bitmap 然后显示到iv_src bitmap srcbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.tomcat); //3.将原图放置在第一个控件中 ivone.setimagebitmap(srcbitmap); //4.创建原图模板 bitmap copybitmap = bitmap.createbitmap(srcbitmap.getwidth(), srcbitmap.getheight(), srcbitmap.getconfig()); //5.想作画 需要一个画布 以copybitmap为模板 canvas canvas = new canvas(copybitmap); //6.创建一个画笔 paint paint = new paint(); //7.开始作画 srcbitmap参考原图去画 canvas.drawbitmap(srcbitmap, new matrix(), paint); for (int i = 0; i < 10; i++) { //[一次修改多个像素] copybitmap.setpixel(20+i,30, color.red); } //8.把copybitmap显示到ivtwo上 ivtwo.setimagebitmap(copybitmap); } }
总结
以上所述是小编给大家介绍的android中复制图片的实例代码,希望对大家有所帮助