android多媒体部分学习笔记三___图像合成

/**
* 图像合成
*
* @time 下午06:21:22
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class ChosePictureCompositeActivity extends Activity implements OnClickListener {
public static final int PICKED_ONE = 0;
public static final int PICKED_TWO = 1;


private Button btn_choose_picture1;
private Button btn_choose_picture2;
private ImageView img_choose_picture;


private Bitmap bitmap1, bitmap2;
private Options bmpOptions;
private Canvas canvas;
private Paint paint;
private boolean onePicked = false;
private boolean twoPicked = false;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chose_picture_composite);
findView();


}


private void findView() {
btn_choose_picture1 = (Button) this.findViewById(R.id.btn_choose_picture_composite1);
btn_choose_picture1.setOnClickListener(this);
btn_choose_picture2 = (Button) this.findViewById(R.id.btn_choose_picture_composite2);
btn_choose_picture2.setOnClickListener(this);


img_choose_picture = (ImageView) this.findViewById(R.id.img_choose_picture_composite);
}


@Override
public void onClick(View v) {
int witch = -1;
if (v == btn_choose_picture1) {
witch = PICKED_ONE;
} else if (v == btn_choose_picture2) {
witch = PICKED_TWO;
}
Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, witch);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);


if (resultCode == RESULT_OK) {
Uri imageFileUri = data.getData();


if (requestCode == PICKED_ONE) {
bitmap1 = loadBitmap(imageFileUri);
onePicked = true;
} else if (requestCode == PICKED_TWO) {
bitmap2 = loadBitmap(imageFileUri);
twoPicked = true;
}


if (onePicked && twoPicked) {
Log.i("tag", "=====choose is over!");
Bitmap drawingBitmap = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig());
canvas = new Canvas(drawingBitmap);
paint = new Paint();
canvas.drawBitmap(bitmap1, 0, 0, paint);


// 图像重叠的绘制规则 PorterDuffXfermode
// 常量:
// SRC 只绘制原图像:


// DST 只绘制目标图像:
// 另外和四个规则,在一幅图像上放置另一幅图像,如何合成
// LIGHT: 获得每个位置上两幅图像中最亮的像素并显示
// DARKEN:最暗的像素并显示
// MULTIPLY:将第个位置的像素相乘,除以255,创建一个新像素显示
// SCREEN: 反转每个颜色,执行如(MULTIPLY)常量的操作
paint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
canvas.drawBitmap(bitmap2, 0, 0, paint);


img_choose_picture.setImageBitmap(drawingBitmap);
}
}


}


/**
* 加载位图图像
*/
private Bitmap loadBitmap(Uri imageFileUri) {


// 取得屏幕大小
Display display = getWindowManager().getDefaultDisplay();
int dw = display.getWidth();
int dh = display.getHeight();
// 期望是ARGB_4444
Bitmap bitmap = Bitmap.createBitmap((int) dw, (int) dh, Bitmap.Config.ARGB_4444);


try {
// 加载图像的尺寸而不是图像本身
bmpOptions = new BitmapFactory.Options();
// 如果为true,只须返回图像的范围,不须尝试解码图像本身
bmpOptions.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpOptions);


int hRatio = (int) Math.ceil(bmpOptions.outHeight / (float) dh);
int wRatio = (int) Math.ceil(bmpOptions.outWidth / (float) dw);


Log.i("tag", "hRatio" + hRatio);
Log.i("tag", "wRatio" + wRatio);
// 如果两个比率都大于1
// 那么图像的一条边将大小屏幕
if (hRatio > 1 && wRatio > 1) {
if (hRatio > wRatio) {
Log.i("tag", "hRatio" + hRatio);
// 若高度比率更大,则根据它缩放
bmpOptions.inSampleSize = hRatio;
} else {
Log.i("tag", "wRatio" + wRatio);
// 反之则根据宽度缩放
bmpOptions.inSampleSize = wRatio;
}
}
// 对图像进行解码
bmpOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpOptions);


} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap;

}

}

可生成圆形、方形、及方形的组合头像。项目地址:https://github.com/Pedroafa/avatar-android 效果图:如何使用:首先创建个ImageView<ImageView             android:id="@ id/roundedAvatar"             android:layout_height="fill_parent"             android:layout_width="fill_parent"/>2. //通过AvatarDrawableFactory生成各种形状的Drawable AvatarDrawableFactory avatarDrawableFactory = new AvatarDrawableFactory(getResources()); BitmapFactory.Options options = new BitmapFactory.Options(); options.inMutable = false; Bitmap avatar = BitmapFactory.decodeResource(getResources(), R.drawable.avatar, options); //圆形的 Drawable roundedAvatarDrawable = avatarDrawableFactory.getRoundedAvatarDrawable(avatar);//生成圆形的Drawable ImageView roundedAvatarView = (ImageView)rootView.findViewById(R.id.roundedAvatar); roundedAvatarView.setImageDrawable(roundedAvatarDrawable);其他形状的://圆形的且带边框的 Drawable borderedRoundedAvatarDrawable = avatarDrawableFactory.getBorderedRoundedAvatarDrawable(avatar); ImageView borderedRoundedAvatarView = (ImageView)rootView.findViewById(R.id.borderedRoundedAvatar); borderedRoundedAvatarView.setImageDrawable(borderedRoundedAvatarDrawable); //方形的 Drawable squaredAvatarDrawable = avatarDrawableFactory.getSquaredAvatarDrawable(avatar); ImageView squaredAvatarView = (ImageView)rootView.findViewById(R.id.squaredAvatar); squaredAvatarView.setImageDrawable(squaredAvatarDrawable); //俩个方形的组合头像 Drawable doubleSquaredAvatarDrawable = avatarDrawableFactory.getSquaredAvatarDrawable(avatar, avatar); ImageView doubleSquaredAvatarView = (ImageView)rootView.findViewById(R.id.doubleSquaredAvatar); doubleSquaredAvatarView.setImageDrawable(doubleSquaredAvatarDrawable); //个方形的组合头像 Drawable tripleSquaredAvatarDrawable = avatarDrawableFactory.getSquaredAvatarDrawable(avatar, avatar, avatar); ImageView tripleSquaredAvatarView = (ImageView)rootView.findViewById(R.id.tripleSquaredAvatar); tripleSquaredAvatarView.setImageDrawable(tripleSquaredAvatarDrawable); //四个方形的组合头像 Drawable quadrupleSquaredAvatarDrawable = avatarDrawableFactory                     .getSquaredAvatarDrawable(avatar, avatar, avatar, avatar); ImageView quadrupleSquaredAvatarView = (ImageView)rootView.findViewById(R.id.quadrupleSquaredAvatar); quadrupleSquaredAvatarView.setImageDrawable(quadrupleSquaredAvatarDrawable);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值