WO2018113725A1 - Method and device for loading picture resource, and program and medium - Google Patents

Method and device for loading picture resource, and program and medium Download PDF

Info

Publication number
WO2018113725A1
WO2018113725A1 PCT/CN2017/117675 CN2017117675W WO2018113725A1 WO 2018113725 A1 WO2018113725 A1 WO 2018113725A1 CN 2017117675 W CN2017117675 W CN 2017117675W WO 2018113725 A1 WO2018113725 A1 WO 2018113725A1
Authority
WO
WIPO (PCT)
Prior art keywords
picture
resource
background
loading
bitmap
Prior art date
Application number
PCT/CN2017/117675
Other languages
French (fr)
Chinese (zh)
Inventor
杨德立
Original Assignee
北京奇虎科技有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 北京奇虎科技有限公司 filed Critical 北京奇虎科技有限公司
Publication of WO2018113725A1 publication Critical patent/WO2018113725A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/445Program loading or initiating
    • G06F9/44505Configuring for program initiating, e.g. using registry, configuration files

Definitions

  • the present invention belongs to the field of resource loading technologies, and in particular, to a method for loading a picture resource, a device for loading a picture resource, a computer program, and a computer readable medium.
  • the image loading function is a basic function required by the terminal operating system.
  • the default resource loading method is usually used to load the image, but the system is loaded.
  • the default resource loading method cannot uniformly manage the loading of image resources when loading image resources, which leads to cumbersome resource loading and increases the cost of resource loading.
  • the present invention provides a method, an apparatus, and a computer program and a computer readable medium for loading a picture resource, which are used to solve the version of the Android system version 3.0 or lower in the prior art, which is loaded in the prior art.
  • the picture resource is used, there is a technical problem that the image resource cannot be uniformly managed.
  • the invention discloses a method for loading a picture resource, the method comprising: encapsulating a loading program of a picture background setting and a loading program for displaying a picture content into a picture resource loading class; loading a class encapsulation and resource management for the picture resource
  • An interface is configured to invoke a corresponding image resource loading class by using the resource management interface, and load a corresponding image background and a picture content display.
  • the method further includes: storing the bitmap pixel data generated during the loading process into a pre-established bitmap set.
  • the method further includes:
  • the image resource recycling program is called through the resource management interface according to a preset recycling timing, and the useless image resource is recovered.
  • the recovery timing is specifically when the picture on the interface is deleted.
  • the loading method specifically includes: when it is determined that the background of the image is not loaded, obtaining a view size to be displayed, and determining the background of the image according to the view size. Whether compression is required; when it is determined that the background of the picture needs to be compressed, the background of the picture is compressed according to a first compression ratio;
  • the method when it is determined that the background of the picture does not need to be compressed, the method further includes: generating first bitmap pixel data of the picture background according to the current picture background.
  • the method further includes: capturing an insufficient memory exception, and when the abnormality is caught, abandoning loading the current picture background resource.
  • the method when it is determined that the background of the picture has been loaded, the method further includes: calling the first bitmap pixel data of the background of the picture from the set of bitmaps.
  • the method further includes: deleting the generated first bitmap pixel data; searching and parsing the picture background resource by using the resource ID, and generating the second bitmap object.
  • the second bitmap object is assigned to the view that needs to display the background of the image.
  • the method before loading the picture resource, the method further includes: determining a resource type according to the resource ID; and when determining that the resource type is a picture resource, calling the corresponding picture resource loading class by using the resource management interface, and loading the corresponding Picture resources.
  • the resource types include: a picture resource, a color value resource, and an XML resource.
  • the method when the resource type is determined to be a color value resource and an XML file resource, the method further includes:
  • the resource ID is used to find and parse the corresponding resource, and a corresponding bitmap object is generated; and the corresponding bitmap object is assigned to the view that needs to display the background of the image.
  • the loading method specifically includes:
  • the third bitmap object is assigned to a view that needs to display the display of the image content.
  • the invention also discloses an apparatus for loading a picture resource, the apparatus comprising: a first encapsulating module, configured to encapsulate a loading program of a picture background setting and a loading program of a picture content display into a picture resource loading class; the second encapsulating module And the method for loading a class to encapsulate a resource management interface for the image resource; the calling module is configured to invoke a corresponding image resource loading class by using the resource management interface, and load the corresponding image background and image content display.
  • the device further includes a storage module, and after loading the picture resource, the storage module is configured to: store the bitmap pixel data generated during the loading process into a pre-established bitmap set.
  • the calling module is further configured to invoke a picture resource recovery program through the resource management interface according to a preset collection timing to recover useless picture resources.
  • the recovery timing is specifically when the picture on the interface is deleted.
  • the first encapsulating module when the picture resource is loaded and loaded as a background setting of the picture, the first encapsulating module includes:
  • a first determining module configured to determine a view size to be displayed when the background of the picture is not loaded, and a root Determining whether the background of the picture needs compression according to the view size; determining that the picture background is compressed according to the first compression ratio when the background of the picture needs to be compressed; and the first generating module, configured to generate the background according to the compressed picture a first bitmap pixel data of the background of the picture; a second determining module, configured to: when determining that the background of the picture is a normal picture, generate a first bitmap object according to the pixel data of the first bitmap; the first assignment module, And is used to assign the first bitmap object to a view that needs to display a background of the picture.
  • the first generating module is further configured to: generate first bitmap pixel data of the picture background according to the current picture background.
  • the first encapsulating module further includes: a first capturing module, configured to capture an out of memory exception during the loading process, and abandon loading the background resource of the image when an abnormality is captured.
  • the first encapsulating module further includes: a first calling module, when the first determining module determines that the background of the picture has been loaded, the first calling module is configured to invoke the The first bitmap pixel data of the background of the picture.
  • the first encapsulating module further includes: a first deleting module; when the second determining module determines that the background of the picture is a point nine figure, the first deleting module is configured to delete the generated a bitmap pixel data; the first generation module is further configured to: find and parse the picture background resource by using the resource ID to generate a second bitmap object; the first assignment module is further configured to assign the second bitmap object Give a view of the background of the image you want to display.
  • the first determining module is further configured to determine the resource type according to the resource ID; when it is determined that the resource type is a picture resource, the calling module is configured to use the resource management interface Call the corresponding image resource to load the class and load the corresponding image resource.
  • the resource types include: a picture resource, a color value resource, and an XML resource.
  • the first encapsulating module further includes: a second generating module; wherein, when the first determining module determines that the resource type is a color value resource and an XML file resource, the second generating module uses The corresponding resource is searched and parsed by using the resource ID to generate a corresponding bitmap object; the first assignment module is further configured to assign a corresponding bitmap object to a view that needs to display a background of the image.
  • the first package module when the picture resource is loaded as a loading of the picture content display, the first package module further includes:
  • a third determining module configured to determine that the picture content display is not loaded, obtain a view size that needs to be displayed, and determine, according to the view size, whether the picture content display needs to be compressed; when the picture content display needs to be compressed, according to The second compression ratio compresses the picture content display; the third generation module is configured to generate second bitmap pixel data displayed by the picture content according to the compressed picture content display;
  • a fourth determining module configured to: when the picture content is displayed as a normal picture, generate a third bitmap object according to the second bitmap pixel data; and a second assignment module, configured to use the third bitmap object Assign to the view that needs to display the display of the image content.
  • the invention also provides a computer program comprising computer readable code, when the readable code is run on a computing device, causing the computing device to perform a load picture resource according to any of the embodiments of the invention Methods.
  • the embodiment of the invention further provides a computer readable medium, wherein the program according to the embodiment of the invention is stored.
  • the present invention provides a method and an apparatus for loading a picture resource, the method comprising: encapsulating a loader for setting a background of a picture and a loader for displaying a picture content into a picture resource loading class, and loading a class package for the picture resource A resource management interface; in this way, the resource management interface includes two loaders: a loader for setting the background of the image and a loader for displaying the image content; when the image resource needs to be loaded, whether the background image is loaded or the image is loaded.
  • the content shows that the service layer can call the corresponding image resource loading class through the resource management interface, and load the corresponding image background and the image content display, thereby optimizing the resource loading process and reducing the cost of resource loading.
  • FIG. 1 is a schematic flowchart of a method for loading a picture resource according to an embodiment of the present disclosure
  • FIG. 2 is a schematic flowchart of a method for loading a background of a picture according to an embodiment of the present invention
  • FIG. 3 is a schematic flowchart of a method for loading a picture content display according to an embodiment of the present invention
  • FIG. 4 is a schematic structural diagram of an apparatus for loading a picture resource according to an embodiment of the present disclosure
  • FIG. 5 is a schematic structural diagram of a first encapsulation module according to an embodiment of the present disclosure
  • FIG. 6 is still another schematic structural diagram of a first package module according to an embodiment of the present disclosure.
  • Figure 7 shows a block diagram of a computing device for performing a method of loading picture resources in accordance with the present invention
  • Figure 8 illustrates a storage unit for maintaining or carrying program code that implements a method of loading picture assets in accordance with the present invention.
  • This embodiment provides a method for loading a picture resource. As shown in FIG. 1 , the method includes:
  • Step S101 the loader of the picture background setting and the loader of the picture content display are encapsulated into the picture resource loading class.
  • the picture resource generally includes a picture background setting and a picture content display.
  • the background setting and the picture content display need to be loaded, so the loading program of the picture background setting and the loading program of the picture content display are performed.
  • bitmap set mLeakBitmaps is also needed to store the bitmap pixel data Bitmap generated during the loading process in advance.
  • the bitmap set is in mLeakBitmaps.
  • Decode DrawableHelper is as follows:
  • the Background refers to the background of the picture
  • the Image refers to the display of the picture content.
  • the loading method corresponding to the DecodeDrawableHelper.loadBackground is:
  • Step S201 Receive a resource, and determine, according to the resource ID, whether the resource type is a picture resource.
  • the different resource types correspond to different resource IDs.
  • step S202 is performed.
  • step S203 is performed.
  • the resource type includes a picture resource, a color value resource, and an XML resource.
  • the picture resource may include: a normal picture and a point nine picture; and the color value resource includes a color value type such as RGB8, RGB4, and the like.
  • step S202 it is determined whether the background of the picture is loaded. When it is determined that the background of the picture is not loaded, step S204 is performed. When the background of the picture has been loaded, step S205 is performed.
  • Step S203 When it is determined that the resource is a color value resource and an XML resource, generate a corresponding bitmap object according to the color value resource and the resource ID corresponding to the XML; and assign the corresponding bitmap object to the view that needs to display the background.
  • the corresponding bitmap object Drawable is obtained by the Resources.getDrawable(resId) method, and the bitmap object Drawable is assigned to the view View, so that the view View can display the corresponding resource.
  • the resId represents a resource ID.
  • the method further includes:
  • the View calls the setBackground(drawable) method to obtain the bitmap object Drawable, and uses the bitmap object Drawable to assign a background, thereby displaying the corresponding resource;
  • the setBackgroundDrawable(drawable) method to get the bitmap object Drawable, and uses the bitmap object Drawable to assign the background to display the corresponding resources.
  • the background referred to herein refers to the display background of the display color value resource and the XML resource, which is different from the background image.
  • Step S204 obtaining a view view size and a size of the background of the image to be displayed, determining whether the background of the picture needs to be compressed according to the view size; when the picture background needs to be compressed, performing step 206; when the picture background does not need When compressing, step 207 is performed.
  • the size of the background of the image is obtained in this step in order to reduce the memory occupation.
  • the specific implementation manner is: setting whether to obtain only the boundary inJustDecodeBounds value is true, when set to true, in the process of loading the background of the image, only Get the length, width, and height of the background of the image without actually parsing the background of the image. After reading the size of the background of the image, set the value of inJustDecodeBounds to false.
  • Step S205 when it is determined that the background of the picture has been loaded, the first bitmap pixel data Bitmap of the background of the picture is called from the bitmap set mLeakBitmaps.
  • one pixel in the first bitmap pixel data Bitmap is set to occupy 2 bytes, for example,
  • the loading format of the first bitmap pixel data Bitmap can be set to Bitmap.Config.RGB_565, and the Bitmap.Config.RGB_565 occupies 2 bytes.
  • Step S206 compressing the background of the picture according to the first compression ratio, and compressing the background of the picture, and generating a first bitmap pixel data Bitmap of the background of the picture according to the background of the compressed picture; wherein the first compression ratio is based on The background size and view size of the image are calculated.
  • Step S207 generating a first bitmap pixel data Bitmap of the background of the picture according to the current picture background.
  • Step S208 after the first bitmap pixel data Bitmap of the background of the picture is generated, buffering the first bitmap pixel data Bitmap into the bitmap set mLeakBitmaps, and determining whether the background of the picture is a point nine map, such as determining When the picture background is a normal picture, step S209 is performed; if the picture is determined to be a point nine picture, step S210 is performed.
  • Step S209 when it is determined that the background of the picture is a normal picture, generating a first bitmap object Drawable according to the first bitmap pixel data; assigning the first bitmap object Drawable to a view View that needs to display a background of the picture, So that the view View can display the background of the picture.
  • Step S210 when it is determined that the background of the picture is a point nine map, deleting the generated first bitmap pixel data Bitmap; searching and parsing the picture background resource by using the resource ID to generate a second bitmap object Drawable;
  • the binary bitmap object Drawable is assigned to the view View that needs to display the background of the image.
  • the point nine map is a picture type specially supported by the Android system, if it is a point nine map, the repeated content is filled according to the preset horizontal and vertical iteration area in the picture when drawing.
  • the first bitmap object Drawable is obtained by directly generating the first bitmap object Drawable according to the first bitmap pixel data Bitmap, the attribute of the point nine map is overwritten. Therefore, when the background of the picture is a nine-point map, the generated first bitmap pixel data Bitmap needs to be deleted.
  • the resource loading class can also be added as a unit, and an exception catching is added.
  • OOM Out of Memorty exception
  • the Try, Catch parcel captures an Out of Memorty exception (OOM, Out Of Memorty), and when an exception occurs, the current load is discarded.
  • OOM Out of Memorty exception
  • the loading method corresponding to the DecodeDrawableHelper.loadImage is:
  • Step S301 receiving a resource, and determining a resource type according to the resource ID.
  • the different resource types correspond to different resource IDs.
  • step S302 is performed.
  • step S303 is performed.
  • the resource type includes a picture resource, a color value resource, and an XML resource.
  • the picture resource may include: a normal picture and a point nine picture; and the color value resource includes a color value type such as RGB8, RGB4, and the like.
  • step S302 it is determined whether the picture content display is loaded. When it is determined that the picture content display is not loaded, step S304 is performed. When the picture content display has been loaded, step S305 is performed.
  • Step S303 when it is determined that the resource is a color value resource and an XML resource, generate a corresponding bitmap object according to the color value resource and the resource ID corresponding to the XML.
  • the corresponding bitmap object Drawable is obtained through the Resources.getDrawable(resId) method, and the The ImageView.setImageDrawable method assigns a bitmap object to a view that requires content display so that the view View can display the corresponding resource.
  • the resId represents a resource ID.
  • step S304 the size of the view and the size of the image content to be displayed are obtained, and whether the image content display needs to be compressed according to the view size; when the image content display needs to be compressed, step 306 is performed; When the content display does not require compression, step 307 is performed.
  • the size of the image content display is obtained in this step in order to reduce the memory occupation.
  • the specific implementation manner is: setting whether to obtain only the boundary inJustDecodeBounds is true, when set to true, only the process of loading the background of the image is obtained. Long, wide, and high information displayed in the image content, without actually parsing the image content display. After reading the size of the image content display, set inJustDecodeBounds to false.
  • Step S305 when it is determined that the picture content display has been loaded, the second bitmap pixel data Bitmap displayed by the picture content is called from the bitmap set mLeakBitmaps.
  • one pixel of the second bitmap pixel data Bitmap is set to occupy 2 bytes, for example, loading of the second bitmap pixel data Bitmap.
  • the format can be Bitmap.Config.RGB_565, and the Bitmap.Config.RGB_565 occupies 2 bytes.
  • Step S306 compressing the background of the picture according to the second compression ratio, and compressing the picture content display, and generating a second bitmap pixel data Bitmap displayed by the picture content according to the compressed picture content; wherein the second compression The scale is calculated based on the image display size and view size.
  • Step S307 generating a second bitmap pixel data Bitmap displayed by the picture content according to the current picture content.
  • Step S308 after the second bitmap pixel data Bitmap displayed by the picture content is generated, the second bitmap pixel data Bitmap is buffered into the bitmap set, and it is determined whether the picture content display is a point nine map, such as determining When the picture content is displayed as a normal picture, step S309 is performed; if it is determined that the picture content is displayed as a point nine figure, step S310 is performed.
  • Step S309 when it is determined that the picture content is displayed as a normal picture, generating a third bitmap object Drawable according to the second bitmap pixel data Bitmap; assigning the third bitmap object Drawable to the display of the content of the picture to be displayed. View View so that the View View can display the image content display.
  • Step S310 when it is determined that the picture content is displayed as a point nine map, deleting the generated second bitmap pixel data Bitmap; searching and parsing the picture content display resource by using the resource ID to generate a fourth bitmap object;
  • the fourth bitmap object Drawable is assigned to the view View that needs to display the image content display.
  • the point nine map is a picture type specially supported by the Android system, if it is a point nine map, the repeated content is filled according to the preset horizontal and vertical iteration area in the picture when drawing.
  • the third bitmap object Drawable is obtained by directly generating the third bitmap object Drawable according to the second bitmap pixel data Bitmap, the attribute of the point nine map is overwritten. Therefore, when the picture content display is a nine-point map, the generated second bitmap pixel data Bitmap needs to be deleted.
  • the memory management can be controlled to avoid application crash caused by insufficient memory.
  • the loader and the image content display loader are set in the background of the image.
  • the program releaseSrc() that collects the image resource is further encapsulated into the image resource loading class, and the program for recovering the image resource shares a resource management interface with the program loaded by the image resource.
  • the program releaseSrc() for reclaiming the picture resource is specifically implemented as follows:
  • the resource recovery program is called to execute the recycle by the resource management interface to recover the useless bitmap data of the first bitmap pixel and the bitmap in the bitmap set mLeakBitmaps.
  • One bitmap pixel data Bitmap is specifically when the picture on the interface is deleted.
  • Step S102 loading a resource management interface for the picture resource loading class.
  • the image resource loading interface IDecodeDrawableHelper needs to be encapsulated for the image resource loading class, and the image is needed.
  • the image resource loading program in the resource loading class may be called to load the image resource through the resource management interface, or the image resource recycling program may be invoked through the resource management interface to the image resource. Recycle. In this way, the same resource management interface can be used to realize picture background setting, picture content display and image resource recycling, thereby realizing unified management of image resource loading and recycling.
  • getHelper which refers to the resource loading method under the loaded loading class
  • releaseSrc which refers to the method of reclaiming resources
  • Step S103 Call the corresponding picture resource loading class through the resource management interface, and load the corresponding picture background and the picture content display.
  • the image resource loading method and the image resource recovery method described in step S102 can be applied to any View, ViewGroup, and base class BaseActivity, taking the BaseActivity of the Activity in a browser as an example.
  • the specific implementation of the method of resource loading and the method of recovering image resources is as follows:
  • the resource recycling method releaseSrc is used to recover the image resource, and the program of the calling process is implemented as follows:
  • the embodiment further provides a device for loading a picture resource.
  • the device includes: a first encapsulating module 41, a second encapsulating module 42, a calling module 43, and a storage module 44;
  • the first encapsulation module 41 is configured to encapsulate a loading program of a picture background setting and a loading program for displaying a picture content into a picture resource loading class, where the picture resource generally includes a picture background setting and a picture content display, and the loading picture is loaded.
  • the picture resource generally includes a picture background setting and a picture content display
  • the loading picture is loaded.
  • the background setting and the image content display need to be loaded. Therefore, the loading program of the image background setting and the loading program of the image content display are packaged into the image resource loading class Decode DrawableHelper, which is convenient for unified loading management.
  • the bitmap set mLeakBitmaps needs to be established, and the storage module 44 can load the process.
  • the bitmap pixel data Bitmap generated in the bitmap is stored in the pre-established bitmap set mLeakBitmaps.
  • the first encapsulating module 41 encapsulates the loading program of the picture background setting and the loading program of the picture content display into the image resource loading class Decode DrawableHelper.
  • the implementation program is as follows:
  • the Background refers to the background of the picture
  • the Image refers to the display of the picture content.
  • the first encapsulating module 41 includes: a first determining module 51, a first calling module 52, a first generating module 53, a second determining module 54, a first assigning module 55, and a first deleting module. 56, a second generation module 57 and a first capture module 58; wherein
  • the first determining module 51 is configured to receive a resource, and determine a resource type according to the resource ID.
  • the resource type can be determined according to the resource ID.
  • the resource type includes a picture resource, a color value resource, and an XML resource.
  • the picture resource may include: a normal picture and a point nine picture; and the color value resource includes a color value type such as RGB8, RGB4, and the like.
  • the first determining module 51 determines that the resource is a background resource of the picture, it is also required to determine whether the background of the picture is loaded. When it is determined that the background of the picture is not loaded, the size of the view that needs to be displayed and the size of the background of the picture are obtained. The view size determines whether the picture background needs compression. When it is determined that the picture background needs to be compressed, the picture background is compressed according to the first compression ratio. Here, the first determining module 51 obtains the size of the background of the picture in order to reduce the memory occupation.
  • the specific implementation manner is: the first determining module 51 sets whether to obtain only the boundary inJustDecodeBounds value is true, when set to true, the background image is loaded. In the process, only the length, width and height of the background of the picture will be obtained. Information without going to really parse the picture background. When the first determining module 51 reads the size of the background of the picture, set inJustDecodeBounds to false.
  • the first calling module 52 is configured to invoke the first bitmap pixel data of the background of the picture from the bitmap set mLeakBitmaps. Bitmap.
  • one pixel in the first bitmap pixel data Bitmap is set to occupy 2 bytes, for example, loading of the first bitmap pixel data Bitmap.
  • the format can be Bitmap.Config.RGB_565, and the Bitmap.Config.RGB_565 occupies 2 bytes.
  • the first generating module 53 is further configured to generate a first bitmap pixel data Bitmap of the picture background according to the current picture background.
  • the storage module 44 caches the first bitmap pixel data Bitmap into the bitmap set mLeakBitmaps. .
  • the second determining module 54 is configured to determine whether the background of the image is a point nine map, such as determining When the background of the picture is a normal picture, the first bitmap object Drawable is generated according to the first bitmap pixel data Bitmap, and the first assignment module 55 assigns the first bitmap object Drawable to the image to be displayed.
  • the View view of the background so that the View View can display the background of the image.
  • the first deleting module 56 is configured to delete the generated first bitmap pixel data Bitmap; the first generating module 53 further uses The first bitmap module 55 is further configured to assign the second bitmap object to the view View that needs to display the background of the image, by using the resource ID to find and parse the picture background resource.
  • the point nine map is a picture type specially supported by the Android system, if it is a point nine map, the repeated content is filled according to the preset horizontal and vertical iteration area in the picture when drawing.
  • the first bitmap object Drawable is obtained directly according to the first bitmap pixel data Bitmap to the first bitmap object Drawable, the attributes of the nine maps are overwritten. Therefore, when the background of the picture is a nine-point map, the generated first bitmap pixel data Bitmap needs to be deleted.
  • the second generating module 57 is further configured to generate a corresponding bit according to the color value resource and the resource ID corresponding to the XML.
  • the first evaluation module 55 is further configured to assign a corresponding bitmap object to a view that needs to display a background.
  • the second generating module 57 acquires a corresponding bitmap object Drawable by using a Resources.getDrawable(resId) manner, where the first The assignment module 55 assigns the bitmap object Drawable to the view View so that the view View can display the corresponding resource.
  • the resId represents a resource ID.
  • the first determining module 51 is further configured to: determine whether the SDK_INT in the system is greater than 16, when determining that the SDK_INT is greater than or equal to 16 , View calls the setBackground (drawable) method to get the bitmap object Drawable, using bitmap objects Drawable assigns a background to display the corresponding resource; when it is determined that the SDK_INT is less than 16, View calls the setBackgroundDrawable) (drawable) method to obtain the bitmap object Drawable, and uses the bitmap object Drawable to assign a background to display the corresponding resource.
  • the background referred to herein refers to the display background of the display color value resource and the XML resource, which is different from the background image.
  • the first encapsulating module 41 further includes: a first capturing module 58 for the resource loading class as a unit, adding an exception capturing, through Try The Catch package catches an Out of Memorty (OOM) exception.
  • OOM Out of Memorty
  • the first encapsulating module 41 further includes: a third determining module 61, a second calling module 62, a third generating module 63, and a a determining module 64, a second assigning module 65, a second deleting module 66, a fourth generating module 67, and a second capturing module 68;
  • the third determining module 61 is configured to receive a resource, and determine a resource type according to the resource ID.
  • different resource types correspond to different resource IDs, so the resource type can be determined according to the resource ID.
  • the third determining module 61 determines, according to the resource ID, that the resource is a picture content display resource, determines whether the picture content display is loaded, and determines that the picture content display is not loaded; and obtains a view view that needs to be displayed.
  • the size and the size of the picture content display determining whether the picture content display needs to be compressed according to the view size; when it is determined that the picture content display needs to be compressed, compressing the picture content display according to the second compression ratio, and displaying the picture content
  • the second bitmap pixel data Bitmap displayed on the image content is generated according to the compressed image content; wherein the second compression ratio is calculated according to the image content display size and the view size.
  • the third determining module 61 obtains the size of the picture content display in order to reduce the memory occupation.
  • the specific implementation manner is: setting whether to obtain only the boundary inJustDecodeBounds value is true, when set to true, the process of loading the picture background In the middle, only the length, width, and height information of the image content display will be obtained, and the image content display will not be truly resolved. After reading the size of the image content display, set the value of inJustDecodeBounds to false.
  • the second calling module 62 is configured to invoke the second bitmap pixel data Bitmap of the picture content display from the bitmap set mLeakBitmaps.
  • one pixel of the second bitmap pixel data Bitmap is set to occupy 2 bytes, for example, loading of the second bitmap pixel data Bitmap.
  • the format can be Bitmap.Config.RGB_565, and the Bitmap.Config.RGB_565 occupies 2 bytes.
  • the third generating module 63 displays a second bitmap pixel data Bitmap of the picture background according to the current picture content.
  • the third generation module 63 After the third generation module 63 generates the second bitmap pixel data Bitmap displayed according to the current picture content display, the second bitmap pixel data Bitmap is buffered into the bitmap set, the fourth determination.
  • the module 64 is configured to determine whether the picture content is displayed as a point nine figure. If the picture content is determined to be a normal picture, the third generation module 63 is configured to generate a third bit according to the second bitmap pixel data. a drawing object Drawable; the second assigning module 65 assigns the third bitmap object Drawable to a view View that needs to display a picture content display, Enables the View View to display the image content display.
  • the second deleting module 66 is configured to delete the generated second bitmap pixel data Bitmap; the third generating module 63 further The second bitmap function is further configured to assign the fourth bitmap object to the view View that needs to display the display of the image content.
  • the nine-point map is a specially supported image type, if it is a nine-point map, the repeated content is filled according to the preset horizontal and vertical iteration regions in the image.
  • the third bitmap object Drawable is obtained by directly generating the third bitmap object Drawable according to the second bitmap pixel data Bitmap, the attribute of the point nine map is overwritten. Therefore, when the picture content display is a nine-point map, the generated second bitmap pixel data Bitmap needs to be deleted.
  • the fourth generating module 67 is further configured to generate a corresponding bitmap according to the color value resource and the resource ID corresponding to the XML.
  • the second assignment module 65 is further configured to assign a corresponding bitmap object to a view that needs to display a display of the displayed picture content.
  • the fourth generating module 67 acquires a corresponding bitmap object Drawable by using a Resources.getDrawable(resId) manner, and the second assignment The module 65 assigns the bitmap object to the view that needs content display through the ImageView.setImageDrawable method, so that the view View can display the corresponding resource.
  • the resId represents a resource ID.
  • the first encapsulating module 41 further includes: a second capturing module 68, where the second capturing module 68 is used for the resource loading class, adding an exception capturing, through Try The Catch package captures insufficient OOM exceptions. When an exception occurs, it abandons loading the current image resource to avoid system application crashes due to insufficient memory.
  • the memory management can be controlled to avoid application crash caused by insufficient memory, and the first encapsulating module 41 displays the loading program and the image content of the background setting of the image.
  • the program releaseSrc() for recycling the image resource is further encapsulated into the image resource loading class, and the program for recovering the image resource and the program loaded by the image resource are loaded.
  • a resource management interface is shared, and the program releaseSrc() for reclaiming the picture resource is specifically implemented as follows:
  • the recycle is cyclically executed to recover the useless first bitmap pixel data Bitmap and the first bitmap pixel data Bitmap in the bitmap set mLeakBitmaps.
  • the recovery timing is specifically when the picture on the interface is deleted.
  • the second encapsulating module 42 is used to: Loading a class to encapsulate a resource management interface for the image resource.
  • the class encapsulation of the picture resource needs to be loaded.
  • a resource management interface IDecodeDrawableHelper when the image resource needs to be loaded or the image resource needs to be recycled, the image resource loading program in the resource loading class can be called to load the image resource through the resource management interface, or through the resource.
  • the management interface calls the image resource recycling program to recycle the image resources. In this way, the same resource management interface can be used to realize picture background setting, picture content display and image resource recycling, thereby realizing unified management of image resource loading and recycling.
  • the encapsulation process of the resource management interface IDecodeDrawableHelper by the second encapsulation module 42 is as follows:
  • getHelper which refers to the resource loading method under the loaded loading class
  • releaseSrc which refers to the method of reclaiming resources
  • the first encapsulating module 41 encapsulates the loading program of the picture background setting and the loading program of the picture content display into the picture resource loading class; the second encapsulating module 42 encapsulates the resource management interface IDecodeDrawableHelper corresponding to the picture resource loading class. Then, the calling module 43 can call the corresponding picture resource loading class through the resource management interface, and load the corresponding picture background and picture content display. For the specific loading process, refer to the above; or the calling module 43 may also invoke the image resource recycling program by using the resource management interface IDecodeDrawableHelper according to a preset recycling time to recover useless image resources, and the specific recycling resource process may also be referred to. Above.
  • the image resource loading process and the image resource recycling process described above can be applied to any View, ViewGroup, and base class BaseActivity, taking the BaseActivity of the Activity in a browser as an example, the image resource.
  • the specific implementation of the loading process and the process of image resource recycling is as follows:
  • the calling module 43 may call the resource recycling method releaseSrc to recover the image resource, and the calling process is implemented as follows:
  • the invention discloses a method for loading a picture resource.
  • the loading program of the picture background setting and the loading program of the picture content display are encapsulated into the picture resource loading class, and the image resource loading class is encapsulated.
  • the resource management interface includes two loaders: a loader for setting the background of the image and a loader for displaying the image content; when the image resource needs to be loaded, whether the image background is loaded or the image content is loaded.
  • the service layer can call the corresponding image resource loading class through the resource management interface, and load the corresponding image background and the image content display, thereby optimizing the resource loading process and reducing the cost of resource loading.
  • the exceptions that occur during the loading process can be uniformly captured and processed, thereby further reducing the cost of resource loading.
  • the method stores the generated bitmap pixel data in the loading process into the bitmap set by pre-establishing the bitmap set, implements memory management controllable, and encapsulates the resource recovery program into the resource loading class.
  • the recycling opportunity is added in the resource recycling program.
  • the resource recycling program is called through the same resource management interface to timely recycle the useless image resources in the bitmap collection, thereby realizing the resource recovery and controllable. It fundamentally solves the OOM problem caused by the lack of NativeCache. It also realizes unified management of resource loading and recycling.
  • the description is relatively simple, and the relevant parts can be referred to the description of the method embodiment.
  • modules in the devices of the embodiments can be adaptively changed and placed in one or more devices different from the embodiment.
  • the modules or units or components of the embodiments may be combined into one module or unit or component, and further they may be divided into a plurality of sub-modules or sub-units or sub-components.
  • any combination of the features disclosed in the specification, including the accompanying claims, the abstract and the drawings, and any methods so disclosed, or All processes or units of the device are combined.
  • Each feature disclosed in this specification (including the accompanying claims, the abstract and the drawings) may be replaced by alternative features that provide the same, equivalent or similar purpose.
  • the various component embodiments of the present invention may be implemented in hardware, or in a software module running on one or more processors, or in a combination thereof.
  • a microprocessor or digital signal processor may be used in practice to implement some or all of the functionality of some, or all, of the gateways, proxy servers, systems in accordance with embodiments of the present invention.
  • the invention can also be implemented as a device or device program (e.g., a computer program and a computer program product) for performing some or all of the methods described herein.
  • a program implementing the invention may be stored on a computer readable medium or may be in the form of one or more signals. Such signals may be downloaded from an Internet website, provided on a carrier signal, or provided in any other form.
  • Figure 7 illustrates a computing device that can implement a method of loading picture resources in accordance with the present invention.
  • the computing device conventionally includes a processor 710 and a program product or readable medium in the form of a memory 720.
  • Memory 720 can be an electronic memory such as flash memory, EEPROM (Electrically Erasable Programmable Read Only Memory), EPROM, or ROM.
  • Memory 720 has a memory space 730 for program code 731 for performing any of the method steps described above.
  • storage space 730 for program code may include various program code 731 for implementing various steps in the above methods, respectively.
  • These program codes can be read from or written to one or more program products.
  • These program products include program code carriers such as memory cards.
  • Such a program product is typically a portable or fixed storage unit as described with reference to FIG.
  • the storage unit may have storage segments, storage spaces, and the like that are similarly arranged to memory 720 in the computing device of FIG.
  • the program code can be compressed, for example, in an appropriate form.
  • the storage unit includes readable code 731', ie, code that can be read by a processor, such as 710, that when executed by the computing device causes the computing device to perform various steps in the methods described above .

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Compression Or Coding Systems Of Tv Signals (AREA)
  • Telephone Function (AREA)

Abstract

A method and device for loading a picture resource, and a program and a medium. The method comprises: encapsulating a loading program for picture background setting and a loading program for picture content displaying into a picture resource loading class, and encapsulating a resource management interface for the picture resource loading class, thus the resource management interface comprising two loading programs: the loading program for picture background setting and the loading program for picture content displaying; and when a picture resource needs to be loaded, no matter loading picture background setting or loading picture content displaying, a service layer being able to invoke the corresponding picture resource loading class via the resource management interface, and load the corresponding picture background and the picture content displaying together, thereby optimizing a resource loading process and reducing the cost of resource loading.

Description

加载图片资源的方法、装置、程序及介质Method, device, program and medium for loading picture resources 技术领域Technical field
本发明属于资源加载技术领域,尤其涉及一种加载图片资源的方法、一种加载图片资源的装置、一种计算机程序及一种计算机可读介质。The present invention belongs to the field of resource loading technologies, and in particular, to a method for loading a picture resource, a device for loading a picture resource, a computer program, and a computer readable medium.
背景技术Background technique
随着电子产品的不断发展,智能终端基本上已经成为人手必备的电子产品。目前的主流终端(比如手机、平板电脑等)操作系统包括:Android系统和IOS系统。With the continuous development of electronic products, smart terminals have basically become the necessary electronic products for human hands. Current mainstream terminals (such as mobile phones, tablets, etc.) operating systems include: Android system and IOS system.
图片加载功能是终端操作系统需要具备的基本功能,现有技术中,对于Android系统3.0版本之前的版本,在对图片资源进行加载时,通常利用系统默认的资源加载方法对图片进行加载,但是系统默认的资源加载方法在加载图片资源时无法对图片资源的加载进行统一管理,导致资源加载的过程繁琐,且增加了资源加载的成本。The image loading function is a basic function required by the terminal operating system. In the prior art, for the version before the Android system version 3.0, when the image resource is loaded, the default resource loading method is usually used to load the image, but the system is loaded. The default resource loading method cannot uniformly manage the loading of image resources when loading image resources, which leads to cumbersome resource loading and increases the cost of resource loading.
发明内容Summary of the invention
针对现有技术存在的问题,本发明实施例提供了一种加载图片资源的方法、装置、及计算机程序及计算机可读介质,用于解决现有技术中Android系统3.0版本以下的版本,在加载图片资源时,无法对图片资源的加载进行统一管理的技术问题。The present invention provides a method, an apparatus, and a computer program and a computer readable medium for loading a picture resource, which are used to solve the version of the Android system version 3.0 or lower in the prior art, which is loaded in the prior art. When the picture resource is used, there is a technical problem that the image resource cannot be uniformly managed.
本发明公开了一种加载图片资源的方法,所述方法包括:将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中;为所述图片资源加载类封装一资源管理接口;通过所述资源管理接口调用相应的图片资源加载类,加载相应的图片背景及图片内容显示。The invention discloses a method for loading a picture resource, the method comprising: encapsulating a loading program of a picture background setting and a loading program for displaying a picture content into a picture resource loading class; loading a class encapsulation and resource management for the picture resource An interface is configured to invoke a corresponding image resource loading class by using the resource management interface, and load a corresponding image background and a picture content display.
上述方案中,当对所述图片资源进行加载后,所述方法还包括:将加载过程中生成的位图像素数据存入预先建立的位图集合中。In the above solution, after loading the picture resource, the method further includes: storing the bitmap pixel data generated during the loading process into a pre-established bitmap set.
上述方案中,将加载过程中生成的位图像素数据存入所述位图集合中后,所述方法还包括:In the above solution, after the bitmap pixel data generated in the loading process is stored in the bitmap set, the method further includes:
按照预设的回收时机通过所述资源管理接口调用图片资源回收程序,回收无用的图片资源。The image resource recycling program is called through the resource management interface according to a preset recycling timing, and the useless image resource is recovered.
上述方案中,所述回收时机具体为界面上的图片被删除时。In the above solution, the recovery timing is specifically when the picture on the interface is deleted.
上述方案中,当所述图片资源加载为图片背景设置的加载时,加载方法具体包括:当确定所述图片背景未加载过时,获取需要显示的视图尺寸,根据所述视图尺寸判断所述图片背景是否需要压缩;当确定所述图片背景需要压缩时,根据第一压缩比例压缩所述图片背景;In the above solution, when the image resource is loaded and loaded as a background setting of the image, the loading method specifically includes: when it is determined that the background of the image is not loaded, obtaining a view size to be displayed, and determining the background of the image according to the view size. Whether compression is required; when it is determined that the background of the picture needs to be compressed, the background of the picture is compressed according to a first compression ratio;
根据压缩后的图片背景生成所述图片背景的第一位图像素数据;当确定所述图片背景为普通图片时,根据所述第一位图像素数据生成第一位图对象;将所述第一位图对象赋值 给需要显示图片背景的视图。Generating a first bitmap pixel data of the background of the image according to the compressed background of the image; and when determining that the background of the image is a normal image, generating a first bitmap object according to the pixel data of the first bitmap; One graph object assignment Give a view of the background of the image you want to display.
上述方案中,当确定所述图片背景不需要压缩时,所述方法还包括:根据当前的图片背景生成所述图片背景的第一位图像素数据。In the above solution, when it is determined that the background of the picture does not need to be compressed, the method further includes: generating first bitmap pixel data of the picture background according to the current picture background.
上述方案中,在加载过程中,所述方法还包括:捕捉内存不足异常,当捕捉到异常时,放弃加载当前的图片背景资源。In the above solution, during the loading process, the method further includes: capturing an insufficient memory exception, and when the abnormality is caught, abandoning loading the current picture background resource.
上述方案中,当确定所述图片背景已加载过,所述方法还包括:从位图集合中调用所述图片背景的第一位图像素数据。In the above solution, when it is determined that the background of the picture has been loaded, the method further includes: calling the first bitmap pixel data of the background of the picture from the set of bitmaps.
上述方案中,当确定所述图片背景为点九图时,所述方法还包括:删除生成的所述第一位图像素数据;利用资源ID查找并解析图片背景资源,生成第二位图对象;将所述第二位图对象赋值给需要显示图片背景的视图。In the above solution, when it is determined that the background of the picture is a point nine figure, the method further includes: deleting the generated first bitmap pixel data; searching and parsing the picture background resource by using the resource ID, and generating the second bitmap object. The second bitmap object is assigned to the view that needs to display the background of the image.
上述方案中,在加载图片资源之前,所述方法还包括:根据资源ID判断资源类型;当确定所述资源类型为图片资源时,通过所述资源管理接口调用相应的图片资源加载类,加载相应的图片资源。In the above solution, before loading the picture resource, the method further includes: determining a resource type according to the resource ID; and when determining that the resource type is a picture resource, calling the corresponding picture resource loading class by using the resource management interface, and loading the corresponding Picture resources.
上述方案中,所述资源类型包括:图片资源、色值资源及XML资源。In the foregoing solution, the resource types include: a picture resource, a color value resource, and an XML resource.
上述方案中,当确定所述资源类型为色值资源及XML文件资源时,所述方法还包括:In the above solution, when the resource type is determined to be a color value resource and an XML file resource, the method further includes:
利用所述资源ID查找并解析相应的资源,生成相应的位图对象;将相应的位图对象赋值给需要显示所述图片背景的视图。The resource ID is used to find and parse the corresponding resource, and a corresponding bitmap object is generated; and the corresponding bitmap object is assigned to the view that needs to display the background of the image.
上述方案中,当所述图片资源加载为图片内容显示的加载时,加载方法具体包括:In the above solution, when the image resource is loaded and loaded for the display of the image content, the loading method specifically includes:
当确定所述图片内容显示未加载过时,获取需要显示的视图尺寸,根据所述视图尺寸判断所述图片内容显示是否需要压缩;当确定所述图片内容显示需要压缩时,根据第二压缩比例压缩所述图片内容显示;根据压缩后的图片内容显示生成所述图片内容显示的第二位图像素数据;当确定所述图片内容显示不是点九图时,根据所述第二位图像素数据生成第三位图对象;When it is determined that the picture content display is not loaded, obtaining a view size that needs to be displayed, determining, according to the view size, whether the picture content display needs to be compressed; and when determining that the picture content display needs to be compressed, compressing according to a second compression ratio Displaying the picture content; generating second bitmap pixel data displayed by the picture content according to the compressed picture content; and when determining that the picture content display is not a point nine figure, generating according to the second bitmap pixel data Third bitmap object;
将所述第三位图对象赋值给需要显示图片内容显示的视图。The third bitmap object is assigned to a view that needs to display the display of the image content.
本发明还公开一种加载图片资源的装置,所述装置包括:第一封装模块,用于将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中;第二封装模块,用于为所述图片资源加载类封装一资源管理接口;调用模块,用于通过所述资源管理接口调用相应的图片资源加载类,加载相应的图片背景及图片内容显示。The invention also discloses an apparatus for loading a picture resource, the apparatus comprising: a first encapsulating module, configured to encapsulate a loading program of a picture background setting and a loading program of a picture content display into a picture resource loading class; the second encapsulating module And the method for loading a class to encapsulate a resource management interface for the image resource; the calling module is configured to invoke a corresponding image resource loading class by using the resource management interface, and load the corresponding image background and image content display.
上述方案中,所述装置还包括储存模块,当对所述图片资源进行加载后,所述储存模块用于:将加载过程中生成的位图像素数据存入预先建立的位图集合中。In the above solution, the device further includes a storage module, and after loading the picture resource, the storage module is configured to: store the bitmap pixel data generated during the loading process into a pre-established bitmap set.
上述方案中,所述调用模块还用于按照预设的回收时机通过所述资源管理接口调用图片资源回收程序,回收无用的图片资源。In the above solution, the calling module is further configured to invoke a picture resource recovery program through the resource management interface according to a preset collection timing to recover useless picture resources.
上述方案中,所述回收时机具体为界面上的图片被删除时。In the above solution, the recovery timing is specifically when the picture on the interface is deleted.
上述方案中,当所述图片资源加载为图片背景设置的加载时,所述第一封装模块包括:In the foregoing solution, when the picture resource is loaded and loaded as a background setting of the picture, the first encapsulating module includes:
第一确定模块,用于确定当所述图片背景未加载过时,获取需要显示的视图尺寸,根 据所述视图尺寸判断所述图片背景是否需要压缩;当确定当所述图片背景需要压缩时,根据第一压缩比例压缩所述图片背景;第一生成模块,用于根据压缩后的图片背景生成所述图片背景的第一位图像素数据;第二确定模块,用于确定所述图片背景为普通图片时,根据所述第一位图像素数据生成第一位图对象;第一赋值模块,用于将所述第一位图对象赋值给需要显示图片背景的视图。a first determining module, configured to determine a view size to be displayed when the background of the picture is not loaded, and a root Determining whether the background of the picture needs compression according to the view size; determining that the picture background is compressed according to the first compression ratio when the background of the picture needs to be compressed; and the first generating module, configured to generate the background according to the compressed picture a first bitmap pixel data of the background of the picture; a second determining module, configured to: when determining that the background of the picture is a normal picture, generate a first bitmap object according to the pixel data of the first bitmap; the first assignment module, And is used to assign the first bitmap object to a view that needs to display a background of the picture.
上述方案中,当所述第一确定模块确定所述图片背景不需要压缩时,所述第一生成模块还用于:根据当前的图片背景生成所述图片背景的第一位图像素数据。In the above solution, when the first determining module determines that the background of the picture does not need to be compressed, the first generating module is further configured to: generate first bitmap pixel data of the picture background according to the current picture background.
上述方案中,所述第一封装模块还包括:第一捕捉模块,用于在加载过程中捕捉内存不足异常,当捕捉到异常时,放弃加载所述图片背景资源。In the above solution, the first encapsulating module further includes: a first capturing module, configured to capture an out of memory exception during the loading process, and abandon loading the background resource of the image when an abnormality is captured.
上述方案中,所述第一封装模块还包括:第一调用模块,当所述第一确定模块确定所述图片背景已加载过时,所述第一调用模块用于从位图集合中调用所述图片背景的第一位图像素数据。In the above solution, the first encapsulating module further includes: a first calling module, when the first determining module determines that the background of the picture has been loaded, the first calling module is configured to invoke the The first bitmap pixel data of the background of the picture.
上述方案中,所述第一封装模块还包括:第一删除模块;当所述第二确定模块确定所述图片背景为点九图时,所述第一删除模块用于删除生成的所述第一位图像素数据;所述第一生成模块还用于利用资源ID查找并解析图片背景资源,生成第二位图对象;所述第一赋值模块还用于将所述第二位图对象赋值给需要显示图片背景的视图。In the above solution, the first encapsulating module further includes: a first deleting module; when the second determining module determines that the background of the picture is a point nine figure, the first deleting module is configured to delete the generated a bitmap pixel data; the first generation module is further configured to: find and parse the picture background resource by using the resource ID to generate a second bitmap object; the first assignment module is further configured to assign the second bitmap object Give a view of the background of the image you want to display.
上述方案中,在加载图片资源之前,所述第一确定模块还用于根据资源ID判断资源类型;当确定当所述资源类型为图片资源时,所述调用模块用于通过所述资源管理接口调用相应的图片资源加载类,加载相应的图片资源。In the above solution, before the loading of the picture resource, the first determining module is further configured to determine the resource type according to the resource ID; when it is determined that the resource type is a picture resource, the calling module is configured to use the resource management interface Call the corresponding image resource to load the class and load the corresponding image resource.
上述方案中,所述资源类型包括:图片资源、色值资源及XML资源。In the foregoing solution, the resource types include: a picture resource, a color value resource, and an XML resource.
上述方案中,所述第一封装模块还包括:第二生成模块;其中,当所述第一确定模块确定当所述资源类型为色值资源及XML文件资源时,所述第二生成模块用于利用所述资源ID查找并解析相应的资源,生成相应的位图对象;所述第一赋值模块还用于将相应的位图对象赋值给需要显示图片背景的视图。In the above solution, the first encapsulating module further includes: a second generating module; wherein, when the first determining module determines that the resource type is a color value resource and an XML file resource, the second generating module uses The corresponding resource is searched and parsed by using the resource ID to generate a corresponding bitmap object; the first assignment module is further configured to assign a corresponding bitmap object to a view that needs to display a background of the image.
上述方案中,当所述图片资源加载为图片内容显示的加载时,所述第一封装模块还包括:In the above solution, when the picture resource is loaded as a loading of the picture content display, the first package module further includes:
第三确定模块,用于确定所述图片内容显示未加载过时,获取需要显示的视图尺寸,根据所述视图尺寸判断所述图片内容显示是否需要压缩;当所述图片内容显示需要压缩时,根据第二压缩比例压缩所述图片内容显示;第三生成模块,用于根据压缩后的图片内容显示生成所述图片内容显示的第二位图像素数据;a third determining module, configured to determine that the picture content display is not loaded, obtain a view size that needs to be displayed, and determine, according to the view size, whether the picture content display needs to be compressed; when the picture content display needs to be compressed, according to The second compression ratio compresses the picture content display; the third generation module is configured to generate second bitmap pixel data displayed by the picture content according to the compressed picture content display;
第四确定模块,用于确定当所述图片内容显示为普通图片时,根据所述第二位图像素数据生成第三位图对象;第二赋值模块,用于将所述第三位图对象赋值给需要显示图片内容显示的视图。a fourth determining module, configured to: when the picture content is displayed as a normal picture, generate a third bitmap object according to the second bitmap pixel data; and a second assignment module, configured to use the third bitmap object Assign to the view that needs to display the display of the image content.
本发明还提供了一种计算机程序,包括计算机可读代码,当所述可读代码在计算设备上运行时,导致所述计算设备执行根据本发明实施例中的任一所述的加载图片资源的方法。 The invention also provides a computer program comprising computer readable code, when the readable code is run on a computing device, causing the computing device to perform a load picture resource according to any of the embodiments of the invention Methods.
本发明实施例还提供了一种计算机可读介质,其中存储了如本发明实施例所述的程序。The embodiment of the invention further provides a computer readable medium, wherein the program according to the embodiment of the invention is stored.
本发明提供了一种加载图片资源的方法及装置,所述方法包括:将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中,并为所述图片资源加载类封装一资源管理接口;这样,所述资源管理接口下就包括两种加载程序:图片背景设置的加载程序及图片内容显示的加载程序;在需要加载图片资源时,无论是加载图片背景设置还是加载图片内容显示,业务层都可以通过所述资源管理接口调用相应的图片资源加载类,一并加载相应的图片背景及所述图片内容显示,从而优化了资源加载过程,降低了资源加载的成本。The present invention provides a method and an apparatus for loading a picture resource, the method comprising: encapsulating a loader for setting a background of a picture and a loader for displaying a picture content into a picture resource loading class, and loading a class package for the picture resource A resource management interface; in this way, the resource management interface includes two loaders: a loader for setting the background of the image and a loader for displaying the image content; when the image resource needs to be loaded, whether the background image is loaded or the image is loaded The content shows that the service layer can call the corresponding image resource loading class through the resource management interface, and load the corresponding image background and the image content display, thereby optimizing the resource loading process and reducing the cost of resource loading.
附图说明DRAWINGS
图1为本发明实施例提供的加载图片资源的方法流程示意图;FIG. 1 is a schematic flowchart of a method for loading a picture resource according to an embodiment of the present disclosure;
图2为本发明实施例提供的加载图片背景的方法流程示意图;2 is a schematic flowchart of a method for loading a background of a picture according to an embodiment of the present invention;
图3为本发明实施例提供的加载图片内容显示的方法流程示意图;FIG. 3 is a schematic flowchart of a method for loading a picture content display according to an embodiment of the present invention;
图4为本发明实施例提供的加载图片资源的装置结构示意图;FIG. 4 is a schematic structural diagram of an apparatus for loading a picture resource according to an embodiment of the present disclosure;
图5为本发明实施例提供的第一封装模块的结构示意图;FIG. 5 is a schematic structural diagram of a first encapsulation module according to an embodiment of the present disclosure;
图6为本发明实施例提供的第一封装模块的又一结构示意图;FIG. 6 is still another schematic structural diagram of a first package module according to an embodiment of the present disclosure;
图7示出了用于执行根据本发明的加载图片资源的方法的计算设备的框图;Figure 7 shows a block diagram of a computing device for performing a method of loading picture resources in accordance with the present invention;
图8示出了用于保持或者携带实现根据本发明的加载图片资源的方法的程序代码的存储单元。Figure 8 illustrates a storage unit for maintaining or carrying program code that implements a method of loading picture assets in accordance with the present invention.
具体实施方式detailed description
为了使本申请所属技术领域中的技术人员更清楚地理解本申请,下面将参照附图更详细地描述本公开的示例性实施例。虽然附图中显示了本公开的示例性实施例,然而应当理解,可以以各种形式实现本公开而不应被这里阐述的实施例所限制。相反,提供这些实施例是为了能够更透彻地理解本公开,并且能够将本公开的范围完整的传达给本领域的技术人员。Exemplary embodiments of the present disclosure will be described in more detail below with reference to the drawings in order to make the present invention more clearly understood by those skilled in the art. While the embodiments of the present invention have been shown in the drawings, the embodiments Rather, these embodiments are provided so that this disclosure will be more fully understood and the scope of the disclosure will be fully disclosed.
本实施例提供一种加载图片资源的方法,如图1所示,所述方法包括:This embodiment provides a method for loading a picture resource. As shown in FIG. 1 , the method includes:
步骤S101,将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中。Step S101, the loader of the picture background setting and the loader of the picture content display are encapsulated into the picture resource loading class.
本步骤中,所述图片资源一般包括图片背景设置及图片内容显示,在加载图片资源时,需要对背景设置及图片内容显示进行加载,因此将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类Decode DrawableHelper中,方便统一加载管理。In this step, the picture resource generally includes a picture background setting and a picture content display. When loading the picture resource, the background setting and the picture content display need to be loaded, so the loading program of the picture background setting and the loading program of the picture content display are performed. Encapsulated into the image resource loading class Decode DrawableHelper, convenient for unified loading management.
将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类Decode DrawableHelper中后,还需建立位图集合mLeakBitmaps,以能将加载过程中生成的位图像素数据Bitmap存入预先建立的位图集合mLeakBitmaps中。After the loader of the image background setting and the loader of the image content display are packaged into the image resource loading class Decode DrawableHelper, a bitmap set mLeakBitmaps is also needed to store the bitmap pixel data Bitmap generated during the loading process in advance. The bitmap set is in mLeakBitmaps.
这里,将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类 Decode DrawableHelper的实现程序如下:Here, the loader of the image background setting and the loader of the image content display are encapsulated into a picture resource loading class. The implementation of Decode DrawableHelper is as follows:
DecodeDrawableHelper.loadBackgroundDecodeDrawableHelper.loadBackground
DecodeDrawableHelper.loadImageDecodeDrawableHelper.loadImage
所述Background是指图片背景,所述Image是指图片内容显示。The Background refers to the background of the picture, and the Image refers to the display of the picture content.
具体地,当所述图片资源加载为图片背景设置的加载时,参见图2,所述DecodeDrawableHelper.loadBackground对应的加载方法为:Specifically, when the picture resource is loaded as the loading of the picture background setting, referring to FIG. 2, the loading method corresponding to the DecodeDrawableHelper.loadBackground is:
步骤S201,接收资源,根据资源ID判断资源类型是否为图片资源。Step S201: Receive a resource, and determine, according to the resource ID, whether the resource type is a picture resource.
这里,不同的资源类型对应不同的资源ID,当接收到资源后,根据资源ID确定资源为图片背景资源时,执行步骤S202;当确定资源为色值资源及XML资源时,执行步骤S203。其中,所述资源类型包括图片资源、色值资源及XML资源;所述图片资源可以包括:普通图片及点九图;所述色值资源包括例如RGB8、RGB4等色值类型。Here, the different resource types correspond to different resource IDs. When the resource is determined to be the background resource of the image according to the resource ID, step S202 is performed. When the resource is determined to be the color value resource and the XML resource, step S203 is performed. The resource type includes a picture resource, a color value resource, and an XML resource. The picture resource may include: a normal picture and a point nine picture; and the color value resource includes a color value type such as RGB8, RGB4, and the like.
步骤S202,判断图片背景是否加载过,当确定所述图片背景未加载过时,执行步骤S204。当所述图片背景已加载过时,执行步骤S205。In step S202, it is determined whether the background of the picture is loaded. When it is determined that the background of the picture is not loaded, step S204 is performed. When the background of the picture has been loaded, step S205 is performed.
步骤S203,当确定资源为色值资源及XML资源时,根据所述色值资源及XML对应的资源ID,生成相应的位图对象;将相应的位图对象赋值给需要显示背景的视图。Step S203: When it is determined that the resource is a color value resource and an XML resource, generate a corresponding bitmap object according to the color value resource and the resource ID corresponding to the XML; and assign the corresponding bitmap object to the view that needs to display the background.
具体地,通过Resources.getDrawable(resId)方式获取相应的位图对象Drawable,并将所述位图对象Drawable赋值给视图View,使得视图View可以显示相应的资源。其中,所述resId代指资源ID。Specifically, the corresponding bitmap object Drawable is obtained by the Resources.getDrawable(resId) method, and the bitmap object Drawable is assigned to the view View, so that the view View can display the corresponding resource. Wherein, the resId represents a resource ID.
这里,将所述位图对象Drawable赋值给视图View之前,所述方法还包括:Here, before the bitmap object Drawable is assigned to the view View, the method further includes:
判断系统中的SDK_INT是否大于16,当确定所述SDK_INT大于或等于16时,View调用setBackground(drawable)方法获取位图对象Drawable,利用位图对象Drawable赋值背景,从而显示相应的资源;当确定所述SDK_INT小于16时,View调用setBackgroundDrawable)(drawable)方法获取位图对象Drawable,利用位图对象Drawable赋值背景,从而显示相应的资源。应当注意的是,此处所指的背景是指显示色值资源及XML资源的显示背景,与图片背景不同。Determine whether the SDK_INT in the system is greater than 16. When it is determined that the SDK_INT is greater than or equal to 16, the View calls the setBackground(drawable) method to obtain the bitmap object Drawable, and uses the bitmap object Drawable to assign a background, thereby displaying the corresponding resource; When SDK_INT is less than 16, View calls the setBackgroundDrawable(drawable) method to get the bitmap object Drawable, and uses the bitmap object Drawable to assign the background to display the corresponding resources. It should be noted that the background referred to herein refers to the display background of the display color value resource and the XML resource, which is different from the background image.
步骤S204,获取需要显示的视图View尺寸及图片背景的尺寸,根据所述视图尺寸判断所述图片背景是否需要压缩;当所述图片背景需要压缩时,执行步骤206;当所述图片背景不需要压缩时,执行步骤207。Step S204, obtaining a view view size and a size of the background of the image to be displayed, determining whether the background of the picture needs to be compressed according to the view size; when the picture background needs to be compressed, performing step 206; when the picture background does not need When compressing, step 207 is performed.
这里,在本步骤中获取图片背景的尺寸是为了减少内存占用,具体的实现方式为:设置是否只获取边界inJustDecodeBounds的值为true,当设置为true时,在加载图片背景的过程中,只会获取到图片背景的长、宽、高信息,而不会去真正解析图片背景。读取到图片背景的尺寸后,将inJustDecodeBounds的值设为false。Here, the size of the background of the image is obtained in this step in order to reduce the memory occupation. The specific implementation manner is: setting whether to obtain only the boundary inJustDecodeBounds value is true, when set to true, in the process of loading the background of the image, only Get the length, width, and height of the background of the image without actually parsing the background of the image. After reading the size of the background of the image, set the value of inJustDecodeBounds to false.
步骤S205,当确定所述图片背景已经加载过时,从所述位图集合mLeakBitmaps中调用所述图片背景的第一位图像素数据Bitmap。其中,为了减少所述第一位图像素数据Bitmap的占用内存,本实施例中将第一位图像素数据Bitmap中一个像素设置占用2个字节,例如, 第一位图像素数据Bitmap的加载格式可以设为Bitmap.Config.RGB_565,所述Bitmap.Config.RGB_565占用的字节为2个。Step S205, when it is determined that the background of the picture has been loaded, the first bitmap pixel data Bitmap of the background of the picture is called from the bitmap set mLeakBitmaps. In order to reduce the occupied memory of the first bitmap pixel data Bitmap, in the embodiment, one pixel in the first bitmap pixel data Bitmap is set to occupy 2 bytes, for example, The loading format of the first bitmap pixel data Bitmap can be set to Bitmap.Config.RGB_565, and the Bitmap.Config.RGB_565 occupies 2 bytes.
步骤S206,根据第一压缩比例压缩所述图片背景,将图片背景压缩后,根据压缩后的图片背景生成所述图片背景的第一位图像素数据Bitmap;其中,所述第一压缩比例是根据图片背景尺寸及视图尺寸计算得出的。Step S206, compressing the background of the picture according to the first compression ratio, and compressing the background of the picture, and generating a first bitmap pixel data Bitmap of the background of the picture according to the background of the compressed picture; wherein the first compression ratio is based on The background size and view size of the image are calculated.
步骤S207,根据当前的图片背景生成所述图片背景的第一位图像素数据Bitmap。Step S207, generating a first bitmap pixel data Bitmap of the background of the picture according to the current picture background.
步骤S208,当图片背景的第一位图像素数据Bitmap生成之后,将所述第一位图像素数据Bitmap缓存至位图集合mLeakBitmaps中,并判断所述图片背景是否为点九图,如确定所述图片背景为普通图片时,执行步骤S209;如确定所述图片为点九图时,执行步骤S210。Step S208, after the first bitmap pixel data Bitmap of the background of the picture is generated, buffering the first bitmap pixel data Bitmap into the bitmap set mLeakBitmaps, and determining whether the background of the picture is a point nine map, such as determining When the picture background is a normal picture, step S209 is performed; if the picture is determined to be a point nine picture, step S210 is performed.
步骤S209,当确定所述图片背景为普通图片时,根据所述第一位图像素数据生成第一位图对象Drawable;将所述第一位图对象Drawable赋值给需要显示图片背景的视图View,以使得视图View可以显示图片背景。Step S209, when it is determined that the background of the picture is a normal picture, generating a first bitmap object Drawable according to the first bitmap pixel data; assigning the first bitmap object Drawable to a view View that needs to display a background of the picture, So that the view View can display the background of the picture.
步骤S210,当确定所述图片背景为点九图时,删除生成的所述第一位图像素数据Bitmap;利用资源ID查找并解析图片背景资源,生成第二位图对象Drawable;将所述第二位图对象Drawable赋值给需要显示图片背景的视图View。这里,因点九图是Android系统特殊支持的图片类型,如果是点九图,在绘制的时候会根据图片内的预先设置的横纵迭代区域填充重复内容。这里,如果直接根据第一位图像素数据Bitmap生成第一位图对象Drawable的方式获取第一位图对象Drawable,则会覆盖掉点九图的属性。所以当图片背景是点九图时,则需要删除生成的第一位图像素数据Bitmap。Step S210, when it is determined that the background of the picture is a point nine map, deleting the generated first bitmap pixel data Bitmap; searching and parsing the picture background resource by using the resource ID to generate a second bitmap object Drawable; The binary bitmap object Drawable is assigned to the view View that needs to display the background of the image. Here, because the point nine map is a picture type specially supported by the Android system, if it is a point nine map, the repeated content is filled according to the preset horizontal and vertical iteration area in the picture when drawing. Here, if the first bitmap object Drawable is obtained by directly generating the first bitmap object Drawable according to the first bitmap pixel data Bitmap, the attribute of the point nine map is overwritten. Therefore, when the background of the picture is a nine-point map, the generated first bitmap pixel data Bitmap needs to be deleted.
在加载过程中,为了进一步降低资源加载的成本,还可以资源加载类为单位,添加异常捕捉,通过Try,Catch包裹捕捉内存不足(OOM,Out Of Memorty)异常,当出现异常时,放弃加载当前的图片背景资源,以避免因内存不足导致系统应用崩溃。In the loading process, in order to further reduce the cost of resource loading, the resource loading class can also be added as a unit, and an exception catching is added. The Try, Catch parcel captures an Out of Memorty exception (OOM, Out Of Memorty), and when an exception occurs, the current load is discarded. The background resources of the image to avoid system application crashes due to insufficient memory.
至此,所述图片背景设置的加载过程完成。At this point, the loading process of the picture background setting is completed.
相应地,当所述图片资源加载为图片内容显示的加载时,参见图3,所述DecodeDrawableHelper.loadImage对应的加载方法为:Correspondingly, when the picture resource is loaded for the loading of the picture content display, referring to FIG. 3, the loading method corresponding to the DecodeDrawableHelper.loadImage is:
步骤S301,接收资源,根据资源ID判断资源类型。Step S301, receiving a resource, and determining a resource type according to the resource ID.
这里,不同的资源类型对应不同的资源ID,当接收到资源后,根据资源ID确定资源为图片内容显示资源时,执行步骤S302;当确定资源为色值资源及XML资源时,执行步骤S303。其中,所述资源类型包括图片资源、色值资源及XML资源;所述图片资源可以包括:普通图片及点九图;所述色值资源包括例如RGB8、RGB4等色值类型。Here, the different resource types correspond to different resource IDs. When the resource is determined to be the picture content display resource according to the resource ID, step S302 is performed. When the resource is determined to be the color value resource and the XML resource, step S303 is performed. The resource type includes a picture resource, a color value resource, and an XML resource. The picture resource may include: a normal picture and a point nine picture; and the color value resource includes a color value type such as RGB8, RGB4, and the like.
步骤S302,判断图片内容显示是否加载过,当确定所述图片内容显示未加载过时,执行步骤S304。当所述图片内容显示已加载过时,执行步骤S305。In step S302, it is determined whether the picture content display is loaded. When it is determined that the picture content display is not loaded, step S304 is performed. When the picture content display has been loaded, step S305 is performed.
步骤S303,当确定资源为色值资源及XML资源时,根据所述色值资源及XML对应的资源ID生成相应的位图对象。Step S303, when it is determined that the resource is a color value resource and an XML resource, generate a corresponding bitmap object according to the color value resource and the resource ID corresponding to the XML.
具体地,通过Resources.getDrawable(resId)方式获取相应的位图对象Drawable,通过 ImageView.setImageDrawable方法将位图对象赋值给需要内容显示的视图,使得视图View可以显示相应的资源。其中,所述resId代指资源ID。Specifically, the corresponding bitmap object Drawable is obtained through the Resources.getDrawable(resId) method, and the The ImageView.setImageDrawable method assigns a bitmap object to a view that requires content display so that the view View can display the corresponding resource. Wherein, the resId represents a resource ID.
步骤S304,获取需要显示的视图View尺寸及图片内容显示的尺寸,根据所述视图尺寸判断所述图片内容显示是否需要压缩;当所述图片内容显示需要压缩时,执行步骤306;当所述图片内容显示不需要压缩时,执行步骤307。In step S304, the size of the view and the size of the image content to be displayed are obtained, and whether the image content display needs to be compressed according to the view size; when the image content display needs to be compressed, step 306 is performed; When the content display does not require compression, step 307 is performed.
这里,在本步骤中获取图片内容显示的尺寸是为了减少内存占用,具体的实现方式为:设置是否只获取边界inJustDecodeBounds为true,当设置为true时,在加载图片背景的过程中,只会获取到图片内容显示的长、宽、高信息,而不会去真正解析图片内容显示。读取到图片内容显示的尺寸后,再将inJustDecodeBounds设为false。Here, the size of the image content display is obtained in this step in order to reduce the memory occupation. The specific implementation manner is: setting whether to obtain only the boundary inJustDecodeBounds is true, when set to true, only the process of loading the background of the image is obtained. Long, wide, and high information displayed in the image content, without actually parsing the image content display. After reading the size of the image content display, set inJustDecodeBounds to false.
步骤S305,当确定所述图片内容显示已经加载过时,从所述位图集合mLeakBitmaps中调用所述图片内容显示的第二位图像素数据Bitmap。其中,为了减少所述第二位图像素数据Bitmap的占用内存,本实施例中将第二位图像素数据Bitmap中一个像素设置占用2个字节,例如,第二位图像素数据Bitmap的加载格式可以为Bitmap.Config.RGB_565,所述Bitmap.Config.RGB_565占用的字节为2个。Step S305, when it is determined that the picture content display has been loaded, the second bitmap pixel data Bitmap displayed by the picture content is called from the bitmap set mLeakBitmaps. In order to reduce the occupied memory of the second bitmap pixel data Bitmap, in the embodiment, one pixel of the second bitmap pixel data Bitmap is set to occupy 2 bytes, for example, loading of the second bitmap pixel data Bitmap. The format can be Bitmap.Config.RGB_565, and the Bitmap.Config.RGB_565 occupies 2 bytes.
步骤S306,根据第二压缩比例压缩所述图片背景,将图片内容显示压缩后,根据压缩后的图片内容显示生成所述图片内容显示的第二位图像素数据Bitmap;其中,所述第二压缩比例是根据图片内容显示尺寸及视图尺寸计算得出的。Step S306, compressing the background of the picture according to the second compression ratio, and compressing the picture content display, and generating a second bitmap pixel data Bitmap displayed by the picture content according to the compressed picture content; wherein the second compression The scale is calculated based on the image display size and view size.
步骤S307,根据当前的图片内容显示生成所述图片内容显示的第二位图像素数据Bitmap。Step S307, generating a second bitmap pixel data Bitmap displayed by the picture content according to the current picture content.
步骤S308,当图片内容显示的第二位图像素数据Bitmap生成之后,将所述第二位图像素数据Bitmap缓存至位图集合中,并判断所述图片内容显示是否为点九图,如确定所述图片内容显示为普通图片时,执行步骤S309;如确定所述图片内容显示为点九图时,执行步骤S310。Step S308, after the second bitmap pixel data Bitmap displayed by the picture content is generated, the second bitmap pixel data Bitmap is buffered into the bitmap set, and it is determined whether the picture content display is a point nine map, such as determining When the picture content is displayed as a normal picture, step S309 is performed; if it is determined that the picture content is displayed as a point nine figure, step S310 is performed.
步骤S309,当确定所述图片内容显示为普通图片时,根据所述第二位图像素数据Bitmap生成第三位图对象Drawable;将所述第三位图对象Drawable赋值给需要显示图片内容显示的视图View,以使得视图View可以显示图片内容显示。Step S309, when it is determined that the picture content is displayed as a normal picture, generating a third bitmap object Drawable according to the second bitmap pixel data Bitmap; assigning the third bitmap object Drawable to the display of the content of the picture to be displayed. View View so that the View View can display the image content display.
步骤S310,当确定所述图片内容显示为点九图时,删除生成的所述第二位图像素数据Bitmap;利用资源ID查找并解析图片内容显示资源,生成第四位图对象;将所述第四位图对象Drawable赋值给需要显示图片内容显示的视图View。这里,因点九图是Android系统特殊支持的图片类型,如果是点九图,在绘制的时候会根据图片内的预先设置的横纵迭代区域填充重复内容。这里,如果直接根据第二位图像素数据Bitmap生成第三位图对象Drawable的方式获取第三位图对象Drawable,则会覆盖掉点九图的属性。所以当图片内容显示是点九图时,则需要删除生成的第二位图像素数据Bitmap。Step S310, when it is determined that the picture content is displayed as a point nine map, deleting the generated second bitmap pixel data Bitmap; searching and parsing the picture content display resource by using the resource ID to generate a fourth bitmap object; The fourth bitmap object Drawable is assigned to the view View that needs to display the image content display. Here, because the point nine map is a picture type specially supported by the Android system, if it is a point nine map, the repeated content is filled according to the preset horizontal and vertical iteration area in the picture when drawing. Here, if the third bitmap object Drawable is obtained by directly generating the third bitmap object Drawable according to the second bitmap pixel data Bitmap, the attribute of the point nine map is overwritten. Therefore, when the picture content display is a nine-point map, the generated second bitmap pixel data Bitmap needs to be deleted.
在加载过程中,为了进一步降低资源加载的成本,还可以资源加载类为单位,添加异常捕捉,通过Try,Catch包裹捕捉内存不足(OOM,Out Of Memorty)异常,当捕捉到异常 时,放弃加载当前的图片资源,以避免因内存不足导致系统应用崩溃。In the loading process, in order to further reduce the cost of resource loading, you can also add the exception capture to the resource loading class, and catch the OOM (Out Of Memorty) exception through the Try, Catch package. When you abandon loading the current image resource to avoid system application crashes due to insufficient memory.
至此,所述图片内容显示的加载过程完成。At this point, the loading process of the picture content display is completed.
应当注意的是,本实施例中无论是加载图片背景还是加载图片内容显示时,在资源加载类中,同样的资源只加载一次,有效减少了资源的重复加载,避免因资源重复加载出现内存抖动造成加载过程不稳定的现象。It should be noted that, in the embodiment, whether the background of the loaded image or the content of the loaded image is displayed, in the resource loading class, the same resource is only loaded once, which effectively reduces the repeated loading of the resource, and avoids memory jitter caused by repeated loading of the resource. Causes the loading process to be unstable.
可选地,为了可以回收位图集合mLeakBitmaps中无用的图片资源,实现内存管理的可控,避免因内存不足导致应用崩溃,本实施例在将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中后,还将回收图片资源的程序releaseSrc()封装至所述图片资源加载类中,且所述图片资源回收的程序与所述图片资源加载的程序共用一资源管理接口,所述回收图片资源的程序releaseSrc()具体实现如下:Optionally, in order to recover the useless image resources in the bitmap set mLeakBitmaps, the memory management can be controlled to avoid application crash caused by insufficient memory. In this embodiment, the loader and the image content display loader are set in the background of the image. After being encapsulated into the image resource loading class, the program releaseSrc() that collects the image resource is further encapsulated into the image resource loading class, and the program for recovering the image resource shares a resource management interface with the program loaded by the image resource. The program releaseSrc() for reclaiming the picture resource is specifically implemented as follows:
Figure PCTCN2017117675-appb-000001
Figure PCTCN2017117675-appb-000001
这里,当触发到预设的图片资源的回收时机时,则会通过所述资源管理接口调用资源回收程序循环执行recycle,来回收位图集合mLeakBitmaps中无用的对第一位图像素数据Bitmap及第一位图像素数据Bitmap。其中,所述回收时机具体为界面上的图片被删除时。Here, when the recovery timing of the preset picture resource is triggered, the resource recovery program is called to execute the recycle by the resource management interface to recover the useless bitmap data of the first bitmap pixel and the bitmap in the bitmap set mLeakBitmaps. One bitmap pixel data Bitmap. The recovery timing is specifically when the picture on the interface is deleted.
步骤S102,为所述图片资源加载类封装一资源管理接口。Step S102, loading a resource management interface for the picture resource loading class.
当将图片背景设置的加载程序、图片内容显示的加载程序及图片资源的回收程序封装至图片资源加载类中后,还需为所述图片资源加载类封装一资源管理接口IDecodeDrawableHelper,在需要对图片资源进行加载或对图片资源进行回收时,只需通过所述资源管理接口即可调用资源加载类中的图片资源加载程序对图片资源进行加载,或通过资源管理接口调用图片资源回收程序对图片资源进行回收。这样就可以通过同一个资源管理接口实现图片背景设置、图片内容显示及图片资源回收,从而实现图片资源加载和回收的统一管理。 After the loading program of the picture background setting, the loading program of the picture content display, and the reclaiming program of the picture resource are encapsulated into the image resource loading class, the image resource loading interface IDecodeDrawableHelper needs to be encapsulated for the image resource loading class, and the image is needed. When the resource is loaded or the image resource is reclaimed, the image resource loading program in the resource loading class may be called to load the image resource through the resource management interface, or the image resource recycling program may be invoked through the resource management interface to the image resource. Recycle. In this way, the same resource management interface can be used to realize picture background setting, picture content display and image resource recycling, thereby realizing unified management of image resource loading and recycling.
具体地,所述资源管理接口IDecodeDrawableHelper的封装过程如下:Specifically, the encapsulation process of the resource management interface IDecodeDrawableHelper is as follows:
Public interface IDecodeDrawableHelper{Public interface IDecodeDrawableHelper{
DecodeDrawableHelper getHelper();DecodeDrawableHelper getHelper();
void releaseSrc();Void releaseSrc();
}}
可以看出,在所述资源管理接口IDecodeDrawableHelper下,包含两种方法:第一是getHelper,指获取封装的加载类下的资源加载方法;第二是releaseSrc,指回收资源的方法。It can be seen that under the resource management interface IDecodeDrawableHelper, there are two methods: the first is getHelper, which refers to the resource loading method under the loaded loading class; the second is releaseSrc, which refers to the method of reclaiming resources.
步骤S103,通过所述资源管理接口调用相应的图片资源加载类,加载相应的图片背景及所述图片内容显示。Step S103: Call the corresponding picture resource loading class through the resource management interface, and load the corresponding picture background and the picture content display.
本步骤中,当将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中;且将图片资源加载类对应的资源管理接口IDecodeDrawableHelper封装好之后;即可过所述资源管理接口IDecodeDrawableHelper调用相应的图片资源加载类,加载相应的图片背景及图片内容显示,具体的加载方法可参见步骤201~210及步骤301~310;或者按照预设的回收时机通过所述资源管理接口IDecodeDrawableHelper调用图片资源回收程序,回收无用的图片资源,具体的回收资源方法可参见步骤S102。In this step, when the loading program of the picture background setting and the loading program of the picture content display are encapsulated into the picture resource loading class; and the resource management interface IDecodeDrawableHelper corresponding to the picture resource loading class is encapsulated; The interface IDecodeDrawableHelper invokes the corresponding image resource loading class, and loads the corresponding image background and image content display. For the specific loading method, refer to steps 201-210 and steps 301-310; or pass the resource management interface IDecodeDrawableHelper according to a preset recycling time. The image resource recycling program is called to recover the useless image resource. For the specific method for recycling resources, refer to step S102.
在实际应用中,步骤S102中记载的图片资源加载方法及图片资源回收方法可以适用于任意的View内、ViewGroup内及基类BaseActivity中,以某浏览器中的Activity的基类BaseActivity为例,图片资源加载的方法及图片资源回收的方法的具体实现如下:In an actual application, the image resource loading method and the image resource recovery method described in step S102 can be applied to any View, ViewGroup, and base class BaseActivity, taking the BaseActivity of the Activity in a browser as an example. The specific implementation of the method of resource loading and the method of recovering image resources is as follows:
获取封装的图片资源加载类下的图片资源加载的方法:Get the image resource loading method under the packaged image resource loading class:
Figure PCTCN2017117675-appb-000002
Figure PCTCN2017117675-appb-000002
图片资源回收的方法:Image resource recycling method:
Figure PCTCN2017117675-appb-000003
Figure PCTCN2017117675-appb-000003
在加载过程中,当界面图片被删除,即在Activity onDestroy时调用资源回收方法releaseSrc对图片资源进行回收,调用过程的程序实现如下:During the loading process, when the interface image is deleted, that is, when the activity onDestroy is called, the resource recycling method releaseSrc is used to recover the image resource, and the program of the calling process is implemented as follows:
@Override@Override
protected void onDestroy(){ Protected void onDestroy(){
super.onDestroy();super.onDestroy();
releaseSrc();releaseSrc();
这样在BaseActivity内,以及继承自BaseActivity的类内,加载图片资源的时候只需要通过调用getHelper().loadBackground对图片背景设置进行加载,或者通过调用getHelper.loadImage的方法对图片内容显示进行加载;回收图片资源的时候,只要触发到回收时机即可调用releaseSrc对无用的图片资源进行回收。In the BaseActivity, as well as the class that inherits from BaseActivity, when loading the image resource, you only need to load the image background setting by calling getHelper().loadBackground, or load the image content display by calling getHelper.loadImage; When the image resource is triggered, the releaseSrc can be called to recycle the useless image resources whenever the trigger is triggered.
本实施例还提供一种加载图片资源的装置,如图4所示,所述装置包括:第一封装模块41、第二封装模块42、调用模块43及储存模块44;其中,The embodiment further provides a device for loading a picture resource. As shown in FIG. 4, the device includes: a first encapsulating module 41, a second encapsulating module 42, a calling module 43, and a storage module 44;
所述第一封装模块41用于将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中;其中,所述图片资源一般包括图片背景设置及图片内容显示,在加载图片资源时,需要对背景设置及图片内容显示进行加载,因此将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类Decode DrawableHelper中,方便统一加载管理。The first encapsulation module 41 is configured to encapsulate a loading program of a picture background setting and a loading program for displaying a picture content into a picture resource loading class, where the picture resource generally includes a picture background setting and a picture content display, and the loading picture is loaded. When the resource is used, the background setting and the image content display need to be loaded. Therefore, the loading program of the image background setting and the loading program of the image content display are packaged into the image resource loading class Decode DrawableHelper, which is convenient for unified loading management.
所述第一封装模块41将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类Decode DrawableHelper中后,还需建立位图集合mLeakBitmaps,所述储存模块44以能将加载过程中生成的位图像素数据Bitmap存入预先建立的位图集合mLeakBitmaps中。After the first package module 41 encapsulates the load program of the picture background setting and the load program of the picture content display into the picture resource loading class Decode DrawableHelper, the bitmap set mLeakBitmaps needs to be established, and the storage module 44 can load the process. The bitmap pixel data Bitmap generated in the bitmap is stored in the pre-established bitmap set mLeakBitmaps.
这里,所述第一封装模块41将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类Decode DrawableHelper的实现程序如下:Here, the first encapsulating module 41 encapsulates the loading program of the picture background setting and the loading program of the picture content display into the image resource loading class Decode DrawableHelper. The implementation program is as follows:
DecodeDrawableHelper.loadBackgroundDecodeDrawableHelper.loadBackground
DecodeDrawableHelper.loadImageDecodeDrawableHelper.loadImage
所述Background是指图片背景,所述Image是指图片内容显示。The Background refers to the background of the picture, and the Image refers to the display of the picture content.
具体地,参见图5,所述第一封装模块41包括:第一确定模块51、第一调用模块52、第一生成模块53、第二确定模块54、第一赋值模块55、第一删除模块56、第二生成模块57及第一捕捉模块58;其中,Specifically, referring to FIG. 5, the first encapsulating module 41 includes: a first determining module 51, a first calling module 52, a first generating module 53, a second determining module 54, a first assigning module 55, and a first deleting module. 56, a second generation module 57 and a first capture module 58; wherein
当所述图片资源加载为图片背景设置的加载时,所述第一确定模块51用于接收资源,根据资源ID判断资源类型。这里,因不同的资源类型对应不同的资源ID,因此可以根据资源ID判断出资源类型。其中,所述资源类型包括图片资源、色值资源及XML资源;所述图片资源可以包括:普通图片及点九图;所述色值资源包括例如RGB8、RGB4等色值类型。When the picture resource is loaded as a loading of the picture background, the first determining module 51 is configured to receive a resource, and determine a resource type according to the resource ID. Here, since different resource types correspond to different resource IDs, the resource type can be determined according to the resource ID. The resource type includes a picture resource, a color value resource, and an XML resource. The picture resource may include: a normal picture and a point nine picture; and the color value resource includes a color value type such as RGB8, RGB4, and the like.
当所述第一确定模块51确定资源为图片背景资源时,还需判断图片背景是否加载过,当确定所述图片背景未加载过时,获取需要显示的视图View尺寸及图片背景的尺寸,根据所述视图尺寸判断所述图片背景是否需要压缩。当确定所述图片背景需要压缩时,根据第一压缩比例压缩所述图片背景。这里,第一确定模块51获取图片背景的尺寸是为了减少内存占用,具体的实现方式为:第一确定模块51设置是否只获取边界inJustDecodeBounds的值为true,当设置为true时,在加载图片背景的过程中,只会获取到图片背景的长、宽、高 信息,而不会去真正解析图片背景。当第一确定模块51读取到图片背景的尺寸后,将inJustDecodeBounds设为false。When the first determining module 51 determines that the resource is a background resource of the picture, it is also required to determine whether the background of the picture is loaded. When it is determined that the background of the picture is not loaded, the size of the view that needs to be displayed and the size of the background of the picture are obtained. The view size determines whether the picture background needs compression. When it is determined that the picture background needs to be compressed, the picture background is compressed according to the first compression ratio. Here, the first determining module 51 obtains the size of the background of the picture in order to reduce the memory occupation. The specific implementation manner is: the first determining module 51 sets whether to obtain only the boundary inJustDecodeBounds value is true, when set to true, the background image is loaded. In the process, only the length, width and height of the background of the picture will be obtained. Information without going to really parse the picture background. When the first determining module 51 reads the size of the background of the picture, set inJustDecodeBounds to false.
可选地,当所述第一确定模块51确定所述图片背景已载过时,所述第一调用模块52用于从所述位图集合mLeakBitmaps中调用所述图片背景的第一位图像素数据Bitmap。其中,为了减少所述第一位图像素数据Bitmap的占用内存,本实施例中将第一位图像素数据Bitmap中一个像素设置占用2个字节,例如,第一位图像素数据Bitmap的加载格式可以为Bitmap.Config.RGB_565,所述Bitmap.Config.RGB_565占用的字节为2个。Optionally, when the first determining module 51 determines that the background of the picture has been loaded, the first calling module 52 is configured to invoke the first bitmap pixel data of the background of the picture from the bitmap set mLeakBitmaps. Bitmap. In order to reduce the occupied memory of the first bitmap pixel data Bitmap, in the embodiment, one pixel in the first bitmap pixel data Bitmap is set to occupy 2 bytes, for example, loading of the first bitmap pixel data Bitmap. The format can be Bitmap.Config.RGB_565, and the Bitmap.Config.RGB_565 occupies 2 bytes.
当所述第一确定模块51确定所述图片背景不需要压缩时,所述第一生成模块53还用于根据当前的图片背景生成所述图片背景的第一位图像素数据Bitmap。When the first determining module 51 determines that the background of the picture does not need to be compressed, the first generating module 53 is further configured to generate a first bitmap pixel data Bitmap of the picture background according to the current picture background.
当所述第一生成模块53根据当前的图片背景生成所述图片背景的第一位图像素数据Bitmap后,所述储存模块44将所述第一位图像素数据Bitmap缓存至位图集合mLeakBitmaps中。After the first generation module 53 generates the first bitmap pixel data Bitmap of the picture background according to the current picture background, the storage module 44 caches the first bitmap pixel data Bitmap into the bitmap set mLeakBitmaps. .
当所述第一生成模块53根据当前的图片背景生成所述图片背景的第一位图像素数据Bitmap后,所述第二确定模块54用于判断所述图片背景是否为点九图,如确定所述图片背景为普通图片时,还用于根据所述第一位图像素数据Bitmap生成第一位图对象Drawable,所述第一赋值模块55则将第一位图对象Drawable赋值给需要显示图片背景的视图View,以使得视图View可以显示图片背景。After the first generation module 53 generates the first bitmap pixel data Bitmap of the background of the image according to the current background, the second determining module 54 is configured to determine whether the background of the image is a point nine map, such as determining When the background of the picture is a normal picture, the first bitmap object Drawable is generated according to the first bitmap pixel data Bitmap, and the first assignment module 55 assigns the first bitmap object Drawable to the image to be displayed. The View view of the background so that the View View can display the background of the image.
当所述第二确定模块54确定所述图片背景为点九图时,所述第一删除模块56用于删除生成的所述第一位图像素数据Bitmap;所述第一生成模块53还用于利用资源ID查找并解析图片背景资源,生成第二位图对象;那么所述第一赋值模块55还用于将所述第二位图对象赋值给需要显示图片背景的视图View。这里,因点九图是Android系统特殊支持的图片类型,如果是点九图,在绘制的时候会根据图片内的预先设置的横纵迭代区域填充重复内容。这里,如果直接根据第一位图像素数据Bitmap转第一位图对象Drawable的方式获取第一位图对象Drawable,则会覆盖掉点九图的属性。所以当图片背景是点九图时,则需要删除生成的第一位图像素数据Bitmap。When the second determining module 54 determines that the background of the picture is a nine-point map, the first deleting module 56 is configured to delete the generated first bitmap pixel data Bitmap; the first generating module 53 further uses The first bitmap module 55 is further configured to assign the second bitmap object to the view View that needs to display the background of the image, by using the resource ID to find and parse the picture background resource. Here, because the point nine map is a picture type specially supported by the Android system, if it is a point nine map, the repeated content is filled according to the preset horizontal and vertical iteration area in the picture when drawing. Here, if the first bitmap object Drawable is obtained directly according to the first bitmap pixel data Bitmap to the first bitmap object Drawable, the attributes of the nine maps are overwritten. Therefore, when the background of the picture is a nine-point map, the generated first bitmap pixel data Bitmap needs to be deleted.
可选地,当所述第一确定模块51确定资源为色值资源及XML资源时,所述第二生成模块57还用于根据所述色值资源及XML对应的资源ID,生成相应的位图对象;所述第一赋值模块55还用于将相应的位图对象赋值给需要显示背景的视图。Optionally, when the first determining module 51 determines that the resource is a color value resource and an XML resource, the second generating module 57 is further configured to generate a corresponding bit according to the color value resource and the resource ID corresponding to the XML. The first evaluation module 55 is further configured to assign a corresponding bitmap object to a view that needs to display a background.
具体地,当所述第一确定模块51当确定资源为色值资源及XML资源时,所述第二生成模块57通过Resources.getDrawable(resId)方式获取相应的位图对象Drawable,所述第一赋值模块55将所述位图对象Drawable赋值给视图View,使得视图View可以显示相应的资源。其中,所述resId代指资源ID。Specifically, when the first determining module 51 determines that the resource is a color value resource and an XML resource, the second generating module 57 acquires a corresponding bitmap object Drawable by using a Resources.getDrawable(resId) manner, where the first The assignment module 55 assigns the bitmap object Drawable to the view View so that the view View can display the corresponding resource. Wherein, the resId represents a resource ID.
这里,第一赋值模块55将所述位图对象Drawable赋值给视图View之前,所述第一确定模块51还用于:判断系统中的SDK_INT是否大于16,当确定所述SDK_INT大于或等于16时,View调用setBackground(drawable)方法获取位图对象Drawable,利用位图对象 Drawable赋值背景,从而显示相应的资源;当确定所述SDK_INT小于16时,View调用setBackgroundDrawable)(drawable)方法获取位图对象Drawable,利用位图对象Drawable赋值背景,从而显示相应的资源。应当注意的是,此处所指的背景是指显示色值资源及XML资源的显示背景,与图片背景不同。Here, before the first assignment module 55 assigns the bitmap object Drawable to the view View, the first determining module 51 is further configured to: determine whether the SDK_INT in the system is greater than 16, when determining that the SDK_INT is greater than or equal to 16 , View calls the setBackground (drawable) method to get the bitmap object Drawable, using bitmap objects Drawable assigns a background to display the corresponding resource; when it is determined that the SDK_INT is less than 16, View calls the setBackgroundDrawable) (drawable) method to obtain the bitmap object Drawable, and uses the bitmap object Drawable to assign a background to display the corresponding resource. It should be noted that the background referred to herein refers to the display background of the display color value resource and the XML resource, which is different from the background image.
另外,在加载过程中,为了进一步降低资源加载的成本,所述第一封装模块41还包括:第一捕捉模块58,第一捕捉模块58用于资源加载类为单位,添加异常捕捉,通过Try,Catch包裹捕捉内存不足(OOM,Out Of Memorty)异常,当出现异常时,放弃加载当前的图片背景资源,以避免因内存不足导致系统应用崩溃。In addition, in the loading process, in order to further reduce the cost of resource loading, the first encapsulating module 41 further includes: a first capturing module 58 for the resource loading class as a unit, adding an exception capturing, through Try The Catch package catches an Out of Memorty (OOM) exception. When an exception occurs, the current image background resource is discarded to avoid the system application crash due to insufficient memory.
相应地,当所述图片资源加载为图片内容显示的加载时,参见图6,所述第一封装模块41还包括:第三确定模块61、第二调用模块62、第三生成模块63、第四确定模块64、第二赋值模块65、第二删除模块66、第四生成模块67及第二捕捉模块68;其中,Correspondingly, when the picture resource is loaded as the loading of the picture content display, referring to FIG. 6, the first encapsulating module 41 further includes: a third determining module 61, a second calling module 62, a third generating module 63, and a a determining module 64, a second assigning module 65, a second deleting module 66, a fourth generating module 67, and a second capturing module 68;
所述第三确定模块61用于接收资源,根据资源ID判断资源类型。这里,不同的资源类型对应不同的资源ID,因此可以根据资源ID判断出资源类型。The third determining module 61 is configured to receive a resource, and determine a resource type according to the resource ID. Here, different resource types correspond to different resource IDs, so the resource type can be determined according to the resource ID.
当接收到资源后,所述第三确定模块61根据资源ID确定资源为图片内容显示资源时,判断图片内容显示是否加载过,当确定所述图片内容显示未加载过时;获取需要显示的视图View尺寸及图片内容显示的尺寸,根据所述视图尺寸判断所述图片内容显示是否需要压缩;当确定所述图片内容显示需要压缩时,根据第二压缩比例压缩所述图片内容显示,将图片内容显示压缩后,根据压缩后的图片内容显示生成所述图片内容显示的第二位图像素数据Bitmap;其中,所述第二压缩比例是根据图片内容显示尺寸及视图尺寸计算得出的。这里,所述第三确定模块61获取图片内容显示的尺寸是为了减少内存占用,具体的实现方式为:设置是否只获取边界inJustDecodeBounds的值为true,当设置为true时,在加载图片背景的过程中,只会获取到图片内容显示的长、宽、高信息,而不会去真正解析图片内容显示。读取到图片内容显示的尺寸后,再将inJustDecodeBounds的值设为false。After receiving the resource, the third determining module 61 determines, according to the resource ID, that the resource is a picture content display resource, determines whether the picture content display is loaded, and determines that the picture content display is not loaded; and obtains a view view that needs to be displayed. The size and the size of the picture content display, determining whether the picture content display needs to be compressed according to the view size; when it is determined that the picture content display needs to be compressed, compressing the picture content display according to the second compression ratio, and displaying the picture content After the compression, the second bitmap pixel data Bitmap displayed on the image content is generated according to the compressed image content; wherein the second compression ratio is calculated according to the image content display size and the view size. Here, the third determining module 61 obtains the size of the picture content display in order to reduce the memory occupation. The specific implementation manner is: setting whether to obtain only the boundary inJustDecodeBounds value is true, when set to true, the process of loading the picture background In the middle, only the length, width, and height information of the image content display will be obtained, and the image content display will not be truly resolved. After reading the size of the image content display, set the value of inJustDecodeBounds to false.
当所述第三确定模块61确定所述图片内容显示已加载过时,所述第二调用模块62用于从所述位图集合mLeakBitmaps中调用所述图片内容显示的第二位图像素数据Bitmap。其中,为了减少所述第二位图像素数据Bitmap的占用内存,本实施例中将第二位图像素数据Bitmap中一个像素设置占用2个字节,例如,第二位图像素数据Bitmap的加载格式可以为Bitmap.Config.RGB_565,所述Bitmap.Config.RGB_565占用的字节为2个。When the third determining module 61 determines that the picture content display has been loaded, the second calling module 62 is configured to invoke the second bitmap pixel data Bitmap of the picture content display from the bitmap set mLeakBitmaps. In order to reduce the occupied memory of the second bitmap pixel data Bitmap, in the embodiment, one pixel of the second bitmap pixel data Bitmap is set to occupy 2 bytes, for example, loading of the second bitmap pixel data Bitmap. The format can be Bitmap.Config.RGB_565, and the Bitmap.Config.RGB_565 occupies 2 bytes.
当所述第三确定模块61确定所述图片内容显示不需要压缩时,所述第三生成模块63根据当前的图片内容显示生成所述图片背景的第二位图像素数据Bitmap。When the third determining module 61 determines that the picture content display does not need to be compressed, the third generating module 63 displays a second bitmap pixel data Bitmap of the picture background according to the current picture content.
当所述第三生成模块63根据当前的图片内容显示图片内容显示的第二位图像素数据Bitmap生成之后,将所述第二位图像素数据Bitmap缓存至位图集合中,所述第四确定模块64用于判断所述图片内容显示是否为点九图,如确定所述图片内容显示为普通图片时,所述第三生成模块63用于根据所述第二位图像素数据生成第三位图对象Drawable;所述第二赋值模块65将所述第三位图对象Drawable赋值给需要显示图片内容显示的视图View,以 使得视图View可以显示图片内容显示。After the third generation module 63 generates the second bitmap pixel data Bitmap displayed according to the current picture content display, the second bitmap pixel data Bitmap is buffered into the bitmap set, the fourth determination. The module 64 is configured to determine whether the picture content is displayed as a point nine figure. If the picture content is determined to be a normal picture, the third generation module 63 is configured to generate a third bit according to the second bitmap pixel data. a drawing object Drawable; the second assigning module 65 assigns the third bitmap object Drawable to a view View that needs to display a picture content display, Enables the View View to display the image content display.
当所述第四确定模块64确定所述图片内容显示为点九图时,所述第二删除模块66用于删除生成的所述第二位图像素数据Bitmap;所述第三生成模块63还用于利用资源ID查找并解析图片内容显示资源,生成第四位图对象Drawable;所述第二赋值模块65还用于将所述第四位图对象赋值给需要显示图片内容显示的视图View。这里,因点九图是特殊支持的图片类型,如果是点九图,在绘制的时候会根据图片内的预先设置的横纵迭代区域填充重复内容。这里,如果直接根据第二位图像素数据Bitmap生成第三位图对象Drawable的方式获取第三位图对象Drawable,则会覆盖掉点九图的属性。所以当图片内容显示是点九图时,则需要删除生成的第二位图像素数据Bitmap。When the fourth determining module 64 determines that the picture content is displayed as a point nine map, the second deleting module 66 is configured to delete the generated second bitmap pixel data Bitmap; the third generating module 63 further The second bitmap function is further configured to assign the fourth bitmap object to the view View that needs to display the display of the image content. Here, because the nine-point map is a specially supported image type, if it is a nine-point map, the repeated content is filled according to the preset horizontal and vertical iteration regions in the image. Here, if the third bitmap object Drawable is obtained by directly generating the third bitmap object Drawable according to the second bitmap pixel data Bitmap, the attribute of the point nine map is overwritten. Therefore, when the picture content display is a nine-point map, the generated second bitmap pixel data Bitmap needs to be deleted.
可选地,当所述第三确定模块61确定资源为色值资源及XML资源时,所述第四生成模块67还用于根据所述色值资源及XML对应的资源ID生成相应的位图对象。所述第二赋值模块65还用于将相应的位图对象赋值给需要显示显示图片内容显示的视图。Optionally, when the third determining module 61 determines that the resource is a color value resource and an XML resource, the fourth generating module 67 is further configured to generate a corresponding bitmap according to the color value resource and the resource ID corresponding to the XML. Object. The second assignment module 65 is further configured to assign a corresponding bitmap object to a view that needs to display a display of the displayed picture content.
具体地,当所述第三确定模块61确定资源为色值资源及XML资源时,所述第四生成模块67通过Resources.getDrawable(resId)方式获取相应的位图对象Drawable,所述第二赋值模块65通过ImageView.setImageDrawable方法将位图对象赋值给需要内容显示的视图,使得视图View可以显示相应的资源。其中,所述resId代指资源ID。Specifically, when the third determining module 61 determines that the resource is a color value resource and an XML resource, the fourth generating module 67 acquires a corresponding bitmap object Drawable by using a Resources.getDrawable(resId) manner, and the second assignment The module 65 assigns the bitmap object to the view that needs content display through the ImageView.setImageDrawable method, so that the view View can display the corresponding resource. Wherein, the resId represents a resource ID.
另外,在加载过程中,为了进一步降低资源加载的成本,所述第一封装模块41还包括:第二捕捉模块68,第二捕捉模块68用于资源加载类为单位,添加异常捕捉,通过Try,Catch包裹捕捉内存不足OOM异常,当出现异常时,放弃加载当前的图片资源,以避免因内存不足导致系统应用崩溃。In addition, in the loading process, in order to further reduce the cost of resource loading, the first encapsulating module 41 further includes: a second capturing module 68, where the second capturing module 68 is used for the resource loading class, adding an exception capturing, through Try The Catch package captures insufficient OOM exceptions. When an exception occurs, it abandons loading the current image resource to avoid system application crashes due to insufficient memory.
应当注意的是,本实施例中无论是加载图片背景还是加载图片内容显示时,在资源加载类中,同样的资源只加载一次,有效减少了资源的重复加载,避免因资源重复加载出现内存抖动造成加载过程不稳定的现象。It should be noted that, in the embodiment, whether the background of the loaded image or the content of the loaded image is displayed, in the resource loading class, the same resource is only loaded once, which effectively reduces the repeated loading of the resource, and avoids memory jitter caused by repeated loading of the resource. Causes the loading process to be unstable.
可选地,为了可以回收位图集合mLeakBitmaps中无用的图片资源,实现内存管理的可控,避免因内存不足导致应用崩溃,所述第一封装模块41将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中后,还用于将回收图片资源的程序releaseSrc()封装至所述图片资源加载类中,且所述图片资源回收的程序与所述图片资源加载的程序共用一资源管理接口,所述回收图片资源的程序releaseSrc()具体实现如下:Optionally, in order to recover the useless image resources in the bitmap set mLeakBitmaps, the memory management can be controlled to avoid application crash caused by insufficient memory, and the first encapsulating module 41 displays the loading program and the image content of the background setting of the image. After the loader is packaged into the image resource loading class, the program releaseSrc() for recycling the image resource is further encapsulated into the image resource loading class, and the program for recovering the image resource and the program loaded by the image resource are loaded. A resource management interface is shared, and the program releaseSrc() for reclaiming the picture resource is specifically implemented as follows:
Figure PCTCN2017117675-appb-000004
Figure PCTCN2017117675-appb-000004
Figure PCTCN2017117675-appb-000005
Figure PCTCN2017117675-appb-000005
这里,当触发到预设的图片资源的回收时机时,则会循环执行recycle,来回收位图集合mLeakBitmaps中无用的对第一位图像素数据Bitmap及第一位图像素数据Bitmap。其中,所述回收时机具体为界面上的图片被删除时。Here, when the recovery timing of the preset picture resource is triggered, the recycle is cyclically executed to recover the useless first bitmap pixel data Bitmap and the first bitmap pixel data Bitmap in the bitmap set mLeakBitmaps. The recovery timing is specifically when the picture on the interface is deleted.
可选地,当所述第一封装模块41将图片背景设置的加载程序、图片内容显示的加载程序及图片资源的回收程序封装至图片资源加载类中后,所述第二封装模块42用于为所述图片资源加载类封装一资源管理接口。Optionally, after the first encapsulating module 41 encapsulates the loading program of the picture background setting, the loading program of the picture content display, and the reclaiming program of the picture resource into the picture resource loading class, the second encapsulating module 42 is used to: Loading a class to encapsulate a resource management interface for the image resource.
具体地,当当所述第一封装模块41将图片背景设置的加载程序、图片内容显示的加载程序及图片资源的回收程序封装至图片资源加载类中后,还需为所述图片资源加载类封装一资源管理接口IDecodeDrawableHelper,在需要对图片资源进行加载或对图片资源进行回收时,只需通过所述资源管理接口即可调用资源加载类中的图片资源加载程序对图片资源进行加载,或通过资源管理接口调用图片资源回收程序对图片资源进行回收。这样就可以通过同一个资源管理接口实现图片背景设置、图片内容显示及图片资源回收,从而实现图片资源加载和回收的统一管理。Specifically, when the first encapsulating module 41 encapsulates the loading program of the background setting of the picture, the loading program of the picture content display, and the reclaiming program of the picture resource into the picture resource loading class, the class encapsulation of the picture resource needs to be loaded. A resource management interface IDecodeDrawableHelper, when the image resource needs to be loaded or the image resource needs to be recycled, the image resource loading program in the resource loading class can be called to load the image resource through the resource management interface, or through the resource. The management interface calls the image resource recycling program to recycle the image resources. In this way, the same resource management interface can be used to realize picture background setting, picture content display and image resource recycling, thereby realizing unified management of image resource loading and recycling.
具体地,所述第二封装模块42对所述资源管理接口IDecodeDrawableHelper的封装过程如下:Specifically, the encapsulation process of the resource management interface IDecodeDrawableHelper by the second encapsulation module 42 is as follows:
Public interface IDecodeDrawableHelper{Public interface IDecodeDrawableHelper{
DecodeDrawableHelper getHelper();DecodeDrawableHelper getHelper();
void releaseSrc();Void releaseSrc();
}}
可以看出,在所述资源管理接口IDecodeDrawableHelper下,包含两种方法:第一是getHelper,指获取封装的加载类下的资源加载方法;第二是releaseSrc,指回收资源的方法。It can be seen that under the resource management interface IDecodeDrawableHelper, there are two methods: the first is getHelper, which refers to the resource loading method under the loaded loading class; the second is releaseSrc, which refers to the method of reclaiming resources.
当所述第一封装模块41将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中;所述第二封装模块42将图片资源加载类对应的资源管理接口IDecodeDrawableHelper封装好之后,所述调用模块43即可通过所述资源管理接口调用相应的图片资源加载类,加载相应的图片背景及图片内容显示。具体的加载过程可参见上文;或者所述调用模块43也可按照预设的回收时机通过所述资源管理接口IDecodeDrawableHelper调用图片资源回收程序,回收无用的图片资源,具体的回收资源过程也可参见上文。 The first encapsulating module 41 encapsulates the loading program of the picture background setting and the loading program of the picture content display into the picture resource loading class; the second encapsulating module 42 encapsulates the resource management interface IDecodeDrawableHelper corresponding to the picture resource loading class. Then, the calling module 43 can call the corresponding picture resource loading class through the resource management interface, and load the corresponding picture background and picture content display. For the specific loading process, refer to the above; or the calling module 43 may also invoke the image resource recycling program by using the resource management interface IDecodeDrawableHelper according to a preset recycling time to recover useless image resources, and the specific recycling resource process may also be referred to. Above.
在实际应用中,上文中记载的图片资源加载过程及图片资源回收过程可以适用于任意的View内、ViewGroup内及基类BaseActivity中,以某浏览器中的Activity的基类BaseActivity为例,图片资源加载的过程及图片资源回收的过程的具体实现如下:In practical applications, the image resource loading process and the image resource recycling process described above can be applied to any View, ViewGroup, and base class BaseActivity, taking the BaseActivity of the Activity in a browser as an example, the image resource. The specific implementation of the loading process and the process of image resource recycling is as follows:
获取封装的图片资源加载类下的图片资源加载的方法:Get the image resource loading method under the packaged image resource loading class:
Figure PCTCN2017117675-appb-000006
Figure PCTCN2017117675-appb-000006
图片资源回收的方法:Image resource recycling method:
Figure PCTCN2017117675-appb-000007
Figure PCTCN2017117675-appb-000007
在加载过程中,当界面图片被删除,即在Activity onDestroy时,所述调用模块43可调用资源回收方法releaseSrc对图片资源进行回收,所述调用过程的程序实现如下:During the loading process, when the interface image is deleted, that is, in the Activity onDestroy, the calling module 43 may call the resource recycling method releaseSrc to recover the image resource, and the calling process is implemented as follows:
@Override@Override
protected void onDestroy(){Protected void onDestroy(){
super.onDestroy();super.onDestroy();
releaseSrc();releaseSrc();
这样在BaseActivity内,以及继承自BaseActivity的类内,加载图片资源的时候只需要通过调用getHelper().loadBackground对图片背景设置进行加载,或者通过调用getHelper.loadImage的方法对图片内容显示进行加载;回收图片资源的时候,只要触发到回收时机即可调用releaseSrc对无用的图片资源进行回收。In the BaseActivity, as well as the class that inherits from BaseActivity, when loading the image resource, you only need to load the image background setting by calling getHelper().loadBackground, or load the image content display by calling getHelper.loadImage; When the image resource is triggered, the releaseSrc can be called to recycle the useless image resources whenever the trigger is triggered.
本发明具有以下有益效果或者优点:The invention has the following beneficial effects or advantages:
本发明公开了一种加载图片资源的方法,在加载图片资源之前,将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中,并为所述图片资源加载类封装一资源管理接口;这样,所述资源管理接口下就包括两种加载程序:图片背景设置的加载程序及图片内容显示的加载程序;在需要加载图片资源时,无论是加载图片背景设置还是加载图片内容显示,业务层都可以通过所述资源管理接口调用相应的图片资源加载类,一并加载相应的图片背景及所述图片内容显示,从而优化了资源加载过程,降低了资源加载的成本。并且,以资源加载类为单位,添加异常捕捉,可以对加载过程出现的异常进行统一捕捉和处理,进一步降低了资源加载的成本。 The invention discloses a method for loading a picture resource. Before loading the picture resource, the loading program of the picture background setting and the loading program of the picture content display are encapsulated into the picture resource loading class, and the image resource loading class is encapsulated. The resource management interface; in this way, the resource management interface includes two loaders: a loader for setting the background of the image and a loader for displaying the image content; when the image resource needs to be loaded, whether the image background is loaded or the image content is loaded The service layer can call the corresponding image resource loading class through the resource management interface, and load the corresponding image background and the image content display, thereby optimizing the resource loading process and reducing the cost of resource loading. Moreover, by adding an exception capture to the resource loading class, the exceptions that occur during the loading process can be uniformly captured and processed, thereby further reducing the cost of resource loading.
无论是加载图片背景还是加载图片内容显示时,在资源加载类中,同样的资源只加载一次,有效减少了资源的重复加载,避免因资源重复加载出现内存抖动造成加载过程不稳定的现象。另外,所述方法通过预先建立位图集合,将加载过程中的生成的位图像素数据存储至该位图集合中,实现内存管理可控,并且将资源回收程序也封装至资源加载类中,并在资源回收程序中添加回收时机,当触发所述回收时机时,则会通过相同的资源管理接口调用资源回收程序对位图集合中无用的图片资源进行及时回收,实现了资源回收可控,从根本上解决了NativeCache不足导致的OOM问题。并且也实现了资源加载、回收的统一管理。When loading the background of the image or loading the content of the image, in the resource loading class, the same resource is only loaded once, which effectively reduces the repeated loading of the resource, and avoids the instability of the loading process caused by memory jitter caused by repeated loading of the resource. In addition, the method stores the generated bitmap pixel data in the loading process into the bitmap set by pre-establishing the bitmap set, implements memory management controllable, and encapsulates the resource recovery program into the resource loading class. And the recycling opportunity is added in the resource recycling program. When the recycling opportunity is triggered, the resource recycling program is called through the same resource management interface to timely recycle the useless image resources in the bitmap collection, thereby realizing the resource recovery and controllable. It fundamentally solves the OOM problem caused by the lack of NativeCache. It also realizes unified management of resource loading and recycling.
对于装置实施例而言,由于其与方法实施例基本相似,所以描述的比较简单,相关之处参见方法实施例的部分说明即可。For the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the description of the method embodiment.
在此提供的算法和显示不与任何特定计算机、虚拟系统或者其它设备固有相关。各种通用系统也可以与基于在此的示教一起使用。根据上面的描述,构造这类系统所要求的结构是显而易见的。此外,本发明也不针对任何特定编程语言。应当明白,可以利用各种编程语言实现在此描述的本发明的内容,并且上面对特定语言所做的描述是为了披露本发明的最佳实施方式。The algorithms and displays provided herein are not inherently related to any particular computer, virtual system, or other device. Various general purpose systems can also be used with the teaching based on the teachings herein. The structure required to construct such a system is apparent from the above description. Moreover, the invention is not directed to any particular programming language. It is to be understood that the invention may be embodied in a variety of programming language, and the description of the specific language has been described above in order to disclose the preferred embodiments of the invention.
在此处所提供的说明书中,说明了大量具体细节。然而,能够理解,本发明的实施例可以在没有这些具体细节的情况下实践。在一些实例中,并未详细示出公知的方法、结构和技术,以便不模糊对本说明书的理解。In the description provided herein, numerous specific details are set forth. However, it is understood that the embodiments of the invention may be practiced without these specific details. In some instances, well-known methods, structures, and techniques are not shown in detail so as not to obscure the understanding of the description.
类似地,应当理解,为了精简本公开并帮助理解各个发明方面中的一个或多个,在上面对本发明的示例性实施例的描述中,本发明的各个特征有时被一起分组到单个实施例、图、或者对其的描述中。然而,并不应将该公开的方法解释成反映如下意图:即所要求保护的本发明要求比在每个权利要求中所明确记载的特征更多的特征。更确切地说,如下面的权利要求书所反映的那样,发明方面在于少于前面公开的单个实施例的所有特征。因此,遵循具体实施方式的权利要求书由此明确地并入该具体实施方式,其中每个权利要求本身都作为本发明的单独实施例。Similarly, the various features of the invention are sometimes grouped together into a single embodiment, in the above description of the exemplary embodiments of the invention, Figure, or a description of it. However, the method disclosed is not to be interpreted as reflecting the intention that the claimed invention requires more features than those recited in the claims. Rather, as the following claims reflect, inventive aspects reside in less than all features of the single embodiments disclosed herein. Therefore, the claims following the specific embodiments are hereby explicitly incorporated into the embodiments, and each of the claims as a separate embodiment of the invention.
本领域那些技术人员可以理解,可以对实施例中的设备中的模块进行自适应性地改变并且把它们设置在与该实施例不同的一个或多个设备中。可以把实施例中的模块或单元或组件组合成一个模块或单元或组件,以及此外可以把它们分成多个子模块或子单元或子组件。除了这样的特征和/或过程或者单元中的至少一些是相互排斥之外,可以采用任何组合对本说明书(包括伴随的权利要求、摘要和附图)中公开的所有特征以及如此公开的任何方法或者设备的所有过程或单元进行组合。除非另外明确陈述,本说明书(包括伴随的权利要求、摘要和附图)中公开的每个特征可以由提供相同、等同或相似目的的替代特征来代替。Those skilled in the art will appreciate that the modules in the devices of the embodiments can be adaptively changed and placed in one or more devices different from the embodiment. The modules or units or components of the embodiments may be combined into one module or unit or component, and further they may be divided into a plurality of sub-modules or sub-units or sub-components. In addition to such features and/or at least some of the processes or units being mutually exclusive, any combination of the features disclosed in the specification, including the accompanying claims, the abstract and the drawings, and any methods so disclosed, or All processes or units of the device are combined. Each feature disclosed in this specification (including the accompanying claims, the abstract and the drawings) may be replaced by alternative features that provide the same, equivalent or similar purpose.
此外,本领域的技术人员能够理解,尽管在此的一些实施例包括其它实施例中所包括的某些特征而不是其它特征,但是不同实施例的特征的组合意味着处于本发明的范围之内 并且形成不同的实施例。例如,在下面的权利要求书中,所要求保护的实施例的任意之一都可以以任意的组合方式来使用。In addition, those skilled in the art will appreciate that although some embodiments herein include certain features included in other embodiments and not other features, combinations of features of different embodiments are intended to be within the scope of the present invention. And different embodiments are formed. For example, in the following claims, any one of the claimed embodiments can be used in any combination.
本发明的各个部件实施例可以以硬件实现,或者以在一个或者多个处理器上运行的软件模块实现,或者以它们的组合实现。本领域的技术人员应当理解,可以在实践中使用微处理器或者数字信号处理器(DSP)来实现根据本发明实施例的网关、代理服务器、系统中的一些或者全部部件的一些或者全部功能。本发明还可以实现为用于执行这里所描述的方法的一部分或者全部的设备或者装置程序(例如,计算机程序和计算机程序产品)。这样的实现本发明的程序可以存储在计算机可读介质上,或者可以具有一个或者多个信号的形式。这样的信号可以从因特网网站上下载得到,或者在载体信号上提供,或者以任何其他形式提供。The various component embodiments of the present invention may be implemented in hardware, or in a software module running on one or more processors, or in a combination thereof. Those skilled in the art will appreciate that a microprocessor or digital signal processor (DSP) may be used in practice to implement some or all of the functionality of some, or all, of the gateways, proxy servers, systems in accordance with embodiments of the present invention. The invention can also be implemented as a device or device program (e.g., a computer program and a computer program product) for performing some or all of the methods described herein. Such a program implementing the invention may be stored on a computer readable medium or may be in the form of one or more signals. Such signals may be downloaded from an Internet website, provided on a carrier signal, or provided in any other form.
例如,图7示出了可以实现根据本发明的加载图片资源的方法的计算设备。该计算设备传统上包括处理器710和以存储器720形式的程序产品或者可读介质。存储器720可以是诸如闪存、EEPROM(电可擦除可编程只读存储器)、EPROM或者ROM之类的电子存储器。存储器720具有用于执行上述方法中的任何方法步骤的程序代码731的存储空间730。例如,用于程序代码的存储空间730可以包括分别用于实现上面的方法中的各种步骤的各个程序代码731。这些程序代码可以从一个或者多个程序产品中读出或者写入到这一个或者多个程序产品中。这些程序产品包括诸如存储卡之类的程序代码载体。这样的程序产品通常为如参考图8所述的便携式或者固定存储单元。该存储单元可以具有与图7的计算设备中的存储器720类似布置的存储段、存储空间等。程序代码可以例如以适当形式进行压缩。通常,存储单元包括可读代码731’,即可以由例如诸如710之类的处理器读取的代码,这些代码当由计算设备运行时,导致该计算设备执行上面所描述的方法中的各个步骤。For example, Figure 7 illustrates a computing device that can implement a method of loading picture resources in accordance with the present invention. The computing device conventionally includes a processor 710 and a program product or readable medium in the form of a memory 720. Memory 720 can be an electronic memory such as flash memory, EEPROM (Electrically Erasable Programmable Read Only Memory), EPROM, or ROM. Memory 720 has a memory space 730 for program code 731 for performing any of the method steps described above. For example, storage space 730 for program code may include various program code 731 for implementing various steps in the above methods, respectively. These program codes can be read from or written to one or more program products. These program products include program code carriers such as memory cards. Such a program product is typically a portable or fixed storage unit as described with reference to FIG. The storage unit may have storage segments, storage spaces, and the like that are similarly arranged to memory 720 in the computing device of FIG. The program code can be compressed, for example, in an appropriate form. Typically, the storage unit includes readable code 731', ie, code that can be read by a processor, such as 710, that when executed by the computing device causes the computing device to perform various steps in the methods described above .
应该注意的是上述实施例对本发明进行说明而不是对本发明进行限制,并且本领域技术人员在不脱离所附权利要求的范围的情况下可设计出替换实施例。在权利要求中,不应将位于括号之间的任何参考符号构造成对权利要求的限制。单词“包括”不排除存在未列在权利要求中的元件或步骤。位于元件之前的单词“一”或“一个”不排除存在多个这样的元件。本发明可以借助于包括有若干不同元件的硬件以及借助于适当编程的计算机来实现。在列举了若干装置的单元权利要求中,这些装置中的若干个可以是通过同一个硬件项来具体体现。单词第一、第二、以及第三等的使用不表示任何顺序。可将这些单词解释为名称。 It is to be noted that the above-described embodiments are illustrative of the invention and are not intended to be limiting, and that the invention may be devised without departing from the scope of the appended claims. In the claims, any reference signs placed between parentheses shall not be construed as a limitation. The word 'comprising' does not exclude the presence of the elements or steps that are not recited in the claims. The word "a" or "an" The invention can be implemented by means of hardware comprising several distinct elements and by means of a suitably programmed computer. In the unit claims enumerating several means, several of these means can be embodied by the same hardware item. The use of the words first, second, and third does not indicate any order. These words can be interpreted as names.

Claims (28)

  1. 一种加载图片资源的方法,其特征在于,所述方法包括:A method for loading a picture resource, the method comprising:
    将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中;Encapsulating the loader of the image background setting and the loader of the image content display into the image resource loading class;
    为所述图片资源加载类封装一资源管理接口;Loading a class to encapsulate a resource management interface for the image resource;
    通过所述资源管理接口调用相应的图片资源加载类,加载相应的图片背景及图片内容显示。The corresponding picture resource loading class is invoked by the resource management interface, and the corresponding picture background and picture content display are loaded.
  2. 如权利要求1所述的方法,其特征在于,当对所述图片资源进行加载后,所述方法还包括:将加载过程中生成的位图像素数据存入预先建立的位图集合中。The method of claim 1, wherein after loading the picture resource, the method further comprises: storing the bitmap pixel data generated during the loading process into a pre-established bitmap set.
  3. 如权利要求2所述的方法,其特征在于,将加载过程中生成的位图像素数据存入所述位图集合中后,所述方法还包括:The method of claim 2, wherein after the bitmap pixel data generated in the loading process is stored in the bitmap set, the method further comprises:
    按照预设的回收时机通过所述资源管理接口调用图片资源回收程序,回收无用的图片资源。The image resource recycling program is called through the resource management interface according to a preset recycling timing, and the useless image resource is recovered.
  4. 如权利要求3所述的方法,其特征在于,所述回收时机具体为界面上的图片被删除时。The method according to claim 3, wherein the recovery timing is specifically when the picture on the interface is deleted.
  5. 如权利要求1所述的方法,其特征在于,当所述图片资源加载为图片背景设置的加载时,加载方法具体包括:The method of claim 1, wherein when the picture resource is loaded as a loading of a picture background setting, the loading method specifically includes:
    当确定所述图片背景未加载过时,获取需要显示的视图尺寸,根据所述视图尺寸判断所述图片背景是否需要压缩;When it is determined that the background of the picture is not loaded, obtaining a view size that needs to be displayed, and determining, according to the view size, whether the background of the picture needs to be compressed;
    当确定所述图片背景需要压缩时,根据第一压缩比例压缩所述图片背景;When it is determined that the picture background needs to be compressed, compressing the picture background according to the first compression ratio;
    根据压缩后的图片背景生成所述图片背景的第一位图像素数据;Generating first bitmap pixel data of the background of the picture according to the compressed background of the picture;
    当确定所述图片背景为普通图片时,根据所述第一位图像素数据生成第一位图对象;When it is determined that the background of the picture is a normal picture, generating a first bitmap object according to the first bitmap pixel data;
    将所述第一位图对象赋值给需要显示图片背景的视图。Assigning the first bitmap object to a view that needs to display the background of the image.
  6. 如权利要求5所述的方法,其特征在于,当确定所述图片背景不需要压缩时,所述方法还包括:根据当前的图片背景生成所述图片背景的第一位图像素数据。The method according to claim 5, wherein when it is determined that the background of the picture does not require compression, the method further comprises: generating first bitmap pixel data of the background of the picture according to a current picture background.
  7. 如权利要求5所述的方法,其特征在于,在加载过程中,所述方法还包括:捕捉内存不足异常,当捕捉到异常时,放弃加载当前的图片背景资源。The method of claim 5, wherein during the loading process, the method further comprises: capturing an out of memory exception, and abandoning loading the current picture background resource when the exception is caught.
  8. 如权利要求5所述的方法,其特征在于,当确定所述图片背景已加载过,所述方法还包括:从位图集合中调用所述图片背景的第一位图像素数据。The method of claim 5, wherein when it is determined that the background of the picture has been loaded, the method further comprises: calling the first bitmap pixel data of the background of the picture from the set of bitmaps.
  9. 如权利要求5所述的方法,其特征在于,当确定所述图片背景为点九图时,所述方法还包括:The method of claim 5, wherein when the background of the picture is determined to be a nine-point map, the method further comprises:
    删除生成的所述第一位图像素数据;Deleting the generated first bitmap pixel data;
    利用资源ID查找并解析图片背景资源,生成第二位图对象;Find and parse the picture background resource by using the resource ID to generate a second bitmap object;
    将所述第二位图对象赋值给需要显示图片背景的视图。Assigning the second bitmap object to a view that needs to display the background of the image.
  10. 如权利要求5所述的方法,其特征在于,在加载图片资源之前,所述方法还包括:The method of claim 5, wherein before loading the picture resource, the method further comprises:
    根据资源ID判断资源类型; Determining the resource type according to the resource ID;
    当确定所述资源类型为图片资源时,通过所述资源管理接口调用相应的图片资源加载类,加载相应的图片资源。When it is determined that the resource type is a picture resource, the corresponding picture resource loading class is invoked by the resource management interface, and the corresponding picture resource is loaded.
  11. 如权利要求10所述的方法,其特征在于,所述资源类型包括:图片资源、色值资源及XML资源。The method of claim 10, wherein the resource type comprises: a picture resource, a color value resource, and an XML resource.
  12. 如权利要求11所述的方法,其特征在于,当确定所述资源类型为色值资源及XML文件资源时,所述方法还包括:The method of claim 11, wherein when the resource type is determined to be a color value resource and an XML file resource, the method further comprises:
    利用所述资源ID查找并解析相应的资源,生成相应的位图对象;Using the resource ID to find and parse the corresponding resource, and generating a corresponding bitmap object;
    将相应的位图对象赋值给需要显示所述图片背景的视图。Assign the corresponding bitmap object to the view that needs to display the background of the image.
  13. 如权利要求5所述的方法,其特征在于,当所述图片资源加载为图片内容显示的加载时,加载方法具体包括:The method of claim 5, wherein when the image resource is loaded for loading of the image content display, the loading method specifically includes:
    当确定所述图片内容显示未加载过时,获取需要显示的视图尺寸,根据所述视图尺寸判断所述图片内容显示是否需要压缩;When it is determined that the picture content display is not loaded, obtaining a view size that needs to be displayed, and determining, according to the view size, whether the picture content display needs to be compressed;
    当确定所述图片内容显示需要压缩时,根据第二压缩比例压缩所述图片内容显示;When it is determined that the picture content display needs to be compressed, compressing the picture content display according to a second compression ratio;
    根据压缩后的图片内容显示生成所述图片内容显示的第二位图像素数据;Generating, according to the compressed picture content, the second bitmap pixel data generated by the image content display;
    当确定所述图片内容显示不是点九图时,根据所述第二位图像素数据生成第三位图对象;When it is determined that the picture content display is not a point nine map, generating a third bitmap object according to the second bitmap pixel data;
    将所述第三位图对象赋值给需要显示图片内容显示的视图。The third bitmap object is assigned to a view that needs to display the display of the image content.
  14. 一种加载图片资源的装置,其特征在于,所述装置包括:An apparatus for loading a picture resource, the apparatus comprising:
    第一封装模块,用于将图片背景设置的加载程序及图片内容显示的加载程序封装至图片资源加载类中;a first encapsulating module, configured to encapsulate a loader of a picture background setting and a loader for displaying a picture content into a picture resource loading class;
    第二封装模块,用于为所述图片资源加载类封装一资源管理接口;a second encapsulating module, configured to load a resource management interface for the image resource loading class;
    调用模块,用于通过所述资源管理接口调用相应的图片资源加载类,加载相应的图片背景及图片内容显示。The calling module is configured to invoke a corresponding image resource loading class through the resource management interface, and load a corresponding image background and a picture content display.
  15. 如权利要求14所述的装置,其特征在于,所述装置还包括储存模块,当对所述图片资源进行加载后,所述储存模块用于:将加载过程中生成的位图像素数据存入预先建立的位图集合中。The device of claim 14, wherein the device further comprises a storage module, configured to: store the bitmap pixel data generated during the loading process after loading the picture resource Pre-established bitmap set.
  16. 如权利要求15所述的装置,其特征在于,所述调用模块还用于:按照预设的回收时机通过所述资源管理接口调用图片资源回收程序,回收无用的图片资源。The device according to claim 15, wherein the calling module is further configured to: call a picture resource recycling program through the resource management interface according to a preset recycling time, and recycle unnecessary image resources.
  17. 如权利要求15所述的装置,其特征在于,所述回收时机具体为界面上的图片被删除时。The device according to claim 15, wherein the recovery timing is specifically when the picture on the interface is deleted.
  18. 如权利要求14所述的装置,其特征在于,当所述图片资源加载为图片背景设置的加载时,所述第一封装模块包括:The apparatus according to claim 14, wherein when the picture resource is loaded as a loading of a picture background setting, the first encapsulating module comprises:
    第一确定模块,用于确定当所述图片背景未加载过时,获取需要显示的视图尺寸,根据所述视图尺寸判断所述图片背景是否需要压缩;当确定当所述图片背景需要压缩时,根据第一压缩比例压缩所述图片背景; a first determining module, configured to determine, when the background of the picture is not loaded, obtaining a view size that needs to be displayed, determining, according to the view size, whether the picture background needs to be compressed; when determining that the picture background needs to be compressed, according to The first compression ratio compresses the background of the picture;
    第一生成模块,用于根据压缩后的图片背景生成所述图片背景的第一位图像素数据;a first generation module, configured to generate first bitmap pixel data of the background of the image according to the compressed background of the image;
    第二确定模块,用于确定所述图片背景为普通图片时,根据所述第一位图像素数据生成第一位图对象;a second determining module, configured to generate a first bitmap object according to the first bitmap pixel data when the background of the picture is a normal picture;
    第一赋值模块,用于将所述第一位图对象赋值给需要显示图片背景的视图。The first assignment module is configured to assign the first bitmap object to a view that needs to display a background of the image.
  19. 如权利要求18所述的装置,其特征在于,当所述第一确定模块确定所述图片背景不需要压缩时,所述第一生成模块还用于:根据当前的图片背景生成所述图片背景的第一位图像素数据。The device according to claim 18, wherein when the first determining module determines that the background of the picture does not need to be compressed, the first generating module is further configured to: generate the picture background according to a current picture background The first bitmap pixel data.
  20. 如权利要求18所述的装置,其特征在于,所述第一封装模块还包括:第一捕捉模块,用于在加载过程中捕捉内存不足异常,当捕捉到异常时,放弃加载所述图片背景资源。The apparatus according to claim 18, wherein the first encapsulating module further comprises: a first capturing module, configured to capture an out of memory exception during loading, and abandon loading the background of the image when an exception is caught Resources.
  21. 如权利要求18所述的装置,其特征在于,所述第一封装模块还包括:第一调用模块,当所述第一确定模块确定所述图片背景已加载过时,所述第一调用模块用于从位图集合中调用所述图片背景的第一位图像素数据。The device according to claim 18, wherein the first encapsulating module further comprises: a first calling module, when the first determining module determines that the background of the picture has been loaded, the first calling module uses The first bitmap pixel data of the background of the picture is called from a bitmap set.
  22. 如权利要求18所述的装置,其特征在于,所述第一封装模块还包括:第一删除模块;当所述第二确定模块确定所述图片背景为点九图时,The apparatus according to claim 18, wherein the first encapsulating module further comprises: a first deleting module; and when the second determining module determines that the background of the picture is a point nine figure,
    所述第一删除模块用于删除生成的所述第一位图像素数据;The first deleting module is configured to delete the generated first bitmap pixel data;
    所述第一生成模块还用于利用资源ID查找并解析图片背景资源,生成第二位图对象;The first generation module is further configured to: find and parse a picture background resource by using a resource ID, and generate a second bitmap object;
    所述第一赋值模块还用于将所述第二位图对象赋值给需要显示图片背景的视图。The first assignment module is further configured to assign the second bitmap object to a view that needs to display a background of the image.
  23. 如权利要求14所述的装置,其特征在于,在加载图片资源之前,所述第一确定模块还用于根据资源ID判断资源类型;The device according to claim 14, wherein the first determining module is further configured to determine a resource type according to the resource ID before loading the picture resource;
    当确定当所述资源类型为图片资源时,所述调用模块用于通过所述资源管理接口调用相应的图片资源加载类,加载相应的图片资源。When it is determined that the resource type is a picture resource, the calling module is configured to invoke a corresponding picture resource loading class by using the resource management interface, and load a corresponding picture resource.
  24. 如权利要求23所述的装置,其特征在于,所述资源类型包括:图片资源、色值资源及XML资源。The apparatus according to claim 23, wherein the resource type comprises: a picture resource, a color value resource, and an XML resource.
  25. 如权利要求23所述的装置,其特征在于,所述第一封装模块还包括:第二生成模块;其中,当所述第一确定模块确定当所述资源类型为色值资源及XML文件资源时,The apparatus according to claim 23, wherein the first encapsulating module further comprises: a second generating module; wherein, when the first determining module determines that the resource type is a color value resource and an XML file resource Time,
    所述第二生成模块用于利用所述资源ID查找并解析相应的资源,生成相应的位图对象;The second generating module is configured to use the resource ID to find and parse a corresponding resource, and generate a corresponding bitmap object;
    所述第一赋值模块还用于将相应的位图对象赋值给需要显示图片背景的视图。The first assignment module is further configured to assign a corresponding bitmap object to a view that needs to display a background of the picture.
  26. 如权利要求18所述的装置,其特征在于,当所述图片资源加载为图片内容显示的加载时,所述第一封装模块还包括:The apparatus according to claim 18, wherein when the picture resource is loaded for loading of the picture content display, the first encapsulating module further comprises:
    第三确定模块,用于确定所述图片内容显示未加载过时,获取需要显示的视图尺寸,根据所述视图尺寸判断所述图片内容显示是否需要压缩;当所述图片内容显示需要压缩时,根据第二压缩比例压缩所述图片内容显示;a third determining module, configured to determine that the picture content display is not loaded, obtain a view size that needs to be displayed, and determine, according to the view size, whether the picture content display needs to be compressed; when the picture content display needs to be compressed, according to a second compression ratio compresses the picture content display;
    第三生成模块,用于根据压缩后的图片内容显示生成所述图片内容显示的第二位图像素数据;a third generation module, configured to generate second bitmap pixel data displayed by the image content according to the compressed image content display;
    第四确定模块,用于确定当所述图片内容显示为普通图片时,根据所述第二位图像素 数据生成第三位图对象;a fourth determining module, configured to determine, according to the second bitmap pixel, when the picture content is displayed as a normal picture The data generates a third bitmap object;
    第二赋值模块,用于将所述第三位图对象赋值给需要显示图片内容显示的视图。And a second assignment module, configured to assign the third bitmap object to a view that needs to display a display of the content of the picture.
  27. 一种计算机程序,包括计算机可读代码,当所述可读代码在计算设备上运行时,导致所述计算设备执行根据权利要求1-13中的任一所述的加载图片资源的方法。A computer program comprising computer readable code, when the readable code is run on a computing device, causing the computing device to perform the method of loading a picture asset according to any of claims 1-13.
  28. 一种计算机可读介质,其中存储了如权利要求27所述的程序。 A computer readable medium storing the program of claim 27.
PCT/CN2017/117675 2016-12-21 2017-12-21 Method and device for loading picture resource, and program and medium WO2018113725A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201611189784.2A CN106598679B (en) 2016-12-21 2016-12-21 Method and device for loading picture resources
CN201611189784.2 2016-12-21

Publications (1)

Publication Number Publication Date
WO2018113725A1 true WO2018113725A1 (en) 2018-06-28

Family

ID=58600129

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2017/117675 WO2018113725A1 (en) 2016-12-21 2017-12-21 Method and device for loading picture resource, and program and medium

Country Status (2)

Country Link
CN (1) CN106598679B (en)
WO (1) WO2018113725A1 (en)

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113256485A (en) * 2021-05-21 2021-08-13 百果园技术(新加坡)有限公司 Image stretching method, device, electronic equipment and storage medium
CN113419802A (en) * 2021-06-21 2021-09-21 网易(杭州)网络有限公司 Atlas generation method and apparatus, electronic device and storage medium
CN114398124A (en) * 2021-12-31 2022-04-26 深圳市珍爱捷云信息技术有限公司 Point nine-effect graph rendering method based on iOS system and related device thereof

Families Citing this family (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106598679B (en) * 2016-12-21 2020-07-31 北京奇虎科技有限公司 Method and device for loading picture resources
CN107301053B (en) * 2017-07-04 2020-12-15 武汉斗鱼网络科技有限公司 Picture configuration method and device and electronic terminal
CN107491334B (en) * 2017-08-29 2020-12-15 四川长虹电器股份有限公司 Method for organizing and loading preloaded files
CN113254131A (en) * 2021-05-20 2021-08-13 北京有竹居网络技术有限公司 Page background display method and device
CN114090116A (en) * 2021-11-08 2022-02-25 芜湖雄狮汽车科技有限公司 Application method and device of vehicle-mounted equipment, vehicle and storage medium

Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103309749A (en) * 2013-06-25 2013-09-18 四川九洲电器集团有限责任公司 Internal memory release method of image resource based on Android system
CN103902318A (en) * 2012-12-30 2014-07-02 青岛海尔软件有限公司 Android platform based image loading method
US20140351380A1 (en) * 2013-05-22 2014-11-27 Alibaba Group Holding Limited Loading image information
CN104267996A (en) * 2014-09-29 2015-01-07 广州金山网络科技有限公司 Image resource processing method, device and terminal
WO2015161809A1 (en) * 2014-04-25 2015-10-29 Tencent Technology (Shenzhen) Company Limited Picture splitting method and apparatus, and picture loading method and apparatus
CN105446763A (en) * 2014-09-30 2016-03-30 Tcl集团股份有限公司 Picture loading and displaying method and system
CN106598679A (en) * 2016-12-21 2017-04-26 北京奇虎科技有限公司 Method and device for loading picture resources

Family Cites Families (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP6223001B2 (en) * 2013-06-06 2017-11-01 キヤノン株式会社 Image processing apparatus, control method thereof, and program
CN104360871A (en) * 2014-10-12 2015-02-18 厦门雅基软件有限公司 Quick starting method for installation of cell phone games
CN104850440A (en) * 2015-05-25 2015-08-19 广州市久邦数码科技有限公司 Android system based resource loading method and system therefor
CN105468368A (en) * 2015-11-23 2016-04-06 浪潮软件股份有限公司 Interface beautifying method
CN105511864A (en) * 2015-11-30 2016-04-20 青岛海信移动通信技术股份有限公司 Resource positioning method and device
CN106126650A (en) * 2016-06-24 2016-11-16 武汉斗鱼网络科技有限公司 A kind of Web page picture based on PHP extension supports system

Patent Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103902318A (en) * 2012-12-30 2014-07-02 青岛海尔软件有限公司 Android platform based image loading method
US20140351380A1 (en) * 2013-05-22 2014-11-27 Alibaba Group Holding Limited Loading image information
CN103309749A (en) * 2013-06-25 2013-09-18 四川九洲电器集团有限责任公司 Internal memory release method of image resource based on Android system
WO2015161809A1 (en) * 2014-04-25 2015-10-29 Tencent Technology (Shenzhen) Company Limited Picture splitting method and apparatus, and picture loading method and apparatus
CN104267996A (en) * 2014-09-29 2015-01-07 广州金山网络科技有限公司 Image resource processing method, device and terminal
CN105446763A (en) * 2014-09-30 2016-03-30 Tcl集团股份有限公司 Picture loading and displaying method and system
CN106598679A (en) * 2016-12-21 2017-04-26 北京奇虎科技有限公司 Method and device for loading picture resources

Cited By (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113256485A (en) * 2021-05-21 2021-08-13 百果园技术(新加坡)有限公司 Image stretching method, device, electronic equipment and storage medium
CN113256485B (en) * 2021-05-21 2024-01-30 百果园技术(新加坡)有限公司 Image stretching method, device, electronic equipment and storage medium
CN113419802A (en) * 2021-06-21 2021-09-21 网易(杭州)网络有限公司 Atlas generation method and apparatus, electronic device and storage medium
CN113419802B (en) * 2021-06-21 2022-08-05 网易(杭州)网络有限公司 Atlas generation method and apparatus, electronic device and storage medium
CN114398124A (en) * 2021-12-31 2022-04-26 深圳市珍爱捷云信息技术有限公司 Point nine-effect graph rendering method based on iOS system and related device thereof
CN114398124B (en) * 2021-12-31 2024-04-12 深圳市珍爱捷云信息技术有限公司 Point nine effect graph rendering method based on iOS system and related device thereof

Also Published As

Publication number Publication date
CN106598679A (en) 2017-04-26
CN106598679B (en) 2020-07-31

Similar Documents

Publication Publication Date Title
WO2018113725A1 (en) Method and device for loading picture resource, and program and medium
CN109885324B (en) Processing method, device, terminal and storage medium of application program installation package
CN104869305B (en) Method and apparatus for processing image data
CN105138376A (en) Mixed model application, webpage resource upgrading method thereof, mobile terminal and system
WO2017202175A1 (en) Method and device for video compression and electronic device
CN106598614B (en) Method and device for recycling picture resources
CN112416458A (en) Preloading method and device based on ReactNative, computer equipment and storage medium
WO2019170126A1 (en) Picture drawing method and related products
CN110889067A (en) Page rendering method, device and equipment and storage medium
CN111931095A (en) Picture loading method of Android system
CN106293658B (en) Interface component generation method and equipment
CN110955434A (en) Software development kit processing method and device, computer equipment and storage medium
KR20190013870A (en) Image generation method and device
CN112256774B (en) Power data processing method, device, computer equipment and storage medium
CN106020729A (en) Clearing method and device for multi-media data of mobile terminal
CN106204668B (en) Picture decoding method and device
CN108287892B (en) Picture loading method and device and electronic equipment
CN107092601B (en) Resource file construction method, resource file application method and device
CN115062340A (en) Device access method, interface processing method and device
CN110704157B (en) Application starting method, related device and medium
CN114443306A (en) Resource processing method and device, electronic equipment and computer readable storage medium
CN106020730A (en) Clearing method and device for multi-media data of mobile device
CN113784075A (en) Screen video reading method and system and computing device
CN114237871A (en) Arranging method and device of cloud resources, computer equipment and storage medium
CN112000364B (en) Application program packaging method, device, equipment and storage medium

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 17882523

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 17882523

Country of ref document: EP

Kind code of ref document: A1