`

将View的内容映射成Bitmap转图片导出

阅读更多
将view映射到一个bitmap中,稍加改进可以用于一些截图工具或者截图软件(QQ截图之类),例子写的不够完善,不过很有些学习的意义内容大致如下:

在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap。
在使用的时候调用

Bitmap bitmap = view.getDrawingCache();

就可以得到图片的bitmap了。

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

[/img]
布局文件:
main.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">
	<Button
		android:id="@+id/setview"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="SET_VIEW" />
	<Button
		android:id="@+id/addview"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="ADD_VIEW" />
</LinearLayout>


add_view.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="add_view"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
	<LinearLayout
		android:id="@+id/addViewContent"
		android:orientation="vertical"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" />


	<LinearLayout
		android:id="@+id/addViewCache"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
		<ImageView
			android:id="@+id/imgAddViewCache"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content" />

	</LinearLayout>

</LinearLayout>


set_view.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" />
	<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
			android:id="@+id/imgSource2"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</LinearLayout>

	<LinearLayout
		android:id="@+id/cache"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
		<ImageView
			android:id="@+id/imgCache"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />

	</LinearLayout>
</LinearLayout>


AddViewActivity
package com.zart;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class AddViewActivity extends Activity {

	private LinearLayout addViewContent;
	private ImageView imgAddViewCache;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_view);
		addViewContent = (LinearLayout) findViewById(R.id.addViewContent);
		imgAddViewCache = (ImageView) findViewById(R.id.imgAddViewCache);
		// addImgSource();
		addRelativeLayout();

		addViewContent.setDrawingCacheEnabled(true);
		addViewContent.measure(MeasureSpec.makeMeasureSpec(0,
				MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0,
				MeasureSpec.UNSPECIFIED));
		addViewContent.layout(0, 0, addViewContent.getMeasuredWidth(),
				addViewContent.getMeasuredHeight());

		addViewContent.buildDrawingCache();
		int color = addViewContent.getDrawingCacheBackgroundColor();

		Bitmap cacheBitmap = addViewContent.getDrawingCache();
		Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);// 注意:这地方必须特别注意
		if (bitmap != null) {
			imgAddViewCache.setImageBitmap(bitmap);
			imgAddViewCache.setDrawingCacheBackgroundColor(color);
		} 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));
	}

}


MainActivity
package com.zart;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private Button btn_setView;
	private Button btn_addView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn_setView = (Button) findViewById(R.id.setview);
		btn_addView = (Button) findViewById(R.id.addview);
		btn_setView.setOnClickListener(this);
		btn_addView.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.setview:
			Intent intent1 = new Intent();
			intent1.setClass(this, SetViewActivity.class);
			startActivity(intent1);
			break;
		case R.id.addview:
			Intent intent2 = new Intent();
			intent2.setClass(this, AddViewActivity.class);
			startActivity(intent2);
			break;
		default:
			break;
		}
		

	}


}


SetViewActivity
package com.zart;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View.MeasureSpec;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class SetViewActivity extends Activity {
	/** Called when the activity is first created. */
	private LinearLayout contentLayout;
	private ImageView imgSource1;
	private ImageView imgSource2;
	private ImageView imgCache;

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


转自:http://hddev.blog.51cto.com/3365350/629808
只为学习。
  • 大小: 11.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics