前言
本文给大家分享一个使用Android开发写字板功能Dem、简单操作内存中的图像、对图像进行简单的处理、绘制直线、以达到写字板的效果
效果图如下
XML布局代码
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.tomes.paint.MainActivity" >
<ImageView
android:id="@ id/iv_drawingBoard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg"/>
</RelativeLayout>
Java代码
public void init() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.bg);
copyBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
paint = new Paint();
canvas = new Canvas(copyBitmap);
Matrix matrix=new Matrix();
canvas.drawBitmap(bitmap, matrix, paint);
imageView = (ImageView) findViewById(R.id.iv_drawingBoard);
imageView.setImageBitmap(copyBitmap);
imageView.setOnTouchListener(new OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
startX=event.getX();
startY=event.getY();
break;
case MotionEvent.ACTION_MOVE:
float currentX=event.getX();
float currentY=event.getY();
canvas.drawLine(startX, startY, currentX, currentY, paint);
imageView.setImageBitmap(copyBitmap);
startX=currentX;
startY=currentY;
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});
}
源码下载:点击这里
总结
以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流。