天天看點

Android--簡單的畫畫闆執行個體代碼

版權聲明:本文為部落客原創文章,轉載請标明出處。 https://blog.csdn.net/chaoyu168/article/details/51149273

<span style="font-size:14px;">public class MainActivity extends Activity {
 private ImageView iv;
 private Bitmap baseBitmap;
 private Canvas canvas;
 private Paint paint;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  this.iv = (ImageView) this.findViewById(R.id.iv);
  // 建立一張空白圖檔
  baseBitmap = Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888);
  // 建立一張畫布
  canvas = new Canvas(baseBitmap);
  // 畫布背景為灰色
  canvas.drawColor(Color.GRAY);
  // 建立畫筆
  paint = new Paint();
  // 畫筆顔色為紅色
  paint.setColor(Color.RED);
  // 寬度5個像素
  paint.setStrokeWidth(5);
  // 先将灰色背景畫上
  canvas.drawBitmap(baseBitmap, new Matrix(), paint);
  iv.setImageBitmap(baseBitmap);

  iv.setOnTouchListener(new OnTouchListener() {
   int startX;
   int startY;
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
     // 擷取手按下時的坐标
     startX = (int) event.getX();
     startY = (int) event.getY();
     break;
    case MotionEvent.ACTION_MOVE:
     // 擷取手移動後的坐标
     int stopX = (int) event.getX();
     int stopY = (int) event.getY();
     // 在開始和結束坐标間畫一條線
     canvas.drawLine(startX, startY, stopX, stopY, paint);
     // 實時更新開始坐标
     startX = (int) event.getX();
     startY = (int) event.getY();
     iv.setImageBitmap(baseBitmap);
     break;
    }
    return true;
   }
  });
 }
 public void save(View view) {
  try {
   File file = new File(Environment.getExternalStorageDirectory(),
     System.currentTimeMillis() + ".jpg");
   OutputStream stream = new FileOutputStream(file);
   baseBitmap.compress(CompressFormat.JPEG, 100, stream);
   stream.close();
   // 模拟一個廣播,通知系統sdcard被挂載
   Intent intent = new Intent();
   intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
   intent.setData(Uri.fromFile(Environment
     .getExternalStorageDirectory()));
   sendBroadcast(intent);
   Toast.makeText(this, "儲存圖檔成功", 0).show();
  } catch (Exception e) {
   Toast.makeText(this, "儲存圖檔失敗", 0).show();
   e.printStackTrace();
  }
 }
}
</span>           

簡單的布局檔案:

<span style="font-size:14px;"><RelativeLayout 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"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:onClick="save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="儲存圖檔" />
    <ImageView
        android:layout_above="@id/button1"
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout></span>
           

再來個權限:

<span style="font-size:14px;"><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/></span>
           

繼續閱讀