在定义的区间中可以作画,进行编辑,有简单的特效:颜色,字体刷,同时可以保存在本地
需要添加sd卡写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
保存图片的时候:
sd卡每次遍历sd卡所有的文件,系统会把所有的多媒体文件,都在MediaStore数据库中生一个索引,数据库中保存了文件的文件名,路径,大小,长度和艺术家等参数。
图库,音乐,视频每次启动的时候并不会遍历sd卡寻找多媒体文件,而是直接从MediaStore数据库中读取多媒体文件,通过数据库的索引寻找对应的多媒体文件以后,把文件显示在界面。
实现代码如下:
//发送sd卡就绪广播,刷新数据库里面的数据
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
sendBroadcast(intent);
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:onClick="red"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色"
android:onClick="green"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刷子"
android:onClick="brush"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:onClick="save"
/>
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.ldw.paint;
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.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
private int startX;
private int startY;
private Bitmap bitCopy;
private Paint paint;
private Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加载动画
Bitmap bitSrc = BitmapFactory.decodeResource(getResources(), R.drawable.on);
//创建图片副本
bitCopy = Bitmap.createBitmap(bitSrc.getWidth(), bitSrc.getHeight(), bitSrc.getConfig());
paint = new Paint();
canvas = new Canvas(bitCopy);
//绘制副本
canvas.drawBitmap(bitSrc, new Matrix(), paint);
iv = (ImageView)findViewById(R.id.iv);
iv.setImageBitmap(bitCopy);
//设置触碰侦听
iv.setOnTouchListener(new OnTouchListener(){
//触摸事件产生时,此方法被调用
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch(action){
//触摸屏幕
case MotionEvent.ACTION_DOWN:
startX = (int) event.getX();
startY = (int) event.getY();
break;
//屏幕上移动
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX();
int y = (int) event.getY();
canvas.drawLine(startX, startY, x, y, paint);
//每次绘制结束以后,本次绘制的结束坐标变成下次绘制的初始坐标
startX = x;
startY = y;
iv.setImageBitmap(bitCopy);
System.out.println("x=" + x + "y=" +y);
break;
//离开屏幕
case MotionEvent.ACTION_UP:
break;
}
//true:告诉系统,这个事件由自己处理,
//false:告诉系统,事件由自己处理,系统会把触摸事件发送到iamgeView的父节点
return true;
}
});
}
public void red(View v){
paint.setColor(Color.RED);
}
public void green(View v){
paint.setColor(Color.GREEN);
}
public void brush(View v){
paint.setStrokeWidth(4);
}
public void save(View v){
File file = new File("sdcard/tupian.jpeg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitCopy.compress(CompressFormat.JPEG, 100, fos);
//发送sd卡就绪广播,刷新数据库里面的数据
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
sendBroadcast(intent);
}
}