Android实现画画板案例
作者:常利兵 发布时间:2023-01-11 08:35:28
标签:Android,画板
本文实例为大家分享了Android实现画画板的具体代码,供大家参考,具体内容如下
① 准备一个布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.it.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="修改颜色"
android:onClick="color"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="粗细+1"
android:onClick="size"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="保存图片"
android:onClick="save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="@+id/iv_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
② 核心代码
package com.example.it;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
private ImageView iv_show;
private Bitmap copyBm;
private float x;
private float y;
private Paint paint;
private int paintsize = 1;
private FileOutputStream fos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
iv_show = (ImageView) findViewById(R.id.iv_bg);
//创建一个空白的bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
copyBm = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//创建画布
final Canvas canvas = new Canvas(copyBm);
//使用画布绘制图片
paint = new Paint();
canvas.drawBitmap(bitmap,new Matrix(), paint);
iv_show.setImageBitmap(copyBm);
//为画笔在屏幕上移动设置监听事件
iv_show.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//获取当前事件的类型
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
//如果是手指按下时,记录一下手指的位置
x = event.getX();
y = event.getY();
Log.e(TAG,x+"******"+y);
break;
case MotionEvent.ACTION_MOVE:
//如果手指正在移动时
float x1 = event.getX();
float y1 = event.getY();
canvas.drawLine(x,y,x1,y1, paint);
x = x1;
y = y1;
Log.e(TAG,x1+"**********"+y1);
break;
case MotionEvent.ACTION_UP:
break;
}
iv_show.setImageBitmap(copyBm);
return true;
}
});
}
//修改颜色
public void color(View view) {
paint.setColor(Color.RED);
}
//修改线条粗细
public void size(View view) {
paintsize+=1;
paint.setStrokeWidth(paintsize);
}
//保存图片
public void save(View view) {
//设置图片保存的位置
File file = new File(Environment.getExternalStorageDirectory(), SystemClock.currentThreadTimeMillis()+".png");
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//设置参数保存的质量
copyBm.compress(Bitmap.CompressFormat.PNG,100,fos);
//定义一个意图,通过发出广播告诉系统扫描指定的位置的文件
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
}
}
③ 权限信息
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
来源:https://blog.csdn.net/qq_32890771/article/details/71566165
0
投稿
猜你喜欢
- 一、简介在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,
- 什么是抽象类什么是抽象类呢?抽象类顾名思义就是很抽象,就是当我们没有足够的信息去描述这个类的时候我们就可以先不用描述,这样的类就是抽象类。用
- 为什么需要在应用程序中增加渠道信息?Android应用的发布需要面对各种各样的市场,我们称之为渠道。有的时候,我们需要知道应用是从哪个渠道下
- 这篇文章主要介绍了Spring框架构造注入type属性实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 主要讲解Android Studio中生成aar文件以及本地方式使用aar文件的方法,具体内容详情如下所示:在Android Studio中
- 本文实例讲述了C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法,主要是通过重写WndProc来实现的。分享给大家供大家参考
- 目前大多数开发者使用EventBus或者Otto作为事件总线通信库,对于RxJava使用者来说,RxJava也可以轻松实现事件总线,因为它们
- Feign远程调用Multipartfile参数今天在写业务代码的时候遇到的问题, 前端请求A服务,能正确把参数给到A服务<参数里面包
- 下载gradle直接百度gradle,然后点击链接进去就可以找到,这里附上下载链接:gradle下载安装gradle##解压下载下来的zip
- 本文实例讲述了Spring使用@Resource配置依赖操作。分享给大家供大家参考,具体如下:一 配置<?xml version=&q
- 定时任务1import lombok.extern.slf4j.Slf4j;/** * @author Created by niugang
- 一.前言在日常的开发中,经常需要对方法参数进行校验(非空、长度等)。如果采用hardcode去校验(if..else..),会造成代码冗余,
- 目录为什么要实现调用链跟踪?如何实现?第一步,看图、看场景,用户浏览器的一次请求行为所走的路径是什么样的第二步,实现。不想看代码可直接拉最后
- BeanDefinitionRegistryPostProcessor概述可以看到BeanDefinitionRegistryPostPro
- 在使用SuperSocket来写服务器的过程中,这是一个非常快速的开发方式,也非常好用。不过学习的曲线有点高,在使用的过程中经常会遇到各种各
- 最近在项目开发中,使用spring boot+mybatis的架构,数据库设计主键id时定义为bigint类型,使用mybatis的自动生成
- 本文实例为大家分享了C#实现网页画图的具体代码,供大家参考,具体内容如下代码贴着保存下using System;using System.C
- 一、MVVM 和 MVVMLight介绍MVVM是Model-View-ViewModel的简写。类似于目前比较流行的MVC、MVP设计模式
- 什么是EurekaEureka是Netfilx开源的一个用来实现微服务的注册与发现的组件。它包含Server和Client两部分。为什么要有
- 本文实例为大家分享了Java实现部门员工管理的具体代码,供大家参考,具体内容如下项目作业:部门员工管理题目要求:某公司要开发内部的 &