package com.example.drawing; import java.io.File; import java.io.FileOutputStream; import java.util.Random; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Bitmap.CompressFormat; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import android.view.View.OnTouchListener; 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); /** * 初始化 */ iv = (ImageView) findViewById(R.id.iv); // 畫筆 paint = new Paint(); paint.setStrokeWidth(5); final int R=0; final int G=0; final int B=0; int color=Color.rgb(R, G,B); paint.setColor(color); // 畫布 canvas = new Canvas(); canvas.drawColor(Color.WHITE); // 得知手指移動軌跡 iv.setOnTouchListener(new OnTouchListener() { // 手指開始的座標 float startX =0; float startY=0; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { // 首次接觸屏幕 case MotionEvent.ACTION_DOWN: System.out.println("手指按下……"); if (basebitmap != null) { basebitmap = Bitmap.createBitmap(basebitmap); } else { basebitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.ARGB_8888); } canvas.setBitmap(basebitmap); startX = (float) event.getX(); startY = (float) event.getY(); canvas.drawPoint(startX, startY, paint); break; // 手指移動 case MotionEvent.ACTION_MOVE: System.out.println("手指移動……"); //動態更改顏色 Random random=new Random(); int color=Color.rgb(R+random.nextInt(256), G+random.nextInt(256), B+random.nextInt(256)); paint.setColor(color); float newX = (float) event.getX(); float newY = (float) event.getY(); canvas.drawLine(startX, startY, newX, newY, paint); // 更新畫筆開始位置 startX = (float) event.getX(); startY = (float) event.getY(); break; // 手指擡起 case MotionEvent.ACTION_UP: System.out.println("手指擡起……"); break; default: break; } iv.setImageBitmap(basebitmap); // 使得事件不停被執行 須要返回true return true; } }); } public void save(View v) { try { File file=new File(Environment.getExternalStorageDirectory()+"/DCIM/",System.currentTimeMillis()+".jpg"); FileOutputStream fos=new FileOutputStream(file); basebitmap.compress(CompressFormat.JPEG, 100, fos); fos.close(); Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); //通知系統SD卡從新掛載 Intent intent=new Intent(); intent.setAction(Intent.ACTION_MEDIA_MOUNTED); intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory())); sendBroadcast(intent); } catch (Exception e) { Toast.makeText(this, "失敗", Toast.LENGTH_SHORT).show(); } } public void clear(View v) { basebitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.ARGB_8888); iv.setImageBitmap(basebitmap); } }