
首先配置页面布局文件activity_main.xml,如下图所示:
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical">
? ? ? ? ? ? android:id="@+id/iv"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:background="@drawable/bg"
? ? ? ? />
? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:text="画笔的粗细"
? ? ? ? />
? ? ? ? ? ? android:id="@+id/sb"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:max="256"
? ? ? ? />
? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:text="颜色"
? ? ? ? />
? ? ? ? ? ? android:id="@+id/sp"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:entries="@array/color"
? ? ? ? />
? ? ? ? ? ? ? ? android:id="@+id/btn"
? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:text="保存"
? ? ? ? />
?
? ?
然后书写主页的代码MainActivity.java代码如下
package com.xunfang.drawing;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
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.Bundle;
import android.os.Environment;
import android.util.MonthDisplayHelper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
? ? private SeekBar sb;
? ? private ImageView iv;
? ? private Button btn;
? ? private Spinner sp;
? ? private String[] color ;
? ?
? ? private Bitmap bm;
? ? private Bitmap copy;
? ? private? Canvas canvas;
? ? private Paint paint;
? ? private File file;
? ? private int yanse;
? ? ? ?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ?
? ? ? //拿到在xml文件中定义的颜色数组
? ? ? ? color = getResources().getStringArray(R.array.color) ;
? ? ? ? //实例化
? ? ? ? initData();
? ? ? ? //设置监听器
? ? ? ? setLister();
? ? ? ? //画画
? ? ? ? loadingImage();
? ? ? ?
? ? }
? ? private void loadingImage() {
? ? ? ? // 加载原始图片
? ? ? ? bm = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
? ? ? ? // 需要创建一个和原始的图片大小一样的空白图片(一张纸,上面没有任何数据)
? ? ? ? copy = bm.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
? ? ? ? // 需要一个画板,画板上铺上白纸
? ? ? ? canvas= new Canvas(copy);
? ? ? ? // 创建画笔
? ? ? ? paint= new Paint();
? ? ? ?
? ? ? ? // 给imageView空间加载一个滑动监听器
? ? ? ? iv.setOnTouchListener(new OnTouchListener() {
? ? ? ? ? ? int startx;
? ? ? ? ? ? int starty;
? ? ? ? ? ? @Override
? ? ? ? ? ? publi