2012-01-20 80 views
6

我想通过使用DrawingCache捕获ImageView的内容。我写了下面的代码。getDrawingCache()总是返回null

iv1 = (ImageView)findViewById(R.id.iv1); 
iv2 = (ImageView)findViewById(R.id.iv2);    
iv1.setDrawingCacheEnabled(true); 
Bitmap myScreenshot = iv1.getDrawingCache(); 
iv2.setImageBitmap(myScreenshot); 

但我在屏幕上只能看到一个图像。后来我才知道myScreenshot为空

我看到很多帖子关于同样的问题,但没有适当的解决方案。

我想我们必须添加在清单中的任何权限?或实现此目的所需的root权限?请帮我解决这个问题。

回答

10

尝试调用编辑buildDrawingCache()getDrawingCache()

呼叫getDrawingCache(),页面之后已加载,而不是onCreate

+0

我加了iv1.buildDrawingCache();在getDrawingCache()之前。没有使用 –

+0

您是否在应用程序运行时检查了iv1是否有图像。还要指定你在哪里调用你的代码。你可能会调用它之​​前的意见初始化 – aqs

+0

在xml文件中,我只给了android:src for iv1。我在这里发布的代码是在setContentView(R.layout.main)之后;在onCreate()中是否正确? –

0
imageview.setBackgroundResource(R.drawable.imageview);     

imageview1.setBackgroundResource(R.drawable.imageview1); 

使用该两个图像是screan

<RelativeLayout 
    android:id="@+id/item"    
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="320dp" 
     android:layout_height="486dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/iv1" /> 

    <ImageView 
     android:id="@+id/ic_launcer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/iv2" /> 

</RelativeLayout> 
1

你不要做 iv1.buildDrawingCache(true);添加行之前
Bitmap myScreenshot = iv1.getDrawingCache();

+0

您的答案与@aqs – gecco

2

在onWindowFocusChanged()中调用getDrawingCache()而不是在onCreate()中,那么您将不会获得null。

+0

所写的接受答案完全相同,谢谢,它对我有用! –