首页 > 技术文章 > 画画版

zhongyinghe 2016-04-01 15:37 原文

1、视图

 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     tools:context=".MainActivity" >
 7 
 8     <ImageView
 9         android:layout_weight="100"
10         android:id="@+id/iv"
11         android:layout_width="fill_parent"
12         android:layout_height="fill_parent"
13         />
14     <Button 
15         android:layout_width="fill_parent"
16         android:layout_height="wrap_content"
17         android:id="@+id/bt"
18         android:onClick="save"
19         android:text="保存图片"
20         />
21 
22 </LinearLayout>

2、权限,因为要把图片保存在外存

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

3、java代码

 1 package com.example.paint;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 
 7 import android.net.Uri;
 8 import android.os.Bundle;
 9 import android.os.Environment;
10 import android.app.Activity;
11 import android.content.Intent;
12 import android.graphics.Bitmap;
13 import android.graphics.Bitmap.CompressFormat;
14 import android.graphics.Canvas;
15 import android.graphics.Color;
16 import android.graphics.Paint;
17 import android.view.Menu;
18 import android.view.MotionEvent;
19 import android.view.View;
20 import android.view.View.OnTouchListener;
21 import android.widget.ImageView;
22 import android.widget.Toast;
23 
24 public class MainActivity extends Activity {
25     private ImageView iv;
26     private Bitmap baseBitmap;
27     private Canvas canvas;
28     private Paint paint;
29     @Override
30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(R.layout.activity_main);
33         
34         iv = (ImageView) findViewById(R.id.iv);
35         paint = new Paint();//画笔
36         paint.setStrokeWidth(5);//画笔的宽度
37         paint.setColor(Color.GREEN);//画笔的颜色
38         
39         
40         baseBitmap = Bitmap.createBitmap(320, 360, Bitmap.Config.ARGB_8888);//创建可以被修改的bitmap
41         canvas = new Canvas(baseBitmap);//以bitmap为基准创建画布
42         canvas.drawColor(Color.WHITE);//设置颜色为白色
43         
44         //监听屏幕触摸
45         iv.setOnTouchListener(new OnTouchListener(){
46             //获取触摸时开始的坐标
47             int startX;
48             int startY;
49             @Override
50             public boolean onTouch(View v, MotionEvent event) {
51                 // TODO Auto-generated method stub
52                 //获取触摸动作
53                 switch(event.getAction()){
54                 case MotionEvent.ACTION_DOWN://第一次接触屏幕
55                     
56                     startX = (int) event.getX();
57                     startY = (int) event.getY();
58                     break;
59                 case MotionEvent.ACTION_MOVE://手指在屏幕上移动
60                     int newX = (int) event.getX();
61                     int newY = (int) event.getY();
62                     canvas.drawLine(startX, startY, newX, newY, paint);//画线
63                     
64                     startX = (int) event.getX();
65                     startY = (int) event.getY();
66                     
67                     iv.setImageBitmap(baseBitmap);
68                     break;
69                 case MotionEvent.ACTION_UP://手指离开屏幕的动作
70                     break;
71                 }
72                 return true;//让事件不停地执行,就得返回true
73             }
74             
75         });
76     }
77 
78 
79     public void save(View view){
80         try {
81             File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
82             FileOutputStream stream = new FileOutputStream(file);
83             baseBitmap.compress(CompressFormat.JPEG, 100, stream);//把bitmap转化为jpg的方式进行保存
84             stream.close();
85             Toast.makeText(this, "保存图片成功", 0).show();
86             
87             //通知sd卡被重新挂载
88             Intent intent = new Intent();
89             intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
90             intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
91             sendBroadcast(intent);
92         } catch (Exception e) {
93             // TODO Auto-generated catch block
94             Toast.makeText(this, "保存图片失败", 0).show();
95             e.printStackTrace();
96         }
97     }
98     
99 }

 

推荐阅读