Android中bitmap的相关处理
加载大图片 Options options=new Options(); options.inJustDecodeBounds=true;//不加载图片,只加载文件信息 //加载图片,获取到配置信息 BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/a.jpg",options); int width=options.outWidth;//获取到图片的真是宽度 int height=options.outHeight;//获取到图片的真是的高度 //获取到手机屏幕显示对象 Display display = getWindowManager().getDefaultDisplay(); int h = display.getHeight();//获取屏幕的高度 int w = display.getWidth();//获取到平局的宽度 //计算缩放的比率 int scale=1; int scaley=width/w; int scalex=height/h; if(scalex<1 && scaley<1){ }else{ //如果缩放的宽,大于高,则安照宽的缩放比率,因为高的缩放比率小,那么按照高的缩放比率的话,宽则超出了屏幕的宽 scale=scalex>scaley?scalex:scaley; } options.inJustDecodeBounds=true; options.inSampleSize=scale;//指定缩放比率 //根据配置信息,重新生成有一个bitmap对象,这就得到的是缩放之后的图片 Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/a.jpg",options); img.setImageBitmap(bitmap); 涂鸦 1、首先加载一个图片Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/a.jpg"); 2.创建一个空的图片,bit = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),bitmap.getConfig()); 3.创建一个画布,Canvas canvas=new Canvas(bit); 4.创建一个画笔 panint paint=new Paint(); paint.setcolor(Color.red); 5.把加载的图片画到画布上 canvas.drawBitmap(bitmap) 6.在图片上设置触摸事件,根据按下和移动的位置开始画线 加水印 和上面的一样,只是加了一句canvas.drawText("饿哦我到底发生", 100,100, paint);
保存图片通知相册扫描该图片 bit.compress(CompressFormat.JPEG, 100, new FileOutputStream(Environment.getExternalStorageDirectory()+"/hdksh.jpg")); Intent intent=new Intent(); intent.setAction("android.intent.action.MEDIA_MOUNTED"); intent.setData(Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"hdksh.jpg"))); sendBroadcast(intent);//发送广播,通知相册扫描
图片的旋转 Bitmap bitmap=BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/mm.jpg"); Bitmap bit = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); Canvas canvas=new Canvas(bit); Matrix matrix=new Matrix(); matrix.setRotate(180, bitmap.getWidth()/2, bitmap.getHeight()/2); Paint paint=new Paint(); paint.setColor(Color.RED); canvas.drawBitmap(bitmap, matrix, paint); img.setImageBitmap(bit)
镜面效果 Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/mm.jpg"); Bitmap bit = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); Canvas canvas=new Canvas(bit); Paint paint=new Paint(); Matrix matrix=new Matrix(); matrix.setScale(-1, 1);//反转 matrix.postTranslate(bitmap.getWidth(), 0);//移动 canvas.drawBitmap(bitmap, matrix, paint); img.setImageBitmap(bit);