Android 怎么把应用程序画的图片存到SDCARD中
private View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// 启动Gallery应用
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, FIRST_PIC);
}
};
private View.OnClickListener clickListener2 = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// 启动Gallery应用
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SECOND_PIC);
}
};
public boolean onCreateOptionsMenu(Menu menu){
// super.onCreateOptionsMenu(menu);
//MenuInflater menuInflater = new MenuInflater(this);
//menuInflater.inflate(R.layout.image, menu)
menu.add(0,1,1,"复制");
menu.add(0,2,2,"变换");
menu.add(0,3,3,"亮度");
menu.add(0,4,4,"合成");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
switch(id){
case 1:
//复制一个图像
if(srcBitmap != null){
dstBitmap = getDstImage(null);//这里没有变换
dstImageView.setImageBitmap(dstBitmap);
}
break;
case 2:
//对复制后的图像进行变换
if(srcBitmap != null){
dstBitmap = transferImage();
dstImageView.setImageBitmap(dstBitmap);
}
break;
case 3:
//改变图像的色彩
if(srcBitmap != null){
dstBitmap = ajustImage();
dstImageView.setImageBitmap(dstBitmap);
}
break;
case 4:
if(srcBitmap != null && dstBitmap != null){
dstBitmap = compositeImages();
dstImageView.setImageBitmap(dstBitmap);
}
break;
}
return true;
}
/**
* 为了创建一个图像的副本,我们可以在创建一个新的空的Bitmap,然后在这个Bitmap上绘制一个Bitmap
* 这个空的Bitmap应该和已存在的Bitmap具有相同的尺寸和颜色深度
*
* 然后我们需要一个Canvas对象,一个Canvas简单说,就是一个画布,存放Bitmap,在构造时,就可以传入Bitmap对象
* 同时,Canvas中定义了很多便捷的画图方法,方便我们绘制各种图形
* 接下来,如果我们需要处理颜色和对比度,我们需要一个Paint对象,通过Paint我们可以设置画笔的各种特性。
*
* 最后,我们调用Canvas的drawBitmap就可以将原Bitmap绘制在dstBitmap上了
*
*/
private Bitmap getDstImage(Matrix matrix){
Bitmap bmp = null;
//下面这个Bitmap中创建的函数就可以创建一个空的Bitmap
//返回的是一个可以改变的Bitmap对象,这样我们后面就可以对其进行变换和颜色调整等操作了
bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
//创建Canvas对象,
Canvas canvas = new Canvas(bmp);
//创建Paint对象,这里先不用
Paint paint = new Paint();
//在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了
if(matrix != null){
//如果matrix存在,则采用变换
canvas.drawBitmap(dstBitmap, matrix, paint);
}else{
canvas.drawBitmap(srcBitmap, 0, 0, paint);
}
return bmp;
}
/**
* 重载getDstImage函数,传入定制的Paint对象
* @param matrix
* @param paint
* @return
*/
private Bitmap getDstImage(Matrix matrix, Paint paint){
Bitmap bmp = null;
//下面这个Bitmap中创建的函数就可以创建一个空的Bitmap
//返回的是一个可以改变的Bitmap对象,这样我们后面就可以对其进行变换和颜色调整等操作了
bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
//创建Canvas对象,
Canvas canvas = new Canvas(bmp);
//在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了
if(matrix != null){
//如果matrix存在,则采用变换
canvas.drawBitmap(dstBitmap, matrix, paint);
}else{
canvas.drawBitmap(srcBitmap, 0, 0, paint);
}
return bmp;
}
/**
* 为了放大缩小、旋转图像,我们要使用Matrix类。Matrix类是一个三维矩阵。
* 在Android屏幕中,图像的每个像素对应都是一个坐标,一个坐标由x/y/z组成
* ------------------------
* cosX -sinX translateX
* sinX cosX translateY
* 0 0 scale
* ------------------------
* 第一行的值,影响着x坐标。比如 1 0 0 =>x = 1*x + 0*y + 0*z
* 第二行的值,影响着y坐标。比如0 1 0 => y = 0*x + 1*y + 0*z
* 第三行的值,影响着z坐标。比如 0 0 1 => z = 0*x + 0*y + 1*z
*
* 我们自己计算一个矩阵然后通过Matrax.setValues设置。
* 这样,在调用canvas的drawBitmap方法时,传入matrix
*
* Matrix类并不提倡我们使用这种方式来操作变换,Matrix针对不同的变换都相应的有pre,set,post三种方法
* 可以使用。
* pre是矩阵前乘
* set是直接设置
* post是矩阵后乘
*/
private Bitmap transferImage(){
Matrix matrix = new Matrix();
matrix.setValues(new float[]{
.5f,0,0,//这里只会影响到x轴,所以,图片的长度将是原来的一半
0,1,0,
0,0,1
});
return this.getDstImage(matrix);
}
/**
* 该方法中我们将对图像的颜色,亮度,对比度等进行设置
* 需要用到ColorMatrix类。ColorMatrix类是一个四行五列的矩阵
* 每一行影响着[R,G,B,A]中的一个
* -------------------------
* a1 b1 c1 d1 e1
* a2 b2 c2 d2 e2
* a3 b3 c3 d3 e3
* a4 b4 c4 d4 e4
* -------------------------
* Rnew => a1*R+b1*G+c1*B+d1*A+e1
* Gnew => a2*R+b2*G+c2*B+d2*A+e2
* Bnew => a3*R+b3*G+c3*B+d3*A+e3
* Gnew => a4*R+b4*G+c4*B+d4*A+e4
* 其中R,G,B的值是128,A的值是0
*
* 最后将颜色的修改,通过Paint.setColorFilter应用到Paint对象中。
* 主要对于ColorMatrix,需要将其包装成ColorMatrixColorFilter对象,再传给Paint对象
*
* 同样的,ColorMatrix提供给我们相应的方法,setSaturation()就可以设置一个饱和度
*/
private Bitmap ajustImage(){
ColorMatrix cMatrix = new ColorMatrix();
// int brightIndex = -25;
// int doubleColor = 2;
// cMatrix.set(new float[]{
// doubleColor,0,0,0,brightIndex, //这里将1改为2则我们让Red的值为原来的两倍
// 0,doubleColor,0,0,brightIndex,//改变最后一列的值,我们可以不改变RGB同道颜色的基础上,改变亮度
// 0,0,doubleColor,0,brightIndex,
// 0,0,0,doubleColor,0
// });
//cMatrix.setSaturation(2.0f);//设置饱和度
cMatrix.setScale(2.0f, 2.0f, 2.0f, 2.0f);//设置颜色同道色彩缩放
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));
return this.getDstImage(null, paint);
}
/**
* 图像的合成,可以通过在同一个Canvas中绘制两张图片。
* 只是在绘制第二章图片的时候,需要给Paint指定一个变幻模式TransferMode。
* 在Android中有一个XFermode所有的变幻模式都是这个类的子类
* 我们需要用到它的一个子类PorterDuffXfermode,关于这个类,其中用到PorterDuff类
* 这个类很简单,就包含一个Enum是Mode,其中定义了一组规则,这组规则就是如何将
* 一张图像和另一种图像进行合成
* 关于图像合成有四种模式,LIGHTEN,DRAKEN,MULTIPLY,SCREEN
*/
private Bitmap compositeImages(){
Bitmap bmp = null;
//下面这个Bitmap中创建的函数就可以创建一个空的Bitmap
bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
Paint paint = new Paint();
Canvas canvas = new Canvas(bmp);
//首先绘制第一张图片,很简单,就是和方法中getDstImage一样
canvas.drawBitmap(srcBitmap, 0, 0, paint);
//在绘制第二张图片的时候,我们需要指定一个Xfermode
//这里采用Multiply模式,这个模式是将两张图片的对应的点的像素相乘
//,再除以255,然后以新的像素来重新绘制显示合成后的图像
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
canvas.drawBitmap(dstBitmap, 0, 0, paint);
return bmp;
}
怎么存??????????????????????
private View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// 启动Gallery应用
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, FIRST_PIC);
}
};
private View.OnClickListener clickListener2 = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// 启动Gallery应用
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SECOND_PIC);
}
};
public boolean onCreateOptionsMenu(Menu menu){
// super.onCreateOptionsMenu(menu);
//MenuInflater menuInflater = new MenuInflater(this);
//menuInflater.inflate(R.layout.image, menu)
menu.add(0,1,1,"复制");
menu.add(0,2,2,"变换");
menu.add(0,3,3,"亮度");
menu.add(0,4,4,"合成");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
switch(id){
case 1:
//复制一个图像
if(srcBitmap != null){
dstBitmap = getDstImage(null);//这里没有变换
dstImageView.setImageBitmap(dstBitmap);
}
break;
case 2:
//对复制后的图像进行变换
if(srcBitmap != null){
dstBitmap = transferImage();
dstImageView.setImageBitmap(dstBitmap);
}
break;
case 3:
//改变图像的色彩
if(srcBitmap != null){
dstBitmap = ajustImage();
dstImageView.setImageBitmap(dstBitmap);
}
break;
case 4:
if(srcBitmap != null && dstBitmap != null){
dstBitmap = compositeImages();
dstImageView.setImageBitmap(dstBitmap);
}
break;
}
return true;
}
/**
* 为了创建一个图像的副本,我们可以在创建一个新的空的Bitmap,然后在这个Bitmap上绘制一个Bitmap
* 这个空的Bitmap应该和已存在的Bitmap具有相同的尺寸和颜色深度
*
* 然后我们需要一个Canvas对象,一个Canvas简单说,就是一个画布,存放Bitmap,在构造时,就可以传入Bitmap对象
* 同时,Canvas中定义了很多便捷的画图方法,方便我们绘制各种图形
* 接下来,如果我们需要处理颜色和对比度,我们需要一个Paint对象,通过Paint我们可以设置画笔的各种特性。
*
* 最后,我们调用Canvas的drawBitmap就可以将原Bitmap绘制在dstBitmap上了
*
*/
private Bitmap getDstImage(Matrix matrix){
Bitmap bmp = null;
//下面这个Bitmap中创建的函数就可以创建一个空的Bitmap
//返回的是一个可以改变的Bitmap对象,这样我们后面就可以对其进行变换和颜色调整等操作了
bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
//创建Canvas对象,
Canvas canvas = new Canvas(bmp);
//创建Paint对象,这里先不用
Paint paint = new Paint();
//在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了
if(matrix != null){
//如果matrix存在,则采用变换
canvas.drawBitmap(dstBitmap, matrix, paint);
}else{
canvas.drawBitmap(srcBitmap, 0, 0, paint);
}
return bmp;
}
/**
* 重载getDstImage函数,传入定制的Paint对象
* @param matrix
* @param paint
* @return
*/
private Bitmap getDstImage(Matrix matrix, Paint paint){
Bitmap bmp = null;
//下面这个Bitmap中创建的函数就可以创建一个空的Bitmap
//返回的是一个可以改变的Bitmap对象,这样我们后面就可以对其进行变换和颜色调整等操作了
bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
//创建Canvas对象,
Canvas canvas = new Canvas(bmp);
//在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了
if(matrix != null){
//如果matrix存在,则采用变换
canvas.drawBitmap(dstBitmap, matrix, paint);
}else{
canvas.drawBitmap(srcBitmap, 0, 0, paint);
}
return bmp;
}
/**
* 为了放大缩小、旋转图像,我们要使用Matrix类。Matrix类是一个三维矩阵。
* 在Android屏幕中,图像的每个像素对应都是一个坐标,一个坐标由x/y/z组成
* ------------------------
* cosX -sinX translateX
* sinX cosX translateY
* 0 0 scale
* ------------------------
* 第一行的值,影响着x坐标。比如 1 0 0 =>x = 1*x + 0*y + 0*z
* 第二行的值,影响着y坐标。比如0 1 0 => y = 0*x + 1*y + 0*z
* 第三行的值,影响着z坐标。比如 0 0 1 => z = 0*x + 0*y + 1*z
*
* 我们自己计算一个矩阵然后通过Matrax.setValues设置。
* 这样,在调用canvas的drawBitmap方法时,传入matrix
*
* Matrix类并不提倡我们使用这种方式来操作变换,Matrix针对不同的变换都相应的有pre,set,post三种方法
* 可以使用。
* pre是矩阵前乘
* set是直接设置
* post是矩阵后乘
*/
private Bitmap transferImage(){
Matrix matrix = new Matrix();
matrix.setValues(new float[]{
.5f,0,0,//这里只会影响到x轴,所以,图片的长度将是原来的一半
0,1,0,
0,0,1
});
return this.getDstImage(matrix);
}
/**
* 该方法中我们将对图像的颜色,亮度,对比度等进行设置
* 需要用到ColorMatrix类。ColorMatrix类是一个四行五列的矩阵
* 每一行影响着[R,G,B,A]中的一个
* -------------------------
* a1 b1 c1 d1 e1
* a2 b2 c2 d2 e2
* a3 b3 c3 d3 e3
* a4 b4 c4 d4 e4
* -------------------------
* Rnew => a1*R+b1*G+c1*B+d1*A+e1
* Gnew => a2*R+b2*G+c2*B+d2*A+e2
* Bnew => a3*R+b3*G+c3*B+d3*A+e3
* Gnew => a4*R+b4*G+c4*B+d4*A+e4
* 其中R,G,B的值是128,A的值是0
*
* 最后将颜色的修改,通过Paint.setColorFilter应用到Paint对象中。
* 主要对于ColorMatrix,需要将其包装成ColorMatrixColorFilter对象,再传给Paint对象
*
* 同样的,ColorMatrix提供给我们相应的方法,setSaturation()就可以设置一个饱和度
*/
private Bitmap ajustImage(){
ColorMatrix cMatrix = new ColorMatrix();
// int brightIndex = -25;
// int doubleColor = 2;
// cMatrix.set(new float[]{
// doubleColor,0,0,0,brightIndex, //这里将1改为2则我们让Red的值为原来的两倍
// 0,doubleColor,0,0,brightIndex,//改变最后一列的值,我们可以不改变RGB同道颜色的基础上,改变亮度
// 0,0,doubleColor,0,brightIndex,
// 0,0,0,doubleColor,0
// });
//cMatrix.setSaturation(2.0f);//设置饱和度
cMatrix.setScale(2.0f, 2.0f, 2.0f, 2.0f);//设置颜色同道色彩缩放
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));
return this.getDstImage(null, paint);
}
/**
* 图像的合成,可以通过在同一个Canvas中绘制两张图片。
* 只是在绘制第二章图片的时候,需要给Paint指定一个变幻模式TransferMode。
* 在Android中有一个XFermode所有的变幻模式都是这个类的子类
* 我们需要用到它的一个子类PorterDuffXfermode,关于这个类,其中用到PorterDuff类
* 这个类很简单,就包含一个Enum是Mode,其中定义了一组规则,这组规则就是如何将
* 一张图像和另一种图像进行合成
* 关于图像合成有四种模式,LIGHTEN,DRAKEN,MULTIPLY,SCREEN
*/
private Bitmap compositeImages(){
Bitmap bmp = null;
//下面这个Bitmap中创建的函数就可以创建一个空的Bitmap
bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
Paint paint = new Paint();
Canvas canvas = new Canvas(bmp);
//首先绘制第一张图片,很简单,就是和方法中getDstImage一样
canvas.drawBitmap(srcBitmap, 0, 0, paint);
//在绘制第二张图片的时候,我们需要指定一个Xfermode
//这里采用Multiply模式,这个模式是将两张图片的对应的点的像素相乘
//,再除以255,然后以新的像素来重新绘制显示合成后的图像
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
canvas.drawBitmap(dstBitmap, 0, 0, paint);
return bmp;
}
怎么存??????????????????????
然后使用File保存在sd卡
下面把bmp保存到sdcard的test.pngtry {
FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory()+ File.separator+ "test.png");
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}