Android画画板的制作方法
作者:heibuke 时间:2022-08-25 06:08:07
本文实例为大家分享了Android画画板展示的具体代码,供大家参考,具体内容如下
main.xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.example.demo.MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:onClick="onplay"
/>
<Button
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色"
android:onClick="onplay"
/>
<Button
android:id="@+id/root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刷子"
android:onClick="onplay"
/>
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:onClick="onplay"
/>
<Button
android:id="@+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="涂漆"
android:onClick="onplay"
/>
</LinearLayout>
</RelativeLayout>
main布局
/*
画板canvas 画板paint 手势识别器
整体思路:因为我是图片是作画,实际是对图片进行修改,起到画图的效果
1.原图,白纸,画笔,画板
2.根据手势识别进行作画
*/
public class MainActivity extends AppCompatActivity {
private Bitmap bitmap;
private Canvas canvas;
private ImageView iv;
private int startx;
private int starty;
private Paint paint;
private Bitmap bmSrc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加载原图
bmSrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
//创建白纸,宽,高,图片的参数
bitmap = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
//创建画板,参数是白纸对象
canvas = new Canvas(bitmap);
//创建画笔
paint = new Paint();
//在纸上作画
iv=(ImageView)findViewById(R.id.iv);
canvas.drawBitmap(bmSrc,new Matrix(), paint);
//-----------------手势识别器和画笔结合的知识-------------------
//给控件设置手势适配器,可以得到用户在这个控件上所做的手势
iv.setOnTouchListener(new View.OnTouchListener() {
//当用户手在这个控件时,指的就是用户的手对控件滑动,按下,松开的三种场景,自动回调
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN://按下时回调一次
//获取用户手指按下时的坐标
startx = (int) motionEvent.getX();
starty = (int) motionEvent.getY();
break;
case MotionEvent.ACTION_MOVE://手指滑动时,不停地调用
int newx = (int) motionEvent.getX();
int newy = (int) motionEvent.getY();
//在背景图画线
canvas.drawLine(startx,starty,newx,newy, paint);
startx=newx;
starty=newy;
iv.setImageBitmap(bitmap);
break;
case MotionEvent.ACTION_UP://松开时回调一次
break;
}
//事情分发机制
//true:iv处理该触摸事件
//false:iv不处理该触摸事件,事件传递给上一级
return true;
}
});
}
public void onplay(View view){
switch (view.getId()){
case R.id.red:
paint.setColor(Color.RED);
break;
case R.id.green:
paint.setColor(Color.GREEN);
break;
case R.id.root:
paint.setStrokeWidth(5);
break;
case R.id.save:
if(SaveViewUtil.saveScreen(iv)){
Toast.makeText(this, "截图成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "截图失败,请检查sdcard是否可用", Toast.LENGTH_SHORT).show();
}
break;
//涂漆
case R.id.finish:
canvas.drawRect(new Rect(0,0,width,height), paint);
break;
}
}
}
这是一个把画的图存储SD卡的工具类
public class SaveViewUtil {
private static final File rootDir = new File(Environment.getExternalStorageDirectory()+File.separator);
/**保存截图的方法*/
public static boolean saveScreen(View view){
//判断sdcard是否可用
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
return false;
}
if(!rootDir.exists()){
rootDir.mkdir();
}
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
try {
bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(new File(rootDir,System.currentTimeMillis()+".jpg")));
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}finally{
view.setDrawingCacheEnabled(false);
bitmap = null;
}
}
}
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
来源:http://blog.csdn.net/qq_39566440/article/details/76695440
标签:Android,画板
0
投稿
猜你喜欢
Android 通过onDraw实现在View中绘图操作的示例
2023-07-14 02:15:38
基于Tomcat7、Java、WebSocket的服务器推送聊天室实例
2023-11-25 23:35:34
Java实现登录和注册案例
2022-02-27 04:57:46
C++多重继承二义性原理实例解析
2021-10-08 20:18:33
Android中如何指定SnackBar在屏幕的位置及小问题解决
2023-08-07 07:56:19
Android实现视图轮播效果
2023-04-14 08:52:10
Android开启动画之渐隐渐现效果
2021-07-21 08:45:28
C#使用foreach遍历哈希表(hashtable)的方法
2022-10-07 20:00:44
Spring Boot热加载jar实现动态插件的思路
2021-09-25 16:37:55
springboot中使用@Transactional注解事物不生效的坑
2021-10-03 10:01:47
ReentrantLock获取锁释放锁的流程示例分析
2021-08-05 20:51:10
Spring Cache+Redis缓存数据的实现示例
2023-11-26 11:53:20
Java web spring异步方法实现步骤解析
2023-12-19 03:14:58
ImportBeanDefinitionRegistrar手动控制BeanDefinition创建注册详解
2021-11-11 18:49:18
如何基于JavaFX开发桌面程序
2023-10-30 11:06:19
java从输入流中获取数据并返回字节数组示例
2021-12-08 22:47:36
C#单线程和多线程的端口扫描器应用比较详解
2023-08-07 21:01:45
Android中的设计模式
2022-09-13 06:33:21
JVM类加载,垃圾回收
2022-08-16 02:44:36
Maven之导入thymeleaf依赖飘红问题及解决
2023-11-12 12:49:05