public class ViewToBitmap { private View mView; private static ViewToBitmap mViewToBitmap; private static int height =0; private static final int HEIGTH_COEFFICIENT = 2;//高度系数 private static int alpha = 0; private static int ALPHA_COEFFICIENT = 10;//Alpha系数 private Context mContext; private LayoutInflater mLayoutInflater; private ViewToBitmap(Context context , int layoutID){ this.mContext = context; mLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mView = mLayoutInflater.inflate(layoutID, null); } public static ViewToBitmap getInstance(Context context , int layoutID ){ if(mViewToBitmap == null){ mViewToBitmap = new ViewToBitmap(context, layoutID); } return mViewToBitmap; } public static ViewToBitmap getInstance() throws Exception{ if(mViewToBitmap == null){ throw new Exception("ViewToBitmap is null !"); } return mViewToBitmap; } public int getHeight(){ height = height + HEIGTH_COEFFICIENT; return height; } public void initHeight(){ height = 0; } public int getAlpah(){ alpha = alpha +ALPHA_COEFFICIENT; return alpha; } public void initAlpah(){ alpha = 0; } public void setContent(int contentID , String content){ // mView = mLayoutInflater.inflate(R.layout.appwidget_textview_anim, null); TextView contentStr = (TextView)mView.findViewById(contentID); contentStr.setText(content); } public Bitmap getViewBitmap(int w, int h){ mView.setDrawingCacheEnabled(true); mView.measure( MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); mView.layout(0, 0, w, h); mView.buildDrawingCache(); return mView.getDrawingCache(); } public static Bitmap setAlpha(Bitmap sourceImg, int number) { int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()]; sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0,sourceImg.getWidth(), sourceImg.getHeight()); // 获得图片的ARGB值 number = number * 255 / 100; for (int i = 0; i < argb.length; i++) { if(argb[i]!=0){ argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF); } else{argb[i]=0x00000000;} } sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888); return sourceImg; } }