1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android 调用图片裁剪功能 Android图片裁剪 合成(调用系统裁剪功能)

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

时间:2018-11-25 09:50:11

相关推荐

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

做照片相框

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

在之前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="/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" />

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。