activity_main.xml中的配置 MainActivity中代碼: ...
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); } }