有时候在开发过程中可能需要通过获得View的副本来做一些处理,为了得到副本,一种方法是用另一个布局完全一样的View来拷贝目标内容,另外一种方法是直接获得这个View的截图。很显然获得截图的方式更便捷灵活。
我们可以通过View.getDrawingCache()方法来获得View的截图Bitmap,但需要经过以下几个步骤:
1.View.setDrawingCacheEnabled(true);将View缓存下来
2.View.getDrawingCache();获得缓存的bitmap
3.拷贝获得的bitmap
4.View.setDrawingCacheEnabled(false);清除缓存
可能我们会对第3步有疑惑,第2步已经得到缓存的bitmap了,为什么还要重新拷贝?在使用View.setDrawingCacheEnabled(true);缓存图片后,系统将不再自动更新View的缓存,所以在获得缓存的bitmap后,我们必须要使用View.setDrawingCacheEnabled(false)来清空缓存,以便下一次获得新的截图。而一旦使用View.setDrawingCacheEnabled(false)后,缓存的bitmap将会被释放掉,此时我们再引用到该bitmap则会报错,所以我们需要对这个bitmap再做备份。
下面是测试demo,我们需要获得包含图片和文字信息的RelativeLayout的截图。
<ScrollView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ydz.test028_viewshot.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/layout_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/image_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/horse" />
<TextView
android:id="@+id/text_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="骏马"
android:textColor="#00FF00"
android:textSize="20sp" />
</RelativeLayout>
<Button
android:id="@+id/btn_getshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GetPicture" />
<Button
android:id="@+id/btn_change_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ChangePicture" />
<ImageView
android:id="@+id/img_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
package com.ydz.test028_viewshot;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private RelativeLayout mLayoutSource;
private Button mButtonGetShot;
private Button mButtonChange;
private ImageView mImageResult;
private ImageView mImageSource;
private TextView mTextViewSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayoutSource = (RelativeLayout) findViewById(R.id.layout_source);
mButtonGetShot = (Button) findViewById(R.id.btn_getshot);
mButtonChange = (Button) findViewById(R.id.btn_change_picture);
mImageResult = (ImageView) findViewById(R.id.img_result);
mImageSource = (ImageView) findViewById(R.id.image_source);
mTextViewSource = (TextView) findViewById(R.id.text_source);
mButtonGetShot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mLayoutSource.setDrawingCacheEnabled(true);
Bitmap tBitmap = mLayoutSource.getDrawingCache();
// 拷贝图片,否则在setDrawingCacheEnabled(false)以后该图片会被释放掉
tBitmap = tBitmap.createBitmap(tBitmap);
mLayoutSource.setDrawingCacheEnabled(false);
if (tBitmap != null) {
mImageResult.setImageBitmap(tBitmap);
Toast.makeText(getApplicationContext(), "获取成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "获取失败", Toast.LENGTH_SHORT).show();
}
}
});
mButtonChange.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mImageSource.setImageResource(R.drawable.light);
mTextViewSource.setText("孤灯");
}
});
}
}
第二种:直接获取(不保存)
/**
* 获取Layout截图
*
* @return 所需区域的截图
*/
private Bitmap getBitmap(View view) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(); //启用DrawingCache并创建位图
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); //创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收
view.setDrawingCacheEnabled(false); //禁用DrawingCahce否则会影响性能
return bitmap;
}