如何将一布局转换为Bitmap??

Jing丶無雙 2014-11-04 02:59:47
部分代码如下:

public class MainActivity extends Activity {
protected static Bitmap bmp = null;
private LinearLayout ll;
private Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("1***", MainActivity.bmp + "");
// 该线性布局中有两个ImageView、一个TextView
ll = (LinearLayout) findViewById(R.id.ll);
bt = (Button) findViewById(R.id.next);
bmp = ll.getDrawingCache();// 将线性布局中的整个内容转换为位图
Log.d("2***", MainActivity.bmp + "");
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent it = new Intent(MainActivity.this, TwoActivity.class);
startActivity(it);
}
});
}

}



以下是Log日志






bmp = ll.getDrawingCache();// 将线性布局中的整个内容转换为位图
看来这样不能将这个线性布局转换为Bitmap的对象,请问一下大家有什么别的方法啊??
...全文
262 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wndroid 2017-07-06
  • 打赏
  • 举报
回复
LayoutInflater layoutInflater = LayoutInflater.from(context); View view = layoutInflater.layoutInflater.inflate(resourceId, root); BitmapDrawable drawable = (BitmapDrawable) view .getDrawable();
Jing丶無雙 2014-11-04
  • 打赏
  • 举报
回复
引用 3 楼 gqjjqg 的回复:
destroyDrawingCache 先去掉。我这么写是因为我取到之后就使用完了,中间使用bitmap的代码我去掉了。 要是下个界面的话,把bitmap 传递过去就好了,要是传递过去为null,就先copy到native 层的内存里。要用的时候再取就行了。
真的十分感谢,帮大忙喽 嘻嘻
Jing丶無雙 2014-11-04
  • 打赏
  • 举报
回复
谢谢啦 搞定了!原来只需要这样
     mCenter.buildDrawingCache();
                    mCenter.setDrawingCacheEnabled(true);
                    Bitmap b1 = mCenter.getDrawingCache(); 
                    //mCenter.setDrawingCacheEnabled(false);
                    //mCenter.destroyDrawingCache();
把这后面的屏蔽就行了
gqjjqg 2014-11-04
  • 打赏
  • 举报
回复
引用 2 楼 xj396282771 的回复:
[quote=引用 1 楼 gqjjqg 的回复:] CACHE 是在显示之后才有,也就是说不能在onCreate里 用CACHE。 建议这么写:
mCenter.buildDrawingCache();
					mCenter.setDrawingCacheEnabled(true);
					Bitmap b1 = mCenter.getDrawingCache(); 
					mCenter.setDrawingCacheEnabled(false);
					mCenter.destroyDrawingCache();
谢谢,我按照你给我的方法改成了这样:
public class MainActivity extends Activity {
	protected static Bitmap bmp = null;
	private LinearLayout ll;
	private Button bt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("1***", MainActivity.bmp + "");
		// 该线性布局中有两个ImageView、一个TextView
		ll = (LinearLayout) findViewById(R.id.ll);
		bt = (Button) findViewById(R.id.next);
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ll.buildDrawingCache();
				ll.setDrawingCacheEnabled(true);
				bmp = ll.getDrawingCache();// 将线性布局中的整个内容转换为位图
				Log.d("2***", MainActivity.bmp + "");
				ll.setDrawingCacheEnabled(false);
				ll.destroyDrawingCache();
				Intent it = new Intent(MainActivity.this, TwoActivity.class);
				startActivity(it);
			}
		});
	}
}
Log日志里面也显示有对象了 但是跳往下一界面时,我明明设置了iv.setImageBitmap(MainActivity.bmp);却仍旧是白板。这该如何处理啊??
public class TwoActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_two);
		ImageView iv = (ImageView) findViewById(R.id.iv);
		iv.setImageBitmap(MainActivity.bmp);
		Log.d("3***", MainActivity.bmp + "");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.two, menu);
		return true;
	}

}
[/quote] destroyDrawingCache 先去掉。我这么写是因为我取到之后就使用完了,中间使用bitmap的代码我去掉了。 要是下个界面的话,把bitmap 传递过去就好了,要是传递过去为null,就先copy到native 层的内存里。要用的时候再取就行了。
Jing丶無雙 2014-11-04
  • 打赏
  • 举报
回复
引用 1 楼 gqjjqg 的回复:
CACHE 是在显示之后才有,也就是说不能在onCreate里 用CACHE。 建议这么写:
mCenter.buildDrawingCache();
					mCenter.setDrawingCacheEnabled(true);
					Bitmap b1 = mCenter.getDrawingCache(); 
					mCenter.setDrawingCacheEnabled(false);
					mCenter.destroyDrawingCache();
谢谢,我按照你给我的方法改成了这样:
public class MainActivity extends Activity {
	protected static Bitmap bmp = null;
	private LinearLayout ll;
	private Button bt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("1***", MainActivity.bmp + "");
		// 该线性布局中有两个ImageView、一个TextView
		ll = (LinearLayout) findViewById(R.id.ll);
		bt = (Button) findViewById(R.id.next);
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ll.buildDrawingCache();
				ll.setDrawingCacheEnabled(true);
				bmp = ll.getDrawingCache();// 将线性布局中的整个内容转换为位图
				Log.d("2***", MainActivity.bmp + "");
				ll.setDrawingCacheEnabled(false);
				ll.destroyDrawingCache();
				Intent it = new Intent(MainActivity.this, TwoActivity.class);
				startActivity(it);
			}
		});
	}
}
Log日志里面也显示有对象了 但是跳往下一界面时,我明明设置了iv.setImageBitmap(MainActivity.bmp);却仍旧是白板。这该如何处理啊??
public class TwoActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_two);
		ImageView iv = (ImageView) findViewById(R.id.iv);
		iv.setImageBitmap(MainActivity.bmp);
		Log.d("3***", MainActivity.bmp + "");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.two, menu);
		return true;
	}

}
gqjjqg 2014-11-04
  • 打赏
  • 举报
回复
引用 楼主 xj396282771 的回复:
部分代码如下:
public class MainActivity extends Activity {
	protected static Bitmap bmp = null;
	private LinearLayout ll;
	private Button bt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("1***", MainActivity.bmp + "");
		// 该线性布局中有两个ImageView、一个TextView
		ll = (LinearLayout) findViewById(R.id.ll);
		bt = (Button) findViewById(R.id.next);
		bmp = ll.getDrawingCache();// 将线性布局中的整个内容转换为位图
		Log.d("2***", MainActivity.bmp + "");
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent(MainActivity.this, TwoActivity.class);
				startActivity(it);
			}
		});
	}

}
以下是Log日志 bmp = ll.getDrawingCache();// 将线性布局中的整个内容转换为位图 看来这样不能将这个线性布局转换为Bitmap的对象,请问一下大家有什么别的方法啊??
CACHE 是在显示之后才有,也就是说不能在onCreate里 用CACHE。 建议这么写:
mCenter.buildDrawingCache();
					mCenter.setDrawingCacheEnabled(true);
					Bitmap b1 = mCenter.getDrawingCache(); 
					mCenter.setDrawingCacheEnabled(false);
					mCenter.destroyDrawingCache();

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧