android 调用图片裁剪功能,Android图片裁剪,合成(调用系统裁剪功能)

这篇博客介绍了如何在Android应用中实现添加照片相框的功能。用户首先选择图片进行裁剪,然后可以添加预设的相框。通过Bitmap和Canvas的组合使用,利用PorterDuff.Mode.MULTIPLY模式将裁剪后的图片与相框图片融合,展示在ImageView上。代码示例详细展示了选择图片、裁剪图片和添加相框的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做照片相框

步骤就是跟美图秀秀一样,先截图,然后添加相框

在之前Bitmap.createBitmap那个文里,说过使用jpg图片会挡住底层图片,用画这种方法合成图片,没有问题。

public class MainActivity extends Activity implements OnClickListener{

private static final String TAG = "blueweb";

private LinearLayout layout_btnGroup;

private int paintY;// layout_btnGroup高度

private Button selectImageBtn;

private Button cutImageBtn;

private Button btn_ouerlay;

private ImageView imageView;

private Bitmap cutBmp;// 裁剪得到的Bitmap

private Bitmap chooseBmp;// 选择相框的Bitmap

private Canvas canvas;

private Paint paint;

private boolean onePicked = false;

private boolean twoPicked = false;

private static final int  IMAGE_SELECT = 1;

private static final int  IMAGE_CUT = 2;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

layout_btnGroup = (LinearLayout) findViewById(R.id.layout_btnGroup);

selectImageBtn = (Button) findViewById(R.id.btn_chooseimg);

cutImageBtn = (Button) findViewById(R.id.btn_cutimg);

btn_ouerlay = (Button) findViewById(R.id.btn_addoverlay);

imageView = (ImageView) findViewById(R.id.image);

selectImageBtn.setOnClickListener(this);

cutImageBtn.setOnClickListener(this);

btn_ouerlay.setOnClickListener(this);

paintY = layout_btnGroup.getHeight();

}

public void onClick(View v) {

Intent intent;

switch(v.getId()){

case R.id.btn_chooseimg:

intent = new Intent(Intent.ACTION_PICK,

android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(intent, IMAGE_SELECT);

break;

case R.id.btn_cutimg:

intent = getImageClipIntent();

startActivityForResult(intent, IMAGE_CUT);

break;

case R.id.btn_addoverlay:

Toast.makeText(this, "点击了Btn", Toast.LENGTH_SHORT).show();

chooseBmp = BitmapFactory.decodeResource(getResources(), R.drawable.img_overlay);

if (chooseBmp != null){

twoPicked = true;

if (onePicked == true && twoPicked == true){

Bitmap drawingBitmap = Bitmap.createBitmap(cutBmp.getWidth(), cutBmp.getHeight(), cutBmp.getConfig());

canvas = new Canvas(drawingBitmap);

paint = new Paint();

canvas.drawBitmap(cutBmp, 0, paintY, paint);

paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));

canvas.drawBitmap(chooseBmp, 0, paintY, paint);

Log.d(TAG, paintY + "");

imageView.setImageBitmap(drawingBitmap);

}

}else {

Toast.makeText(this, "添加相框失败", Toast.LENGTH_SHORT).show();

}

break;

}

}

/**

* 获取剪切后的图片

*/

public static Intent getImageClipIntent() {

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);

intent.setType("image/*");

intent.putExtra("crop", "true");

intent.putExtra("aspectX", 1);//裁剪框比例

intent.putExtra("aspectY", 1);

intent.putExtra("outputX", 200);//输出图片大小

intent.putExtra("outputY", 300);

intent.putExtra("return-data", true);

return intent;

}

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

if(resultCode == RESULT_OK){

if(requestCode == IMAGE_SELECT){

Uri imageFileUri = intent.getData();

int dw=getWindowManager().getDefaultDisplay().getWidth();

int dh=getWindowManager().getDefaultDisplay().getHeight()/2;

//已屏幕宽 和一般的高作为图片显示的最大尺寸

try{

BitmapFactory.Options factory = new BitmapFactory.Options();

factory.inJustDecodeBounds = true; //当为true时  允许查询图片不为 图片像素分配内存

cutBmp = BitmapFactory.decodeStream(getContentResolver()

.openInputStream(imageFileUri),null,factory);

int hRatio = (int)Math.ceil(factory.outHeight/(float)dh); //图片是高度的几倍

int wRatio = (int)Math.ceil(factory.outWidth/(float)dw); //图片是宽度的几倍

System.out.println("hRatio:"+hRatio+"  wRatio:"+wRatio);

//缩小到  1/ratio的尺寸和 1/ratio^2的像素

if(hRatio>1 || wRatio>1){

if(hRatio > wRatio){

factory.inSampleSize = hRatio;

}

else

factory.inSampleSize = wRatio;

}

factory.inJustDecodeBounds = false;

cutBmp = BitmapFactory.decodeStream(getContentResolver()

.openInputStream(imageFileUri),null,factory);

if (cutBmp != null){

onePicked = true;

imageView.setImageBitmap(cutBmp);

}else {

Toast.makeText(this, "选择图片失败", Toast.LENGTH_SHORT).show();

}

}catch(Exception ex){

}

}

else if(requestCode == IMAGE_CUT){

cutBmp = intent.getParcelableExtra("data");

if (cutBmp != null){

onePicked = true;

imageView.setImageBitmap(cutBmp);

}else {

Toast.makeText(this, "获取裁剪图片失败", Toast.LENGTH_SHORT).show();

}

}

}

}

}

布局文件

xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

tools:context=".MainActivity" >

android:id="@+id/layout_btnGroup"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

android:id="@+id/btn_chooseimg"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="选择图片" />

android:id="@+id/btn_cutimg"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="选择图片裁剪" />

android:id="@+id/btn_addoverlay"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="添加相框" />

android:layout_weight="1"

android:id="@+id/image"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher" />

先点击选择图片裁剪,然后点击添加相框。效果如下,只是做了基本的原理,以后有时间会改一下代码再更新

1359535288_2544.png

1359535301_2169.png

1359535354_6099.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值