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>

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

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

表示验证成功了

posted @ 2015-07-13 00:22  阳阳(ysq)  阅读(1201)  评论(0编辑  收藏  举报