Click to read pen and write app (1)--Get picture drawing from drawable

Source: Internet
Author: User

"If you want to know the background of this point reading the pen and writing app, please go here
http://www.jianshu.com/p/ee2a1bb99280 "

Until this article, I do not know that in the process of running the Android app need to use the picture file should be placed where appropriate, one can certainly not be directly in the app installation of the folder to write to the installation path package, because this will happen mistakenly deleted, This causes the app to run without getting a picture error. If you should not do this, please tell me where to put it.

As the topic said, I put him in the drawable, the operation can be convenient to obtain the picture, but put in the drawable file convenient at the same time also bring me some trouble, listen to me slowly.

In the app, I go to get the picture to be the background of the notebook's page (that is, a line of lines on a regular notebook), write on it, the actual point is to use a specific pixel to replace the previous background image pixels. Of course, the final notebook to write the content of the picture is to be saved, because I hope the next time I open the notebook, I wrote on that page of the small mood still exists. Of course I can't put them in the drawable, I choose to save these records locally.

The content of this article does not relate to how I get pictures locally to initialize my notebook pages or how I write my Pictures locally from memory. These are mentioned here because when you open a notebook, it's a little different to take out the pages you've written and from the drawable. It's normal for me to continue writing on the pages I've written before, and I should have this feature. (You as a programmer should not stop me from the essence of thrift.) Here by the way to give you an example: I have a college roommate, graduate school that will, he was in the draft calculation is the first with a black pen full of drafts, and then change the red pen to write full, and then throw away. What, save it? In fact, he probably wanted to show himself the fruits of his efforts in this way. After all, not every calculation is a result, in that period of confusion and emptiness of the day, the traces of these efforts can prove to live up to youth just . Of course, initialize the written content, save the written content to the local, will write a separate article, please step here. Not yet.
Here's the start:

How to get Bitmap objects
    • Getresources (). Getdrawable.getbimap
    • Gain by bitmapfactory function fatigue

We need to draw on a picture, the process is: Get the diagram of the bitmap object, create a canvas, and at the corresponding pixel painting.
In the course of the project, I found it very convenient to use Bitmapfactory to manipulate bitmap objects,

BitmapFactory.Options opts = new BitmapFactory.Options();opts.inMutable = true;baseBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.page_background, opts);

Bitmapfactory.options is bitmapfactory a function parameter setting that indicates how to handle the object, and if the processing actually determines what we get (although life is more complicated than this, it is also the reason). As here the setting inmutable for true,mutable is mutable meaning, that is, for acquiring the picture we are able to

canvas = new Canvas(baseBitmap);

This creates a canvas for painting. The default obtained bitmap is not support to change the content of bitmap, even if you take this piece of 2 binary data into memory, the reason why do not know is not to protect the integrity of the data (I do not feel very necessary, after all, you go to new canvas is to indicate that you want to operate, in the VC Not protected in the + +). Note that this is not a mutable when creating a canvas will error. If take is the first method obtains the bitmap words, certainly also is non-mutable, here the method is

//其中copy还需要传进参数baseBitmap =((BitmapDrawable)getResources().getDrawable(R.drawable.page_background))    .getBitmap().copy();

Because the second is concise, I recommend the second way, so the parameters in copy I will not say, interested in their own look at the API document it.

Scale a picture proportionally

This describes two ways of zooming

    • Bitmapfactory Proportional Scaling
    • CreateBitmap scale by length-to-width ratio
Bitmapfactory Proportional Scaling

Here we set a scene, assuming we need to get a bitmap object whose length is the size of the original image, i.e. a 800*800 (px) image, the bitmap object I want to get is 800*800. Maybe a little bit about Android friends will know that in the Drawable folder of the picture will be based on his folder suffix (drawable-ldpi,-mdpi,-hdpi,-xhdpi, xxhdpi) to automatically do some stretching, Android is such a mechanism to ensure that the different display resolution on the consistency of the display effect. What the hell is going on here, I'll explain it at the end of the article . This is what I said before, from the local take out the page pictures and from the drawable will be a little different, because drawable will automatically do the image of the zoom Ah, from the drawable may have been reduced, so we want to get the original picture. is not feeling a bit chaotic, actually did not ah, I went to get the original picture is because I knew in advance drawable will scale the picture AH (actually which has what beforehand, before and no one told me, I also fell into the pit first to climb out of the pit).
As for why I want to get the original picture, because I have only one pixel matrix of the original graph I can go to the corresponding pixel position to paint in the "arbitrary" way. Like 800*800 's diagram, I want to divide it into 8*8 's lattice, and then I paint the upper left corner of the grid black:

canvas.drawRect(00100100);

Okay, let's take a look at it. If you use Bitmapfactory to scale the original scale by length-width:

//firstlyBitmapfactory.options opts =NewBitmapfactory.options (); options.injustdecodebounds =true;//This parameter means that the bitmap object is not established, only the original size of the picture, the advantage of doing so is to save memoryBitmapfactory.decoderesource (Getresources (), R.drawable.page_background, opts);//There is no need to return bitmapintbe = (int) (Options.outheight/(float) $);//200 for the hope of high zoom to 200, as long as the setting this is good, because the width and height is proportional//thenOptions.injustdecodebounds =false;//default is False. meaning establishment bitmapOption.insamplesize = be;//Note that the above calculation is the cast of the int, because insample only accepts intBasebitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.page_background, opts);

The above is the image from the 800*800 zoom to the 200*200 example, we do a simple calculation, equivalent to the Option.insamplesize assignment is 4, the obtained figure is the original 1/4. Insamplesize can only be integers, so we can only get images for 1,1/2,1/3,... (This is a problem, 2/3 is not allowed to exist, even if your arithmetic calculation results are integers, this is the last part of the above to give an example ). In fact, know that the bitmapfactory insamplesize set invented to quickly get thumbnails for use, you will be relieved.
According to the above, I want to get the original image, then I should not

 be = (int)(options.outHeight / (float)options.outHeight);//be = 1

Set Option.insamplesize=1, unfortunately the obtained graph is still scaled. That means I didn't use bitmapfactory to take out the picture of the drawable without scaling. In fact, if you can get not to do zoom Picture:

BitmapFactory.decodeFile("/sdcard/test.jpg", options);//注意为本地读取,这就是和drawable的区别了。options不做任何设置,就是取原图
CreateBitmap scale by length-to-width ratio
matrix00matrix, false);

The calculations for ScaleWidth and ScaleHeight are similar SCALEWIDTH=200/800=1/4 where 200 of the dimensions they want to zoom to, Of course here the ScaleWidth and ScaleHeight can be specified separately to different sizes, and there is no limit to the int type.
The parameter in the Bitmap.createbitmap function is Bitmap to the specified scale object, and the next 0, 0, Bmpwith, Bmpheight is the area of the Bitmap object taken, and we bmpwith here, Bmpheight is the corresponding value of the specified object for bitmap, and the meaning takes the whole graph to zoom. The matrix is given a scale scale.

Recognize the role of drawable (HDPI,MDPI,LDPI, etc.)

First, the system is based on the published target device PPI(pixels per inch) to the corresponding folder to search for the added resources of the image, the search is taken out. The difference between DPI and PPI search order is: corresponding folder, with identity folder (HDPI,MDPI,LDPI, and so on, the nearest principle, as to whether the first search big or small, dig this meaningless, after all, if you take the realization of the function of the implementation of the following you) Drawable (default folder). While this DPI is different for each device, Android does not have a single drawable for each dpi, so these dpi are policies that have a specific value nearby .
DPI for each folder

folder Type dpi Value
Drawable 160dpi
drawable-ldpi 120dpi
drawable-mdpi 160dpi
drawable-hdpi 240dpi
drawable-xhdpi 320dpi
drawable-xxhdpi 480dpi

In fact, finally I want to get the original size of the image is not in the second way (the first direct take is not the original size, said before), after all, the kind of stretching can be caused by the deformation of the picture (height and width can be stretched according to different proportions AH). Mainly I like bitmapfactory that kind of operation, encapsulation is very good, if we develop our own process can be made of tools to make this look better, think of the time to look at the source bar (in fact, I did not see it, haha). So how do I do it?

Bitmapfactory. Optionsopts = new Bitmapfactory. Options();Basebitmap = Bitmapfactory. Decoderesource(Getresources (), R. drawable. Page_background, opts);Displaymetrics dm = new Displaymetrics ();Getwindowmanager (). Getdefaultdisplay(). Getmetrics(DM);float density = DM. Density;nnibsize = (int) ( -* ( the* Density/ -));//hdpi represent for 240dp;density depend on;

The previous paragraph we know what is going on, I want to take a picture of the original size (of course, through the URL to local fetch is the original size), it gave me a stretch after it. And then I displaymetrics. Gets the PPI of the target device gives me a corresponding magnification of the pixel width of the brush. That is, I 800*800 before the picture, draw a point is 100*100, each 100*100 box is a point. Now the picture of 800*800 is enlarged, then my corresponding point size also to enlarge (shrink also shrinks, I do this also to be compatible not PPI device, I also very good). Among them, Nnibsize is what I call a dot pixel (100*100). An example would be clearer:

* My device is 5.7inch, resolution is 1920*1080 (PX), calculate the device PPI

This is to get my device PPI. And my device was filed near the drawable? I debug my Code, I found 800*800 picture placed under-HDPI, get the size of the figure for 1467*1467, why this is the value. In the above code, I obtained a nearest pixel density, found that dm.density obtained a value of 2.75, that is, our 386 pixel density near the 440=2.75*160, the result shows that 440 is not in the folder listed in our table, Explains that Android has its own set of archiving strategy, as for why there are 440 this file, there is no need to delve into, it is the way the realization of the people want to deal with things.
The value of 1467=800* (2.75*160/240) is scaled according to this. First of all our images in-hdpi, this pixel density is lower than the corresponding pixel density of our device. That is, in the hdpi corresponding pixel density, our diagram should be displayed as the original, and to the high PPI if the original, then the picture is smaller than the screen, to achieve the same display is to the picture according to the proportion of the PPI magnification, this ratio is 2.75*160/240. So nnibsize = (int) (* * density/240)); This line of code I just scaled my 100 pixel size to fit a larger figure. The effect can be in such as, regardless of the PPI is how much, always can only black a lattice at a time, do not appear to be painted to other pixels or the case of dissatisfaction. *

Click to read pen and write app (1)--Get picture drawing from drawable

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.