ビットマップ13の使用法:
01.ドローアブルの使い方
リソースresources = mContext.getResources(); 描画可能な描画可能 = resources.getDrawable(R.drawable.a)。 imageview.setBackground(drawable);
02. BitmapDrawableビットマップの使用法
リソースr = this .getContext()。getResources(); 入力ストリームは = r.openRawResource(R.drawable.my_background_image);です。 BitmapDrawable bmpDraw = new BitmapDrawable(is); ビットマップbmp = bmpDraw.getBitmap();
03.Bitmap –> BitmapFactory.decodeResource
ビットマップbmp = BitmapFactory.decodeResource(r、R.drawable.icon);
ビットマップnewb = Bitmap.createBitmap(300、300、Config.ARGB_8888);
04.Bitmap –> BitmapFactory.decodeStream
InputStream is = getResources()。openRawResource(R.drawable.icon);
ビットマップmBitmap = BitmapFactory.decodeStream(is);
05.Drawable→ビットマップ
公共の 静的なビットマップdrawableToBitmap(Drawableの描画可能){ ビットマップビットマップ = ビットマップ .createBitmap( drawable.getIntrinsicWidth()、 drawable.getIntrinsicHeight()、 drawable.getOpacity() = PixelFormat.OPAQUE!?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565 ); Canvas canvas = new Canvas(bitmap); // canvas.setBitmap(bitmap); drawable.setBounds(0、0、drawable.getIntrinsicWidth()、 drawable.getIntrinsicHeight()); drawable.draw(canvas); ビットマップを返す; }
06.リソースからビットマップを取得する
リソースres = getResources();
ビットマップbmp = BitmapFactory.decodeResource(res、R.drawable.pic);
07.ビットマップ→byte []
プライベート バイト[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG、 100 、baos); return baos.toByteArray(); }
08.byte []→ビットマップ
プライベートビットマップBytes2Bimap(バイト[] b){ if(b.length!= 0 ){ return BitmapFactory.decodeByteArray(b、0 、b.length); } else { nullを返す ; } }
09.ビットマップを保存
static boolean saveBitmap2file(Bitmap bmp、String filename){ CompressFormat format = Bitmap.CompressFormat.JPEG; int quality = 100 ; OutputStream stream = null ; { stream = new FileOutputStream( "/ sdcard /" + filename);を試してください。 } catch ( FileNotFoundException e){ // TODO自動生成catchブロックFoxit PDF Creatorによって生成されます©Foxit Software http://www.foxitsoftware.com 評価のみ。e.printStackTrace(); } 戻る bmp.compress(フォーマット、品質、ストリーム); }
10.要件に応じて画像をズームします
ビットマップbm = BitmapFactory.decodeStream(getResources(). openRawResource(R.drawable.dog)); // 画像の幅と高さを取得 int width = bm.getWidth(); int height = bm.getHeight(); // 思考を設定する必要なサイズ int newWidth = 320 ; int newHeight = 480 ; // スケーリング比率を計算します float scaleWidth =((float)newWidth)/ width; float scaleHeight =((float)newHeight)/ height; //スケーリングする行列を取得しますパラメータ マトリックスマトリックス= 新しいマトリックス(); matrix.postScale(scaleWidth、scaleHeight); // 新しい画像を取得します Bitmap newbm = Bitmap.createBitmap(bm、0、0、width、height、matrix、 true ); // キャンバスに 配置します canvas.drawBitmap(newbm、0、0、paint);
11.ビットマップへのファイル画像
ビットマップbt = BitmapFactory.decodeFile( "/ sdcard / myImage /" + "head.jpg"); // 画像アドレス
12.画像からビットマップ
public Bitmap drawableToBitamp(int drawableResource){ // 可生の面の资源 BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.RGB_565; opt.inPurgeable = true ; opt.inInputShareable = true ; InputStream is = this .getResources()。openRawResource(drawableResource); BitmapFactory.decodeStream(is、null 、opt); BitmapFactory.decodeStream(is、null }を返します、opt);をます。。
13.ビットマップの使用法のまとめ
BitmapFactory.Options option = new BitmapFactory.Options(); option.inSampleSize = 2; // メモリのオーバーフローを防ぐために画像を元の幅と高さの1/2に設定します Bitmap bm = BitmapFactory.decodeFile( ""、option); // ファイルストリーム URL url = 新しい URL( "" ); InputStream is = url.openStream(); ビットマップbm = BitmapFactory.decodeStream(is); android:scaleType: android:scaleType // 画像のサイズ変更/移動に合わせて制御するImageViewサイズ。ImageView.ScaleType /