activity_main.xml中的配置
class="brush:html;gutter:true;"><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); } }