package com.example.day303;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.OutputStream;
/**
* 选择一张图片然后在图片上根据手指移动绘图,然后保存到SD卡
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener,View.OnTouchListener{
private ImageView imageView;
private Button choosePic,savePic;
Bitmap bitmap;
Bitmap alertBitmap;
Canvas canvas;
Paint paint;
Matrix matrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
choosePic=(Button)findViewById(R.id.choosePic);
savePic=(Button)findViewById(R.id.savePic);
imageView=(ImageView)findViewById(R.id.imageView);
choosePic.setOnClickListener(this);
savePic.setOnClickListener(this);
imageView.setOnTouchListener(this);
}
@Override
public void onClick(View v) {
//打开图库
if (v==choosePic) {
Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,0);
}
//保存图片
if (v==savePic) {
if (alertBitmap != null) {
try {
Uri imageUri=getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new ContentValues());
OutputStream outputStream=getContentResolver().openOutputStream(imageUri);
//compress()压缩位图
alertBitmap.compress(Bitmap.CompressFormat.JPEG,90,outputStream);
Toast toast=Toast.makeText(this,"success",Toast.LENGTH_LONG);
toast.show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode==RESULT_OK) {
//获得图片的路径URI
Uri uri=intent.getData();
Display display=getWindowManager().getDefaultDisplay();
float dw=display.getWidth();
float dh=display.getHeight();
try {
//保存图片尺寸
BitmapFactory.Options biops=new BitmapFactory.Options();
biops.inJustDecodeBounds=true;
bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri),null,biops);
//计算缩放比例
int widthRatio=(int)Math.ceil(biops.outWidth/(double)dw);
int heightRatio=(int)Math.ceil(biops.outHeight/(double)dh);
//根据宽或者高进行缩放
if (widthRatio>1 && heightRatio>1) {
if (widthRatio > heightRatio) {
biops.inSampleSize = widthRatio;
} else {
biops.inSampleSize=heightRatio;
}
}
//给图片解码
biops.inJustDecodeBounds=false;
bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri),null,biops);
//在原有位图对象基础上创建一个和原位图大小相同的空位图
alertBitmap=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig());
//在画布上绘制该位图
canvas=new Canvas(alertBitmap);
paint=new Paint();
matrix=new Matrix();
canvas.drawBitmap(bitmap,matrix,paint);
imageView.setImageBitmap(alertBitmap);
} catch (Exception e) {
}
}
}
float downX=0,downY=0,upX=0,upY=0;
/**
* @param v
* @param event
* @return
* 触摸方法回调
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
int action=event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downX=event.getX();
downY=event.getY();
break;
case MotionEvent.ACTION_UP:
upX=event.getX();
upY=event.getY();
canvas.drawLine(downX,downY,upX,upY,paint);
imageView.invalidate();
break;
case MotionEvent.ACTION_MOVE:
upX=event.getX();
upY=event.getY();
canvas.drawLine(downX,downY,upX,upY,paint);
imageView.invalidate();
downX=upX;
downY=upY;
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
}
选择一张图片然后在图片上根据手指移动绘图,然后保存到SD卡
最新推荐文章于 2019-01-04 11:02:28 发布