- contentLayout.setDrawingCacheEnabled(true);
- contentLayout.measure(
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
- contentLayout.layout(0, 0, contentLayout.getMeasuredWidth(),
- contentLayout.getMeasuredHeight());
- contentLayout.buildDrawingCache();
- //会报Canvas: trying to use a recycled bitmap android.graphics.Bitmap
- //Bitmap bitmap= contentLayout.getDrawingCache();
- //解决办法:
//Copy the bitmap before you pass it to the static variable.
Bitmap bitmap = contentLayout.getDrawingCache(true);
//to
Bitmap bitmap
= Bitmap.createBitmap(contentLayout.getDrawingCache(true));
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.set_view);
- contentLayout = (LinearLayout) findViewById(R.id.content);
- imgSource1 = (ImageView) findViewById(R.id.imgSource1);
- imgSource2 = (ImageView) findViewById(R.id.imgSource2);
- imgCache = (ImageView) findViewById(R.id.imgCache);
- imgSource1.setImageResource(R.drawable.source1);
- imgSource2.setImageResource(R.drawable.source2);
- contentLayout.setDrawingCacheEnabled(true);
- contentLayout.measure(
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
- contentLayout.layout(0, 0, contentLayout.getMeasuredWidth(),
- contentLayout.getMeasuredHeight());
- contentLayout.buildDrawingCache();
- Bitmap bitmap= contentLayout.getDrawingCache();
- if(bitmap!=null){
- imgCache.setImageBitmap(bitmap);
- }else{
- Log.i("CACHE_BITMAP", "DrawingCache=null");
- }
- }
- private void addRelativeLayout() {
- // TODO Auto-generated method stub
- RelativeLayout.LayoutParams layoutpare = new RelativeLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- RelativeLayout relativeLayout = new RelativeLayout(this);
- relativeLayout.setLayoutParams(layoutpare);
- ImageView imgView1 = new ImageView(this);
- ImageView imgView2 = new ImageView(this);
- imgView1.setImageResource(R.drawable.source1);
- imgView2.setImageResource(R.drawable.source2);
- RelativeLayout.LayoutParams img1 = new RelativeLayout.LayoutParams(38,
- 38);
- img1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
- RelativeLayout.LayoutParams img2 = new RelativeLayout.LayoutParams(38,
- 38);
- img2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
- relativeLayout.addView(imgView1, img1);
- relativeLayout.addView(imgView2, img2);
- addViewContent.addView(relativeLayout);
- }
- /**
- * 添加图片源
- */
- private void addImgSource() {
- ImageView imgView1 = new ImageView(this);
- ImageView imgView2 = new ImageView(this);
- imgView1.setImageResource(R.drawable.source1);
- imgView2.setImageResource(R.drawable.source2);
- addViewContent.addView(imgView1, new LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT));
- addViewContent.addView(imgView2, new LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT));
- }