Android利用canvas画画板

首先新建一个项目工程,建立文件,如下图所示

首先配置页面布局文件activity_main.xml,如下图所示:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6
 7     <ImageView
 8         android:id="@+id/iv"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:background="@drawable/bg"
12          />
13     <TextView
14           android:layout_width="wrap_content"
15            android:layout_height="wrap_content"
16            android:text="画笔的粗细"
17         />
18     <SeekBar
19          android:id="@+id/sb"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:max="256"
23         />
24     <TextView
25           android:layout_width="wrap_content"
26            android:layout_height="wrap_content"
27            android:text="颜色"
28         />
29     <Spinner
30         android:id="@+id/sp"
31          android:layout_width="wrap_content"
32            android:layout_height="wrap_content"
33            android:entries="@array/color"
34         />
35     <Button
36             android:id="@+id/btn"
37           android:layout_width="wrap_content"
38            android:layout_height="wrap_content"
39            android:text="保存"
40         />
41
42
43
44 </LinearLayout>

然后书写主页的代码MainActivity.java代码如下

  1 package com.xunfang.drawing;
  2
  3 import java.io.File;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6
  7 import android.app.Activity;
  8 import android.content.Intent;
  9 import android.graphics.Bitmap;
 10 import android.graphics.Bitmap.CompressFormat;
 11 import android.graphics.BitmapFactory;
 12 import android.graphics.Canvas;
 13 import android.graphics.Color;
 14 import android.graphics.Matrix;
 15 import android.graphics.Paint;
 16 import android.net.Uri;
 17 import android.os.Bundle;
 18 import android.os.Environment;
 19 import android.util.MonthDisplayHelper;
 20 import android.view.Menu;
 21 import android.view.MenuItem;
 22 import android.view.MotionEvent;
 23 import android.view.View;
 24 import android.view.View.OnClickListener;
 25 import android.view.View.OnTouchListener;
 26 import android.widget.AdapterView;
 27 import android.widget.AdapterView.OnItemClickListener;
 28 import android.widget.AdapterView.OnItemSelectedListener;
 29 import android.widget.Button;
 30 import android.widget.ImageView;
 31 import android.widget.SeekBar;
 32 import android.widget.Spinner;
 33 import android.widget.Toast;
 34
 35
 36 public class MainActivity extends Activity {
 37     private SeekBar sb;
 38     private ImageView iv;
 39     private Button btn;
 40     private Spinner sp;
 41     private String[] color ;
 42
 43     private Bitmap bm;
 44     private Bitmap copy;
 45     private  Canvas canvas;
 46     private Paint paint;
 47     private File file;
 48     private int yanse;
 49
 50     @Override
 51     protected void onCreate(Bundle savedInstanceState) {
 52         super.onCreate(savedInstanceState);
 53         setContentView(R.layout.activity_main);
 54
 55        //拿到在xml文件中定义的颜色数组
 56         color = getResources().getStringArray(R.array.color) ;
 57         //实例化
 58         initData();
 59         //设置监听器
 60         setLister();
 61         //画画
 62         loadingImage();
 63
 64     }
 65     private void loadingImage() {
 66         // 加载原始图片
 67         bm = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
 68         // 需要创建一个和原始的图片大小一样的空白图片(一张纸,上面没有任何数据)
 69         copy = bm.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
 70         // 需要一个画板,画板上铺上白纸
 71         canvas= new Canvas(copy);
 72         // 创建画笔
 73         paint= new Paint();
 74
 75         // 给imageView空间加载一个滑动监听器
 76         iv.setOnTouchListener(new OnTouchListener() {
 77             int startx;
 78             int starty;
 79             @Override
 80             public boolean onTouch(View v, MotionEvent event) {
 81                 // 拿到动作
 82                 int type = event.getAction();
 83                 switch (type) {
 84                 case MotionEvent.ACTION_DOWN:
 85                     startx = (int) event.getX();
 86                     starty = (int) event.getY();
 87                     break;
 88                 case MotionEvent.ACTION_MOVE:
 89                     int endx = (int) event.getX();
 90                     int endy = (int) event.getY();
 91                     //画画
 92                     canvas.drawLine(startx, starty, endx, endy, paint);
 93                     startx = (int) event.getX();
 94                     starty = (int) event.getY();
 95                     iv.setImageBitmap(copy);
 96                     break;
 97                 case MotionEvent.ACTION_UP:
 98
 99                     break;
100                 }
101                 return true;
102             }
103         });
104
105     }
106     private void setLister() {
107         //下拉框
108         sp.setOnItemSelectedListener(new OnItemSelectedListener() {
109             @Override
110             public void onItemSelected(AdapterView<?> parent, View view,
111                     int position, long id) {
112                 Toast.makeText(getApplicationContext(), "你点击的是:" +  color[position], 0).show();
113                 switch (position) {
114                 case 1:
115                     paint.setColor(Color.GREEN);
116                     break;
117                 case 2:
118                     paint.setColor(Color.BLUE);
119                     break;
120                 case 3:
121                     paint.setColor(Color.BLACK);
122                     break;
123                 case 4:
124                     paint.setColor(Color.YELLOW);
125                     break;
126                 case 0:
127                     paint.setColor(Color.RED);
128                     break;
129                 }
130             }
131             @Override
132             public void onNothingSelected(AdapterView<?> parent) {
133             }
134         });
135         //保存
136         btn.setOnClickListener(new OnClickListener() {
137             @Override
138             public void onClick(View v) {
139                 try {
140                     //指定图片的存储路径
141                     file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/draw.png");
142                     FileOutputStream fos = new FileOutputStream(file);
143                     copy.compress(CompressFormat.PNG, 100, fos);
144                     Toast.makeText(getApplicationContext(), "保存成功", 0).show() ;
145                 } catch (Exception e) {
146                 }
147                 //欺骗系统,告诉系统插入一个sd卡
148                 Intent intent = new Intent();
149                 intent.setAction(intent.ACTION_MEDIA_MOUNTED);
150                 intent.setData(Uri.fromFile(file));
151                 sendBroadcast(intent);
152             }
153         });
154     }
155     private void initData() {
156             sb = (SeekBar) findViewById(R.id.sb);
157             btn = (Button) findViewById(R.id.btn);
158             sp = (Spinner) findViewById(R.id.sp);
159             iv = (ImageView) findViewById(R.id.iv);
160     }
161
162
163 }

AndroidManifest.xml配置文件如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.xunfang.drawing"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6
 7     <uses-sdk
 8         android:minSdkVersion="16"
 9         android:targetSdkVersion="21" />
10
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name=".MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25     </application>
26
27 </manifest>

然后用虚拟机测试如下所示:

可以在模拟器看一下,生成的文件

表示验证成功了

时间: 2024-10-23 16:04:13

Android利用canvas画画板的相关文章

Android利用canvas画各种图形

canvas通俗的说就是一张画布,我们可以使用画笔paint,在其上面画任意的图形. 原理: 可以把canvas视为Surface的替身或者接口,图形便是绘制在Surface上的.Canvas封装了所有的绘制调用.通过Canvas, 绘制到Surface上的内容首先存储到一个内存区域(也就是对应的Bitmapz中),该Bitmap最终会呈现到窗口上. 使用: 1.Canvas可以直接new Canvas(): 2.在View中重写OnDraw()方法,里面有一个Canvas,今天讨论的内容. 方

Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)

1.首先说一下canvas类: Class OverviewThe Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path,

Android利用canvas画各种图形 及Paint用法 .

引自:http://blog.csdn.net/carlfan/article/details/8139984 1.首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing

(转)Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)

来自:http://blog.csdn.net/rhljiayou/article/details/7212620 1.首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writi

Android小应用-----画画板

public class MainActivity extends Activity { private ImageView iv; float startX = 0; float startY = 0; //获得一个可以被bitmap Bitmap bitmap = Bitmap.createBitmap(400, 400,Bitmap.Config.ARGB_8888); //创建画布 Canvas canvas = new Canvas(bitmap); //创建画笔 Paint pain

Android 用Canvas画textview、bitmap、矩形(裁剪)、椭圆、线、点、弧

初始化对象 private Paint mPaint;//画笔 private int count;//点击次数 private Rect rect;//矩形 public CounstomView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); //初始化画笔 mPaint = new Paint(); rect = new Rect(); setOnClickListe

10分钟,利用canvas画一个小的loading界面(顺便讨论下绘制效率问题)

首先利用定义下canvas得样式 <canvas width="1024" height="720" id="canvas" style="border: 1px solid #808080;display: block;margin: 100px auto;>你的游览器不支持canvas</canvas> 这里主要要说的就是宽高,不要在style里面定义,不然会被拉伸.(对于这点,建议大家看下W3c文档,不是很

10分钟,利用canvas画一个小的loading界面

首先利用定义下canvas得样式 <canvas width="1024" height="720" id="canvas" style="border: 1px solid #808080;display: block;margin: 100px auto;>你的游览器不支持canvas</canvas> 这里主要要说的就是宽高,不要在style里面定义,不然会被拉伸.(对于这点,建议大家看下W3c文档,不是很

canvas画画板

制作一个画画板,有清屏有橡皮擦有画笔可以换颜色 style样式 <head> <meta charset="UTF-8"> <title>画画板</title> <style> body{ background-color:#ccc; } .control-bar{ vertical-align:top; display:inline-block; } </style> </head> html结构 &