热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

保存视图像位图,我只得到黑色屏幕。-Saveviewlikebitmap,Ionlygetblackscreen

IhaveadrawingapplicationandIhavemethodsforsavingpicturesdrawn.ButIonlygetablackbi

I have a drawing application and I have methods for saving pictures drawn. But I only get a black bitmap picture with the save button. Where is the problem ?

我有一个绘图应用程序,我有保存绘制的图片的方法。但是我只得到一个带有保存按钮的黑色位图图片。问题在哪?

There is my xml

有我的xml







 


And my main Activity:

我的主要活动:

public class aktivita extends Activity{
Button btn;
public LinearLayout mContent;
krouzky mTicTacToeView = null;
public static String tempDir;
public File mypath;
 public static Bitmap mBitmap;
public static int width;
public static int height;
public static float x;
public static float y;
public static float X;
public static float Y;
public static double vzdalenost;
 public String current = null;
 private String uniqueId;
 public krouzky mSignature;
 public View mView;




public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.zaznam_ran);      
   krouzky.t = 0;






   File directory = new File(Environment.getExternalStorageDirectory() + "/Images");
   if(!directory.exists())

       directory.mkdir();

   uniqueId = getTodaysDate() + "_" + getCurrentTime();
   current = uniqueId + ".png";


   mypath= new File(directory,current);

   mCOntent= (LinearLayout) findViewById(R.id.linearLayout);

   mSignature = new krouzky(this, null);
   mSignature.setBackgroundColor(Color.TRANSPARENT);
   mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

   mView = mContent;





    Display display = getWindowManager().getDefaultDisplay();
     width = (display.getWidth());
     height = (display.getHeight());        
    mTicTacToeView = (krouzky) this.findViewById(R.id.pntr);





     Button btn6 = (Button) findViewById(R.id.button6);

        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    Log.v("log_tag", "Panel Saved");

                        mView.setDrawingCacheEnabled(true);
                       save(v);





                }


        });

}
     private boolean prepareDirectory()
    {
        try
        {
            if (makedirs())
            {
                return true;
            }
            else 
            {
                return false;
            }
        } 
        catch (Exception e)
        {
            e.printStackTrace();
            Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", 1000).show();
            return false;
        }
    }

    private boolean makedirs()  
    {
        File tempdir = new File(tempDir);
        if (!tempdir.exists())
            tempdir.mkdirs();

        if (tempdir.isDirectory())
        {
            File[] files = tempdir.listFiles();
            for (File file : files)
            {
                if (!file.delete())
                {
                    System.out.println("Failed to delete " + file);
                }
            }
        }
        return (tempdir.isDirectory());
    }   

method for saving

保存方法

    public  void save(View v)
    {
        Log.v("log_tag", "Width: " + v.getWidth());
        Log.v("log_tag", "Height: " + v.getHeight());
        if(mBitmap == null)
        {
            mBitmap =  Bitmap.createBitmap (width, height, Bitmap.Config.RGB_565);
        }
        Canvas canvas = new Canvas(mBitmap);
        try
        {

            FileOutputStream mFileOutStream = new FileOutputStream(mypath);

            v.draw(canvas);
            mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
            mFileOutStream.flush();
            mFileOutStream.close();


        }
        catch(Exception e)
        {
            Log.v("log_tag", e.toString());
        }
    }

My problem, was resolved, but I have one small detail. My application get down, when I change my xml. to this.

我的问题解决了,但我有一个小细节。当我更改xml时,我的应用程序就会崩溃。这个。





4 个解决方案

#1


2  

Actually You are passing the view (v) of your Button btn6 in method save(v)- Pass linear layout object which is mcontent or mView, then that would be save(mContent), This will solve your problem

实际上,您正在将按钮btn6的视图(v)传递到方法save(v)-传递线性布局对象,即mcontent或mView,然后保存(mcontent),这将解决您的问题。

You can try this method also-

你也可以试试这个方法

Pass your parent layout or view in method-

在方法中传递你的父布局或视图

 Bitmap file = save(mcontent);

 Bitmap save(View v)
   {
    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;
   }

#2


3  

Try this: - find your parent view

试试这个:-找到你的父视图

RelativeLayout holder = (RelativeLayout) findViewById(R.id.all);

- And call loadBitmapFromView(holder).

——调用loadBitmapFromView(持有人)。

public static Bitmap loadBitmapFromView(View v) {
    DisplayMetrics dm = getResources().getDisplayMetrics(); 
    v.measure(MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.EXACTLY));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    ((RelativeLayout) v).setGravity(Gravity.CENTER);
    Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
            v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(returnedBitmap);
    v.draw(c);

    return returnedBitmap;
}

#3


0  

You can use View.getDrawingCache()

您可以使用View.getDrawingCache()

Before it you should call View.setDrawingCache(true);

在它之前,您应该调用View.setDrawingCache(true);

#4


0  

I've created a cutom component that extends GroupView and used this method to save this layout to a bitmap.

我创建了一个cutom组件,它扩展了GroupView并使用此方法将这个布局保存到位图中。

    public Bitmap createBitmap() {
    //Log.d("Pin", "Image W ["+this.getWidth()+"] x H ["+this.getHeight()+"]");
    Bitmap b = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.RGB_565);       
    Canvas c = new Canvas(b);

    this.draw(c);

    return b;
}

Hope it helps you. If you want to save your layout you should change this with the entire layout you want to save

希望它能帮助你。如果你想要保存你的布局,你应该用你想要保存的整个布局来改变它


推荐阅读
author-avatar
caihuiqian_558
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有