android bitmap的 一些简单操作

浏览:1263 ℃
字体:
发布时间:2013-12-17 09:37:34
来源:

全都是一些代码片段,需要可以直接贴过去用


			/** 获取 drawable 的图片   可以循环   1.图名    2.drawable 3.包名      **/						int imgid = getResources().getIdentifier("ic_launcher", "drawable", "com.example.anywight");			text.setBackgroundResource(imgid);									/** 通过图片id获得Bitmap  **/			Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);									/** 通过 assest 获取       获得Drawable    bitmap **/			InputStream in = this.getAssets().open("ic_launcher");			Drawable da = Drawable.createFromStream(in, null);			Bitmap mm = BitmapFactory.decodeStream(in);							/** 通过 sdcard  获得   bitmap **/			Bitmap bit = BitmapFactory.decodeFile("/sdcard/android.jpg");				



	/** view转Bitmap **/	public static Bitmap convertViewToBitmap(View view, int bitmapWidth, int bitmapHeight){				Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);		view.draw(new Canvas(bitmap));		return bitmap;	}	/** 将控件转换为bitmap **/	public static Bitmap convertViewToBitMap(View view){		// 打开图像缓存		view.setDrawingCacheEnabled(true);		// 必须调用measure和layout方法才能成功保存可视组件的截图到png图像文件		// 测量View大小		view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));		// 发送位置和尺寸到View及其所有的子View		view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());		// 获得可视组件的截图		Bitmap bitmap = view.getDrawingCache();		return bitmap;	}			public static Bitmap getBitmapFromView(View view){		Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);		Canvas canvas = new Canvas(returnedBitmap);		Drawable bgDrawable = view.getBackground();		if (bgDrawable != null)			bgDrawable.draw(canvas);		else			canvas.drawColor(Color.WHITE);		view.draw(canvas);		return returnedBitmap;	}		/**  获取屏幕截图的bitmap对象的代码如下  **/	public Bitmap getScreenPic(View view){				View rootView = view.getRootView();		rootView.setDrawingCacheEnabled(true);		rootView.buildDrawingCache();		// 不明白为什么这里返回一个空,有帖子说不能在oncreat方法中调用		// 测量View大小		rootView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));		// 发送位置和尺寸到View及其所有的子View		rootView.layout(0, 0, rootView.getMeasuredWidth(), rootView.getMeasuredHeight());		// 解决措施,调用上面的measure和layout方法之后,返回值就不再为空		// 如果想要创建的是固定长度和宽度的呢?		Bitmap bitmap = rootView.getDrawingCache();		rootView.destroyDrawingCache();		return bitmap;	}		/** Drawable → Bitmap **/	public static Bitmap drawableToBitmap(Drawable drawable){				Bitmap bitmap = Bitmap.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);		return bitmap;	}			/** bitmap → drawable **/	public static Drawable bitmapToDrawable(Context context,String filename){				 Bitmap image = null;  	     BitmapDrawable ddd = null;  	        try {  	            AssetManager am = context.getAssets();  	            InputStream is = am.open(filename);  	            image = BitmapFactory.decodeStream(is);  	            ddd = new BitmapDrawable(context.getResources(), image);  	            is.close();  	        } catch (Exception e) {  	        }  	     return ddd;  	}			/** byte[] → Bitmap **/	public static Bitmap byteToDrawable(Context context,byte[] bb){	     Bitmap pp  = BitmapFactory.decodeByteArray(bb, 0, bb.length);	     return pp;	}			/**  Bitmap → byte[]**/	public static byte[] bitmapToByte(Bitmap bitmap){				ByteArrayOutputStream baos = new ByteArrayOutputStream();  		bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);  	    byte[] yy = baos.toByteArray();	    return yy;	}		/**  将text  转换成  bitmap  **/	public static Bitmap createTxtImage(String txt, int txtSize) {        Bitmap mbmpTest = Bitmap.createBitmap(txt.length() * txtSize + 4,                txtSize + 4, Config.ARGB_8888);        Canvas canvasTemp = new Canvas(mbmpTest);        Paint p = new Paint();        p.setAntiAlias(true);        p.setColor(Color.WHITE);        p.setTextSize(txtSize);        canvasTemp.drawText(txt, 2, txtSize - 2, p);        return mbmpTest;    }		/**  显示将bitmap进行缩放       **/	public Bitmap  bitmapScanel(Context context){		//通过openRawResource获取一个inputStream对象  	    InputStream inputStream = context.getResources().openRawResource(R.id.backageground);  	    //通过一个InputStream创建一个BitmapDrawable对象  	    BitmapDrawable drawable = new BitmapDrawable(inputStream);  	    //通过BitmapDrawable对象获得Bitmap对象  	    Bitmap bitmap = drawable.getBitmap();  	    //利用Bitmap对象创建缩略图  	    bitmap = ThumbnailUtils.extractThumbnail(bitmap, 40, 40);  	    return bitmap;   		 }		  /**  放大缩小图片    **/      public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){          int width = bitmap.getWidth();          int height = bitmap.getHeight();          Matrix matrix = new Matrix();          float scaleWidht = ((float)w / width);          float scaleHeight = ((float)h / height);          matrix.postScale(scaleWidht, scaleHeight);          Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);          return newbmp;      }  		        /** 获得圆角图片的方法  **/      public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){                    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap                  .getHeight(), Config.ARGB_8888);          Canvas canvas = new Canvas(output);             final int color = 0xff424242;          final Paint paint = new Paint();          final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());          final RectF rectF = new RectF(rect);             paint.setAntiAlias(true);          canvas.drawARGB(0, 0, 0, 0);          paint.setColor(color);          canvas.drawRoundRect(rectF, roundPx, roundPx, paint);             paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));          canvas.drawBitmap(bitmap, rect, rect, paint);             return output;      }          	/** 对 bitmap 进行裁剪     **/	public Bitmap  bitmapClip(Context context , int id , int x , int y){		 Bitmap map = BitmapFactory.decodeResource(context.getResources(), id);		 map = Bitmap.createBitmap(map, x, y, 120, 120);	     return map;	}			/**	 * 图片的倒影效果	 */	public static Bitmap createReflectedImage(Bitmap originalImage) {        final int reflectionGap = 4;        int width = originalImage.getWidth();        int height = originalImage.getHeight();        Matrix matrix = new Matrix();        matrix.preScale(1, -1);        // Create a Bitmap with the flip matrix applied to it.        // We only want the bottom half of the image        Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,                height / 2, width, height / 2, matrix, false);        // Create a new bitmap with same width but taller to fit reflection        Bitmap bitmapWithReflection = Bitmap.createBitmap(width,                (height + height / 2), Config.ARGB_8888);        // Create a new Canvas with the bitmap that's big enough for        // the image plus gap plus reflection        Canvas canvas = new Canvas(bitmapWithReflection);        // Draw in the original image        canvas.drawBitmap(originalImage, 0, 0, null);        // Draw in the gap        Paint defaultPaint = new Paint();        canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);        // Draw in the reflection        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);        // Create a shader that is a linear gradient that covers the reflection        Paint paint = new Paint();        LinearGradient shader = new LinearGradient(0,                originalImage.getHeight(), 0, bitmapWithReflection.getHeight()                        + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);        // Set the paint to use this shader (linear gradient)        paint.setShader(shader);        // Set the Transfer mode to be porter duff and destination in        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));        // Draw a rectangle using the paint with our linear gradient        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);        return bitmapWithReflection;     }		




闂傚倸鍊搁崐鐑芥嚄閸洏鈧焦绻濋崟顓狀槱婵炴潙鍚嬪ḿ娆戝閸ф鈷戞い鎺嗗亾缂佸顕划濠氬冀椤撶喓鍘搁梺鎼炲劗閺呮稖鈪撮梻浣呵归鍡涘箰閹间礁鐓″璺猴功閺嗭箓鏌涢妷銏℃珖闁绘稏鍎崇槐鎾诲磼濞嗘帩鍞归梺閫炲苯澧柛鐔锋健椤㈡棃顢橀悩顐壕閻熸瑥瀚粈鍐磼鐠囪尙澧︽鐐插暣閸╋繝宕ㄩ姘导婵$偑鍊栭幐鐐叏閾忣偆顩查柕蹇曞Л閺€浠嬫煟閹存繃宸濋柛鎺斿缁绘盯寮堕幋婵愪純濡ょ姷鍋涚换姗€寮幘缁樻櫢闁跨噦鎷�/QQ 1602007闂傚倸鍊搁崐鐑芥倿閿旈敮鍋撶粭娑樻噽閻瑩鏌熸潏楣冩闁搞倖鍔栭妵鍕冀椤愵澀绮堕梺娲诲幗椤ㄥ牓骞夌粙娆惧悑濠㈣泛锕﹂崢娲⒑閸濆嫬鏆欓柣妤€妫欓崕顐︽⒒娴g懓顕滅紒璇插€垮畷鎰板川婵犲嫷娲搁梺璇″瀻閳ь剟寮ㄦ禒瀣厽婵☆垵娉曠花鍧楁煕韫囨挻婀伴柕鍥у楠炲洭顢旈崱娆戝幆闂備礁鎼惌澶屾崲濠靛棛鏆﹂柟鐑樺灍閸亪骞栧ǎ顒€鐏柡鍡欏仜閳规垿鎮欏顔兼婵犳鍠楅幐鎶藉箖瑜旈獮妯兼嫚閼碱剛鏆伴梻浣告啞濞诧箓宕归柆宥呯;闁告洦鍨遍悡锝夌叓閸ャ劍灏伴悹鎰剁節瀵偊宕奸妷锔规嫽婵炴挻鍩冮崑鎾寸箾娴e啿鍚樺☉妯滄棃宕担瑙勬珗闂備礁鎲℃笟妤呭窗濮樿翰鈧懘寮婚妷锔惧幐闂佹悶鍎弲娑㈠几閺冨牊鐓冮柣鐔稿鏁堥梺鍝勮嫰缁夎淇婇悜钘壩ㄩ柕澶涢檮閻︼綁鏌i悢鍝ョ煁婵犮垺锚铻炴俊銈呮噺缁犳帡姊绘担鍛靛湱鎹㈤幒妤€鐓曢柛顐g妇閺嬫棃鏌¢崘銊モ偓褰掑窗閹扮増鍋ㄦい鏍电到閺嬬喖鎮楀鐐
>更多相关文章
24小时热门资讯
24小时回复排行
闂傚倸鍊峰ù鍥敋瑜忛埀顒佺▓閺呮粎绮悢鑲烘棃鍩€椤掑喚鏆伴梺璇插閸愬骞忛敓锟� 闂傚倸鍊搁崐椋庣矆娴h櫣绀婂┑鐘插€寸紓姘辨喐閹达箑桅闁规壆澧楅崐鐑芥煟閵忋垺鏆╅柣锝呯埣濮婃椽妫冨☉姘辩杽闂佺ǹ锕ュú鏍亱濠碘槅鍨甸崑鎰涢娑栦簻闁规崘娉涙禒锕傛煕閿濆懏鎲哥紒杈ㄥ浮椤㈡瑩鎳為妷銉э紦闂備礁鎼惌澶岀礊娓氣偓瀹曟椽鏁嶉崟銊ヤ壕闁挎繂楠告禍婊勩亜韫囥儲瀚�
闂傚倸鍊风粈渚€骞夐垾鎰佹綎濠电姵鑹剧壕濠氭煟閺冨倸甯剁紒鐘崇墪閳规垿鎮╅幓鎺撴濡炪倖娲濇ご鎼佸箞閵娿儺娼ㄩ柛鈩冾殔椤亪姊洪崫銉ヮ€撻柟鍑ゆ嫹 闂傚倷娴囬褏鈧稈鏅犲畷妯荤節濮橆厸鎸冮梺鍛婃处閸忔稓鎹㈤崱娆愬枑闁哄倹顑欓崵鏇㈡煟閵忕姴顥忛柡浣哥У閹便劌顫滈崱妤€顫梺鍛娚戠敮妤冩崲濞戙垹绠i柣鎰綑闂夊秶绱撴笟鍥ф灈缂佸鎳撻锝嗙節濮橆厼浜滈梺绋跨箳閸樠冣枔韫囨稒鈷戦柛婵嗗閺嗘瑩鏌涙繝鍌涜础缂侇喖鐗撻弫鎾绘晸閿燂拷
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2025 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();