Android--简单的画画板实例代码

<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>



转载于:https://www.cnblogs.com/chaoyu/p/6436866.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值