当前位置:操作系统 > 安卓/Android >>

Android:将View的内容映射成Bitmap

最近在做一个类似于游标的东西,由一个类似于seekbar的view来控制下端view内容的显示位置。所以需要将view中的内容映射成一张图片,设为seekbar的背景。所以就做了一些尝试,不过还有一些遗漏的小问题。

      在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap,但是刚开始使用的时候,得到的结果都是null,所以在一个论坛里查到了正确的使用方法.代码如下:

 

Java代码 
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(); 

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(); 
      最后就可以调用:Bitmap bitmap = view.getDrawingCache();就可以得到图片了。

 

      为了测试这个功能,我使用了两种方式来创建LinerLayout中的内容,一种是在xml文件中就将view的内容添加了,只需在代码中添加对应ImageView中的图片就行了;另一种是动态添加LinerLayout中的View。

      项目的主界面为:

  


 

接下来是SET_VIEW的代码:

 

Java代码 
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");  
    }  

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

 set_view.xml布局文件为:

 

Xml代码 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:text="set_view" android:layout_width="fill_parent" 
        android:layout_height="wrap_content"></TextView> 
    <LinearLayout android:id="@+id/content" 
        android:orientation="vertical" android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 
        <ImageView android:id="@+id/imgSource1" 
            android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> 
        <ImageView android:id="@+id/imgSource2" 
            android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> 
    </LinearLayout> 
 
    <LinearLayout android:id="@+id/cache" 
        android:layout_width="wrap_content" android:layout_height="wrap_content">

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,