This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathFlipAsyncContentActivity.java
250 lines (199 loc) · 7.17 KB
/
FlipAsyncContentActivity.java
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
Copyright 2012 Aphid Mobile
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.aphidmobile.flip.demo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.aphidmobile.flip.FlipViewController;
import com.aphidmobile.flip.demo.data.Travels;
import com.aphidmobile.flipview.demo.R;
import com.aphidmobile.utils.AphidLog;
import com.aphidmobile.utils.IO;
import com.aphidmobile.utils.UI;
import java.lang.ref.WeakReference;
import java.util.Random;
public class FlipAsyncContentActivity extends Activity {
private FlipViewController flipView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.activity_title);
flipView = new FlipViewController(this);
flipView.setAdapter(new MyBaseAdapter(this, flipView));
setContentView(flipView);
}
@Override
protected void onResume() {
super.onResume();
flipView.onResume();
}
@Override
protected void onPause() {
super.onPause();
flipView.onPause();
}
private static class MyBaseAdapter extends BaseAdapter {
private FlipViewController controller;
private Context context;
private LayoutInflater inflater;
private Bitmap placeholderBitmap;
private MyBaseAdapter(Context context, FlipViewController controller) {
inflater = LayoutInflater.from(context);
this.context = context;
this.controller = controller;
//Use a system resource as the placeholder
placeholderBitmap =
BitmapFactory.decodeResource(context.getResources(), android.R.drawable.dark_header);
}
@Override
public int getCount() {
return Travels.IMG_DESCRIPTIONS.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View layout = convertView;
if (convertView == null) {
layout = inflater.inflate(R.layout.complex1, null);
}
final Travels.Data data = Travels.IMG_DESCRIPTIONS.get(position);
UI
.<TextView>findViewById(layout, R.id.title)
.setText(AphidLog.format("%d. %s", position, data.title));
UI
.<TextView>findViewById(layout, R.id.description)
.setText(Html.fromHtml(data.description));
UI
.<Button>findViewById(layout, R.id.wikipedia)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
Intent.ACTION_VIEW,
Uri.parse(data.link)
);
inflater.getContext().startActivity(intent);
}
});
ImageView photoView = UI.findViewById(layout, R.id.photo);
//Use an async task to load the bitmap
boolean needReload = true;
AsyncImageTask previousTask = AsyncDrawable.getTask(photoView);
if (previousTask != null) {
if (previousTask.getPageIndex() == position && previousTask.getImageName()
.equals(data.imageFilename)) //check if the convertView happens to be previously used
{
needReload = false;
} else {
previousTask.cancel(true);
}
}
if (needReload) {
AsyncImageTask
task =
new AsyncImageTask(layout.getContext().getAssets(), photoView, controller, position,
data.imageFilename);
photoView
.setImageDrawable(new AsyncDrawable(context.getResources(), placeholderBitmap, task));
task.execute();
}
return layout;
}
}
/**
* Borrowed from the official BitmapFun tutorial: http://developer.android.com/training/displaying-bitmaps/index.html
*/
private static final class AsyncDrawable extends BitmapDrawable {
private final WeakReference<AsyncImageTask> taskRef;
public AsyncDrawable(Resources res, Bitmap bitmap, AsyncImageTask task) {
super(res, bitmap);
this.taskRef = new WeakReference<AsyncImageTask>(task);
}
public static AsyncImageTask getTask(ImageView imageView) {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
return ((AsyncDrawable) drawable).taskRef.get();
}
return null;
}
}
private static final class AsyncImageTask extends AsyncTask<Void, Void, Bitmap> {
private static final Random RANDOM = new Random();
private final AssetManager assetManager;
private final WeakReference<ImageView> imageViewRef;
private final WeakReference<FlipViewController> controllerRef;
private final int pageIndex;
private final String imageName;
public AsyncImageTask(AssetManager assetManager, ImageView imageView,
FlipViewController controller, int pageIndex, String imageName) {
this.assetManager = assetManager;
imageViewRef = new WeakReference<ImageView>(imageView);
controllerRef = new WeakReference<FlipViewController>(controller);
this.pageIndex = pageIndex;
this.imageName = imageName;
}
public int getPageIndex() {
return pageIndex;
}
public String getImageName() {
return imageName;
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
Thread.sleep(500 + RANDOM.nextInt(2000)); //wait for a random time
} catch (InterruptedException e) {
}
return IO.readBitmap(assetManager, imageName);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
return;
}
ImageView imageView = imageViewRef.get();
if (imageView != null && AsyncDrawable.getTask(imageView)
== this) { //the imageView can be reused for another page, so it's necessary to check its consistence
imageView.setImageBitmap(bitmap);
FlipViewController controller = controllerRef.get();
if (controller != null) {
controller.refreshPage(pageIndex);
}
}
}
}
}