Android实现画画板案例

所属分类: 软件编程 / Android 阅读数: 99
收藏 0 赞 0 分享

本文实例为大家分享了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"/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多