Android实现画板、写字板功能(附源码下载)
作者:daisy 时间:2021-11-01 17:40:56
前言
本文给大家分享一个使用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,画板,写字板
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Data JPA 设置字段默认值方式
2021-08-13 07:27:06
spring boot的拦截器简单使用示例代码
2021-09-29 04:07:07
使用Spring Data Redis实现数据缓存的方法
2021-08-02 10:19:25
![](https://img.aspxhome.com/file/2023/6/71706_0s.png)
浅谈virtual、abstract方法和静态方法、静态变量理解
2022-08-29 02:29:14
java判断中文字符串长度的简单实例
2022-12-19 09:00:26
Java Valhalla Project项目介绍
2021-10-03 00:29:28
![](https://img.aspxhome.com/file/2023/0/83120_0s.png)
C# partial关键字说明
2021-12-29 15:29:54
Java如何使用ReentrantLock实现长轮询
2023-11-26 00:09:08
详解SpringBoot和SpringBatch 使用
2023-08-17 19:00:38
Spring createBeanInstance实例化Bean
2023-06-17 17:26:27
Java Swing JProgressBar进度条的实现示例
2023-07-15 17:48:47
![](https://img.aspxhome.com/file/2023/3/80893_0s.gif)
MyBatis找不到mapper文件的实现
2023-12-15 09:22:11
![](https://img.aspxhome.com/file/2023/1/61131_0s.png)
Java模拟实现斗地主发牌
2022-07-09 02:16:52
![](https://img.aspxhome.com/file/2023/5/100665_0s.jpg)
c#固定长度的随机字符串例子
2021-10-24 06:11:10
SpringBoot+Redis实现数据字典的方法
2022-08-03 14:22:29
![](https://img.aspxhome.com/file/2023/4/65364_0s.jpg)
c# 给pdf添加数字签名的步骤
2022-04-05 17:58:01
![](https://img.aspxhome.com/file/2023/1/71741_0s.png)
ViewPager实现漂亮的引导页
2022-12-27 21:56:58
![](https://img.aspxhome.com/file/2023/6/108266_0s.png)
通过java反射机制动态调用某方法的总结(推荐)
2022-11-04 01:38:32
java实现砸金蛋抽奖功能
2022-02-22 01:26:50
C#探秘系列(三)——StackTrace,Trim
2022-04-27 21:24:03
![](https://img.aspxhome.com/file/2023/6/80026_0s.png)