android图片的处理

android中算图片的大小,是图片高*宽*4,4是代表这ARGB,A是透明度
android中缩放图片,是按着大的比例缩,
获取屏幕的大小,需要WindowManager,wm=getSystemService("");
disply=wm.getDafultDisplay();


// 根据图片大小缩放
public Bitmap getzoomBitmap() {
// 显示屏幕的高度
WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point outSize = new Point();
display.getSize(outSize);
width = outSize.x;// 宽度
hegiht = outSize.y;// 屏幕适应的高度
// 设置缩放的比例
System.out.println("屏幕宽高" + width + "   " + hegiht);
Options opts = new Options();
opts.inJustDecodeBounds = true;// 读入边框信息
// 设置不加载图片。只加载图片显示的信息,
BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
+ "/ceshi.png", opts);// 先读取信息
int beishu = 1;
int beix = opts.outWidth / width;// 图片放大的比例,
int beiy = opts.outHeight / hegiht;// 谁高用谁的
if (beix > beiy && beix > 1) {
beishu = beix;
}
if (beiy > beix && beiy > 1) {
beishu = beiy;
}
System.out.println("缩放的比例是" + beishu);
opts.inSampleSize = beishu;
opts.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(
Environment.getExternalStorageDirectory() + "/ceshi.png", opts);
return bitmap;


}


要想改动图片,必须复制
图片的复制,
不能用Bitmap crb=Bitmap.createBitmap(bm);//不能用这个,这个得到的还是原图,
src = (ImageView) findViewById(R.id.iv_src);
copy = (ImageView) findViewById(R.id.iv_copy);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.tomcat);// 将图片解析出来
src.setImageBitmap(bm);
// Bitmap crb=Bitmap.createBitmap(bm);//不能用这个,这个得到的还是原图,
Bitmap crb = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(),
bm.getConfig());// 创建一个宽高一样的模板
Canvas canvas = new Canvas(crb);//新建一个画布


Paint paint = new Paint();
Matrix matrix = new Matrix();
canvas.drawBitmap(bm, matrix, paint);//根据原版的往上画
// paint.setColor(Color.RED);


// canvas.drawLine(0, 0, crb.getWidth() * 2, 2 * crb.getHeight(), paint);
for (int i = 0; i < 5; i++)
crb.setPixel(crb.getWidth() - 10 + i, crb.getHeight() - 10,
Color.RED);//直接在Bitmap上画


copy.setImageBitmap(crb);
图片的旋转,平移, 需要用的是matrix中的方法,
旋转matrix.setRotate(degree, copy.getWidth() / 2,copy.getHeight() / 2);//degree偏移量,后两个是,围着旋转的中心点
平移 以及 翻转
// 设置翻转图片
public Bitmap getFanzhuang(Bitmap bitmap) {
Bitmap copy = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), bitmap.getConfig());
Canvas canvas = new Canvas(copy);
Matrix matrix = new Matrix();
Paint paint = new Paint();
// matrix.setTranslate(30, 30);//在原图片的基础上平移30 30
matrix.setScale(0.5f, 0.5f);// 缩放一半
canvas.drawBitmap(bitmap, matrix, paint);
return copy;


}
图片的设置倒影
// 设置倒影
public Bitmap getDaoYing(Bitmap bitmap) {
Bitmap copy = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), bitmap.getConfig());
Canvas canvas = new Canvas(copy);// 一个画布
Matrix matrix = new Matrix();// 设一个几行几列的矩阵
Paint paint = new Paint();// 画笔
matrix.setScale(1.0f, -1.0f);// 设置
matrix.postTranslate(0, bitmap.getHeight());// post是接着上次继续操作,,set更新图片,覆盖掉
canvas.drawBitmap(bitmap, matrix, paint);
return copy;
}
设置照镜子
// 照镜子
public Bitmap getzhaojing(Bitmap bt){
Bitmap copy = Bitmap.createBitmap(bt.getWidth(), bt.getHeight(), bt.getConfig());
Canvas canvas=new Canvas(copy);//设置画布
Matrix matrix=new Matrix();//设置矩阵
Paint paint=new Paint();//设置画笔
matrix.setScale(-1.0f, 1.0f);//将矩阵设置为镜子
matrix.postTranslate(bt.getWidth(), 0);
canvas.drawBitmap(bt, matrix, paint);
return copy;
}
设置圆角图片,原理就是,先在画布上画一个圆角的矩形,之后再画一个Bitmap,然后就设置为显示重叠部分的上层。
// 设置圆角图片
public Bitmap getRoudjiao(Bitmap bitmap, float pain) {
Bitmap round = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), bitmap.getConfig());
Canvas canvas = new Canvas(round);
// canvas.drawARGB(0, 0, 0, 0);//画布背景设为黑色
Paint paint = new Paint();
// paint.setColor(0xff424242);
paint.setAntiAlias(true);// 设置边界是非锯齿形
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF rectf = new RectF(rect);
canvas.drawRoundRect(rectf, pain, pain, paint);// 画一个圆角矩形框
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 下层上层重合,显示上层
canvas.drawBitmap(bitmap, rect, rect, paint);
return round;


}
保存图片
try {
File file = new File(Environment.getExternalStorageDirectory(),
"yuan.png");
OutputStream fos = new FileOutputStream(file);
copy.compress(CompressFormat.PNG, 100, fos);
fos.close();
Toast.makeText(this, "保存成功!", 0).show();
} catch (Exception e) {
// TODO: handle exception
}
设置透明
copy.setPixel((int) event.getX() + i,
(int) event.getY() + j, Color.TRANSPARENT);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值