Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

android - ArrayList of Image resource ID's vs ArrayList of Bitmaps

My objective is to read an XML file which contains references to local Drawables and Strings, saving that data to an array of persistent MapData objects held within a ViewModel. (Once this ViewModel has been initialized, the data will remain until app restart.)

After these MapData objects are initialized in the ViewModel, a MapData object may then be used in a re-useable Fragment which uses the Drawable resource IDs to update an ImageView's image as needed by the user.

Is the following practice of storing the Drawable resource IDs the best practice? Would the alternative be to store the image in a Bitmap? I ask this primarily because the images are relatively large files, and memory seems to be easily consumed once these images get loaded. If loading another image into the ImageView, the memory doesn't free up. In short, I'm concerned about memory consumption.

Thank you!

...
public class MapMenuFragment {
    private MapViewViewModel = null; //Initializes if null at createView()

    ...

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        
        ...

        TypedArray typedArray = getResources().obtainTypedArray(R.array.maps_resources_array);
        MapData[] mapData = new MapData[typedArray.length()];
        for(int i = 0; i < typedArray.length(); i++) {
        
            mapData[i] = new MapData();

            TypedArray mapTypedArray = getResources().obtainTypedArray(typedArray.getResourceId(i, 0));

            //Set map name
            mapData[i].setMapName(mapTypedArray.getString(0));

            //Set map layer images
            TypedArray mapImages = getResources().obtainTypedArray(mapTypedArray.getResourceId(1, 0));
            for(int j = 0; j < mapImages.length(); j++)
                mapData[i].addMapResource(mapImages.getResourceId(j, 0));
            mapImages.recycle();

            //Set map layer names
            TypedArray mapLayerNamesIDs = getResources().obtainTypedArray(mapTypedArray.getResourceId(2, 0));
            for (int j = 0; j < mapLayerNamesIDs.length(); j++)
                mapData[i].addMapLayerNameID(mapLayerNamesIDs.getResourceId(j, 0));

            mapLayerNamesIDs.recycle();

            mapTypedArray.recycle();

        }

        typedArray.recycle();

        mapViewViewModel.setMapData(mapData);

    ...
public class MapData{

    private String mapName = "Unknown";
    private ArrayList<Integer> mapResources = new ArrayList<Integer>();
    private ArrayList<Integer> mapLayerNameIDs = new ArrayList<Integer>();
    private int mapLayerIndex = 0;

   ...
}
public class MapViewViewModel extends ViewModel {

    private MapData[] mapData = null;

    private int currentMap = 0;

    public void setMapData(MapData[] mapData){
        this.mapData = mapData;
    }

    public MapData[] getMapData(){
        return mapData;
    }

    public void setCurrentMapDataFromPos(int currentMapPos){
        this.currentMap = currentMapPos;
    }

    public MapData getCurrentMapData() {
        return mapData[currentMap];
    }
}

public class MapViewFragment extends Fragment
    private MapViewViewModel mapViewViewModel = null; // initializes if null at onCreateView()
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  ...
 //on button click, do the following
mapImage.setImageResource(mapViewViewModel.getCurrentMapData().getMapResources().get(mapViewViewModel.getCurrentMapData().getMapLayerIndex()));
...
}
    <!-- Array of references to map resource arrays-->
    <integer-array name= "maps_resources_array">
        <item>@array/map_asylum_array</item>
        <item>@array/map_bleasdale_array</item>
        <item>@array/map_grafton_array</item>
        <item>@array/map_brownstone_array</item>
        <item>@array/map_edgefield_array</item>
        <item>@array/map_tanglewood_array</item>
        <item>@array/map_ridgeview_array</item>
        <item>@array/map_prison_array</item>
    </integer-array>
    
    <!-- Map-Specific arrays of all their resources -->
    <integer-array name= "map_asylum_array">
        <item>@string/map_name_asylum</item>
        <item>@array/map_asylum_image_outline_array</item>
        <item>@array/map_asylum_layerName_array</item>
    </integer-array>
    
    ...
    
    <!-- Map-Specific arrays of all their outline resources -->
    <integer-array name= "map_asylum_image_outline_array">
        <item>@drawable/map_asylum_layer_outline_top</item>
        <item>@drawable/map_asylum_layer_outline_bottom</item>
    </integer-array>
    
    ...
    
    <!-- Map-Specific arrays of all their Layer names -->
    <integer-array name="map_asylum_layerName_array">
        <item>@string/map_layer_names_first</item>
        <item>@string/map_layer_names_basement</item>
    </integer-array>
    
    ...

Minimum Android API: 21, Current Android API: 30, Language: Java


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...