ViewToBitmap

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; 
	} 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值