代码有点粗糙,但是能实现我们所需要的功能,废话不多说了,直接上代码吧
1、利用drawable(mimap)中的图片资源作为背景
package com.example.viewtest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SecondActivity extends Activity {
private ImageView img,imageTwo;
private Bitmap mBitmap;
private Canvas canvas;
private Paint paint;
// 重置按钮
private Button reset_btn,save_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
img = (ImageView) findViewById(R.id.img);
imageTwo = (ImageView) findViewById(R.id.img_two);
reset_btn = (Button) findViewById(R.id.reset_btn);
save_btn = (Button) findViewById(R.id.save_btn);
reset_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img.setImageBitmap(null);
showImage();
}
});
save_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setmBitmap();
}
});
// 绘图
showImage();
}
private void showImage() {
int height = getWindow().getWindowManager().getDefaultDisplay().getHeight();
int wehit = getWindow().getWindowManager().getDefaultDisplay().getWidth();
// 创建一张图片
mBitmap = drawableToBitamp(getResources().getDrawable(R.drawable.siji)); //drawable中的图片
// 创建一张画布
canvas = new Canvas(mBitmap);
// 创建画笔
paint = new Paint();
// 画笔颜色为蓝色
paint.setColor(Color.BLUE);
// 宽度5个像素
paint.setStrokeWidth(5);
// 先将背景画上
canvas.drawBitmap(mBitmap, new Matrix(), paint);
img.setImageBitmap(mBitmap);
img.setOnTouchListener(new OnTouchListener() {
int startX;
int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 获取手按下时的坐标
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
// 获取手移动后的坐标
int endX = (int) event.getX();
int endY = (int) event.getY();
// 在开始和结束坐标间画一条线
canvas.drawLine(startX, startY, endX, endY, paint);
// 刷新开始坐标
startX = (int) event.getX();
startY = (int) event.getY();
img.setImageBitmap(mBitmap);
break;
}
return true;
}
});
}
<span style="white-space:pre"> </span>//保存到内存卡中
public void setmBitmap(){
File file = new File(Environment.getExternalStorageDirectory(),
"/test" + ".png");
if(!file.exists()){
OutputStream stream;
try {
file.createNewFile() ;
stream = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}else {
OutputStream stream;
try {
stream = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
readSdPic();
}
<span style="white-space:pre"> </span>//读取出图片并显示
public void readSdPic(){
File file = new File(Environment.getExternalStorageDirectory(),
"/test" + ".png");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageTwo.setImageBitmap(bitmap); //显示图片
}
<span style="white-space:pre"> </span>//<span style="font-family: Arial, Helvetica, sans-serif;">drawable转换为bitmap</span>
private Bitmap bitmap;
private Bitmap drawableToBitamp(Drawable drawable)
{
int w = drawable.getMinimumWidth();
int h = drawable.getMinimumHeight();
// int height = getWindow().getWindowManager().getDefaultDisplay().getHeight();
int wehit = getWindow().getWindowManager().getDefaultDisplay().getWidth();
System.out.println("Drawable转Bitmap");
Bitmap.Config config =
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
bitmap = Bitmap.createBitmap(wehit,500,config);
//注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
}
这样就可以实现图片的保存了,这种方式相对来说要简单一些,下面第二种通过读取手机图片或者拍照就要麻烦一点了
2、我们获取到的图片大小不一,有的能显示全屏,有的则不能显示。上面的代码如果试过就知道,只有当你点击到图片原来尺寸上的时候才能够绘画,如果强行让imageview全屏显示,那么就会出现错位的情况,因为画笔只监听了bitmap(获取图片)原来的尺寸,超出的部分则不会画到图片上,相当于无效的绘图,这样就出现了错位的感觉
package com.house.checkhouse.activity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import com.house.checkhouse.R;
import com.house.checkhouse.model.message.ImageInfo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class SecondActivity extends Activity {
public static final String IMAGE = "images";
private ImageView img;
private Bitmap mBitmap,nowBitmap;
private Canvas canvas;
private Paint paint;
private ImageInfo imageInfo;
private String fileUri;
// 重置按钮
private Button reset_btn,save_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
imageInfo = getIntent().getParcelableExtra("image"); //通过打开相册或者照相传递过来的bitmap图片
fileUri = getNowFileName();
mBitmap = imageInfo.getBitmap();
nowBitmap = mBitmap;
img = (ImageView) findViewById(R.id.img);
reset_btn = (Button) findViewById(R.id.reset_btn);
save_btn = (Button) findViewById(R.id.save_btn);
reset_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img.setImageBitmap(null);
showImage();
}
});
save_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// setmBitmap();
}
});
// 绘图
showImage();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.gc();
setmBitmap();
Intent intent = new Intent();
intent.putExtra(IMAGE,fileUri);
setResult(RESULT_OK, intent);
return super.onKeyDown(keyCode, event);
}
private void showImage() {
// 创建一张图片
nowBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888,true);
nowBitmap = big(nowBitmap); //放大bitmap
// 创建一张画布
canvas = new Canvas(nowBitmap);
// 创建画笔
paint = new Paint();
// 画笔颜色为蓝色
paint.setColor(Color.BLUE);
// 宽度1个像素
paint.setStrokeWidth(3);
// 先将背景画上
canvas.drawBitmap(nowBitmap, new Matrix(), paint);
img.setImageBitmap(nowBitmap);
img.setOnTouchListener(new OnTouchListener() {
int startX;
int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 获取手按下时的坐标
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
// 获取手移动后的坐标
int endX = (int) event.getX();
int endY = (int) event.getY();
// 在开始和结束坐标间画一条线
canvas.drawLine(startX, startY, endX, endY, paint);
// 刷新开始坐标
startX = (int) event.getX();
startY = (int) event.getY();
img.setImageBitmap(nowBitmap);
break;
}
return true;
}
});
}
/**
* 获取文件路径和名字(保存路径)
* @return
*/
public String getNowFileName(){
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");//格式大小写有区别
String sysDatetime = fmt.format(Calendar.getInstance().getTime());//2016年02月25日 13:23:40
String fileName = Environment.getExternalStorageDirectory()+File.separator+"com.house"+File.separator+sysDatetime+".png";
return fileName;
}
public void setmBitmap(){
File file = new File(fileUri);
if(!file.exists()){
OutputStream stream;
try {
file.createNewFile() ;
stream = new FileOutputStream(file);
nowBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}else {
OutputStream stream;
try {
stream = new FileOutputStream(file);
nowBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 放大bitmap(放大到全屏显示,这样才不会出现错位情况,完全可以指哪儿画哪儿)
* @param bitmap
* @return
*/
private Bitmap big(Bitmap bitmap) {
int height = bitmap.getHeight();
int width = bitmap.getWidth();
int heightWon = getWindow().getWindowManager().getDefaultDisplay().getHeight();
int widthWon = getWindow().getWindowManager().getDefaultDisplay().getWidth();
float sizeW = (float) widthWon/width;
float sizeH = (float) heightWon/height;
Matrix matrix = new Matrix();
float size = sizeH<sizeW?sizeH:sizeW; //(按照一长或宽的比例来缩放)
matrix.postScale(size,size); //长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
return resizeBmp;
}
}
好了,这样就大功告成了,写起来感觉很简单,但是花费了我一天的时间才搞出来,如果大家有什么更好的方法,希望能分享一下