原理:
1)首先创建一个创建一张空白图片
2)把空白图片设置到imageview控件
3)给imageview设置监听,并返回值true;
代码如下:
/**
* @描述 画板
* @项目名称 App_Basic
* @包名 com.example.basic.image
* @类名 CanvaImage
* @author chenlin
* @version 1.0
*/
public class CanvaImageActivty extends BaseActivity implements OnTouchListener, OnClickListener {
private Button mBtnView;
private ImageView mImageView;
private Bitmap mBlankBitmap;
private Canvas mCanvas;
private Paint mPaint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_canvas);
mBtnView = (Button) findViewById(R.id.btn_view_image);
mImageView = (ImageView) findViewById(R.id.iv_canvas);
mImageView.setOnTouchListener(this);
mBtnView.setOnClickListener(this);
createBitmap();
}
private void createBitmap() {
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(outMetrics);
mBtnView.measure(0, 0);
int height = outMetrics.heightPixels - mBtnView.getMeasuredHeight();
// 创建一张空白图片
mBlankBitmap = Bitmap.createBitmap(outMetrics.widthPixels, height, Config.ARGB_8888);
// 创建一张画布
mCanvas = new Canvas(mBlankBitmap);
//画布背景为白色
mCanvas.drawColor(Color.WHITE);
//初始化画笔
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(3);
//绘制白色背景
mCanvas.drawBitmap(mBlankBitmap, new Matrix(), mPaint);
mImageView.setImageBitmap(mBlankBitmap);
}
private int downX;
private int downY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 获取手按下时的坐标
downX = (int) event.getX();
downY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
// 获取手移动后的坐标
int moveX = (int) event.getX();
int moveY = (int) event.getY();
// 在开始和结束坐标间画一条线
mCanvas.drawLine(downX, downY, moveX, moveY, mPaint);
// 实时更新开始坐标
downX = (int) event.getX();
downY = (int) event.getY();
mImageView.setImageBitmap(mBlankBitmap);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
//mImageView.setImageBitmap(mBlankBitmap);
//保存图片到sdcard
saveBitmap(mBlankBitmap);
break;
}
return true;
}
private void saveBitmap(Bitmap bitmap) {
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/image/";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
String filename = System.currentTimeMillis() + ".jpg";
FileOutputStream fos = new FileOutputStream(new File(path, filename));
//把保存图片
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.close();
notifyHasMount();
} catch (FileNotFoundException e) {
e.printStackTrace();
show("保存失败" + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
show("保存失败" + e.getMessage());
}
}
/**
* 模拟一个广播,通知系统sdcard被挂载
* */
private void notifyHasMount() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
sendBroadcast(intent);
}
/**
* 查看图片
*/
@Override
public void onClick(View v) {
if (v == mBtnView) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("image/*");
startActivity(intent);
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_canvas"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:id="@+id/btn_view_image"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看图片" />
</LinearLayout>
标签:layout,画画,private,event,int,new,Android,版一,android From: https://blog.51cto.com/u_12516227/6055873