Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package nextrek.adapters;

import nextrek.widgets.VList;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class TileAdapter extends BaseAdapter {

    private Resources resources;
    private String packageName;
    
    private int PAGE_WIDTH;
    private int PAGE_HEIGHT;
    private int PAGES;
    
    private String PREFIX;

    public TileAdapter(Context context, int pageWidth, int pageHeight, int pages, String prefix) {
        resources = context.getResources();
        packageName = context.getPackageName();
        PAGE_WIDTH = pageWidth;
        PAGE_HEIGHT = pageHeight;
        PAGES = pages;
        PREFIX = prefix;
    }

    @Override
    public int getCount() {
        return PAGES;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //Integer pos = position;
        //System.out.println("TileAdapter: getView "+pos);
        
        ImageView image;
        AbsListView.LayoutParams params;

        int targetWidth = VList.screenWidth;
        int targetHeight = (PAGE_HEIGHT * targetWidth) / PAGE_WIDTH;

        boolean updateLayout = true;

        String baseName = PREFIX + ((position < 10) ? "0" : "") + position;

        if (convertView == null) {
            image = new ImageView(parent.getContext());
            params = new AbsListView.LayoutParams(targetWidth, targetHeight);
            
            //image.setBackgroundColor(0xFFFFFFFF);
            
            image.setFocusable(false);
            image.setFocusableInTouchMode(false);
            image.setClickable(false);
            
            image.setDrawingCacheEnabled(false);
            image.destroyDrawingCache();
            image.setScaleType(ScaleType.FIT_XY);

            convertView = image;

        } else {
            image = (ImageView) convertView;

            params = (AbsListView.LayoutParams) image.getLayoutParams();

            if ((targetWidth != params.width) || (targetHeight != params.height)) {
                params.width = targetWidth;
                params.height = targetHeight;
            } else {
                updateLayout = false;
            }

        }

        if (updateLayout) {
            image.setLayoutParams(params);
            // image.requestLayout();
        }

        Integer drawable0 = resources.getIdentifier(baseName, "drawable", packageName);
        
        synchronized (image) {
            
            Integer lastUrl = (Integer)image.getTag();
            
            if ((lastUrl == null) || (lastUrl.intValue() != drawable0.intValue())) {
                //System.out.println("Pos: " + position + " draw: " + drawable0);
                image.setTag(drawable0);
                
                BitmapDrawable drawable = null;
                Bitmap bitmap = null;
                try {
                    Options ops = new Options();
                    ops.inDither = false;
                    ops.inPreferredConfig = Config.RGB_565;
                    ops.inSampleSize = 1;
                    ops.inInputShareable = false;
                    ops.inPurgeable = true;
                    ops.inScaled = false;
                    bitmap = BitmapFactory.decodeResource(resources, drawable0, ops);
                    
                    //System.out.println("decoded: "+drawable0+" "+ops.outWidth + " " + Debug.getNativeHeapAllocatedSize());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (bitmap != null) {
                    drawable = new BitmapDrawable(bitmap);
                }
                
                image.setImageDrawable(drawable);
            }
        }
        
        return convertView;
    }
}

Commits for Nextrek/Android/LibrerieNextrek/src/nextrek/adapters/TileAdapter.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000