如何将转换的LinearLayout图像? [英] How to convert a LinearLayout to image?
本文介绍了如何将转换的LinearLayout图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试以下code转换的的LinearLayout
图像:
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 的LinearLayout LYT =(的LinearLayout)findViewById(R.id.lyt);
lyt.setDrawingCacheEnabled(真);
lyt.buildDrawingCache(真); 位图B = Bitmap.createBitmap(lyt.getDrawingCache()); ImageView的IMG =(ImageView的)findViewById(R.id.imageView1);
img.setImageBitmap(二);}
但我得到了 NullPointerException异常
在
位图B = Bitmap.createBitmap(lyt.getDrawingCache());
在布局XML是:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<的LinearLayout
机器人:ID =@ + ID / LYT
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直> <按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=按钮1/> <按钮
机器人:ID =@ + ID /按钮3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=按钮2/> <按钮
机器人:ID =@ + ID /按钮2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=按钮3/> < / LinearLayout中> < ImageView的
机器人:ID =@ + ID / imageView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:SRC =@绘制/ ic_launcher/>< / LinearLayout中>
解决方案
尝试下面code,它的工作原理
添加下面的权限清单中
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/>
MainActivity.java
公共类MainActivity扩展活动实现OnClickListener {
私人的LinearLayout的LinearLayout;
私人按钮saveBtn;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
的LinearLayout =(的LinearLayout)findViewById(R.id.linearLayout_view);
saveBtn =(按钮)findViewById(R.id.save_btn);
saveBtn.setOnClickListener(本);
}@覆盖
公共无效的onClick(视图v){
INT ID = v.getId();
开关(ID){
案例R.id.save_btn:
文件file = saveBitMap(这一点,LinearLayout中); //其中查看您想通过这一观点作为参数
如果(文件!= NULL){
Log.i(TAG,绘图保存到画廊!);
}其他{
Log.i(TAG,!哎呀图像无法保存。);
}
打破;
默认:
打破;
}私人文件saveBitMap(上下文的背景下,查看drawView){
文件pictureFileDir =新的文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),Handcare);
如果(!pictureFileDir.exists()){
布尔isDirectoryCreated = pictureFileDir.mkdirs();
如果(!isDirectoryCreated)
Log.i(ATG,无法创建目录保存图像);
返回null;
}
字符串文件名= pictureFileDir.getPath()+文件分割符+ System.currentTimeMillis的()+JPG;
文件PictureFile的=新的文件(文件名);
位图位图= getBitmapFromView(drawView);
尝试{
pictureFile.createNewFile();
FileOutputStream中的ostream =新的FileOutputStream(PictureFile的);
bitmap.com preSS(比较pressFormat.PNG,100,ostream的);
oStream.flush();
oStream.close();
}赶上(IOException异常五){
e.printStackTrace();
Log.i(TAG,有保存图像的问题。);
}
scanGallery(上下文,pictureFile.getAbsolutePath());
PictureFile的回报;
}
//从视图中创建位图并返回
私人位图getBitmapFromView(查看视图){
//定义一个位图的大小相同的观点
位图returnedBitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888);
//绑定一个画布它
帆布帆布=新的Canvas(returnedBitmap);
//获取视图的背景
可绘制bgDrawable = view.getBackground();
如果(bgDrawable!= NULL){
//有背景的绘制,然后绘制在画布上
bgDrawable.draw(画布);
}其他{
//没有背景的绘制,然后绘制白色背景的画布上
canvas.drawColor(Color.WHITE);
}
//绘制视图在画布上
view.draw(画布);
//返回位图
返回returnedBitmap;
}
//用于扫描图库
私人无效scanGallery(上下文CNTX,字符串路径){
尝试{
MediaScannerConnection.scanFile(CNTX,新的String [] {}路径,空,新MediaScannerConnection.OnScanCompletedListener(){
公共无效onScanCompleted(字符串路径,开放的URI){
}
});
}赶上(例外五){
e.printStackTrace();
}
}
}
I tried the following code to convert the LinearLayout
to image:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lyt = (LinearLayout) findViewById(R.id.lyt);
lyt.setDrawingCacheEnabled(true);
lyt.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(lyt.getDrawingCache());
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(b);
}
but I got NullPointerException
in :
Bitmap b = Bitmap.createBitmap(lyt.getDrawingCache());
where the layout XML is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/lyt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
解决方案
Try below code, it works
Add below permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
private LinearLayout linearLayout;
private Button saveBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout_view);
saveBtn = (Button) findViewById(R.id.save_btn);
saveBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.save_btn:
File file = saveBitMap(this, linearLayout); //which view you want to pass that view as parameter
if (file != null) {
Log.i("TAG", "Drawing saved to the gallery!");
} else {
Log.i("TAG", "Oops! Image could not be saved.");
}
break;
default:
break;
}
private File saveBitMap(Context context, View drawView){
File pictureFileDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Handcare");
if (!pictureFileDir.exists()) {
boolean isDirectoryCreated = pictureFileDir.mkdirs();
if(!isDirectoryCreated)
Log.i("ATG", "Can't create directory to save the image");
return null;
}
String filename = pictureFileDir.getPath() +File.separator+ System.currentTimeMillis()+".jpg";
File pictureFile = new File(filename);
Bitmap bitmap =getBitmapFromView(drawView);
try {
pictureFile.createNewFile();
FileOutputStream oStream = new FileOutputStream(pictureFile);
bitmap.compress(CompressFormat.PNG, 100, oStream);
oStream.flush();
oStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.i("TAG", "There was an issue saving the image.");
}
scanGallery( context,pictureFile.getAbsolutePath());
return pictureFile;
}
//create bitmap from view and returns it
private Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
} else{
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
}
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
// used for scanning gallery
private void scanGallery(Context cntx, String path) {
try {
MediaScannerConnection.scanFile(cntx, new String[] { path },null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
这篇关于如何将转换的LinearLayout图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文