Further tweaks to the async code

Change-Id: Ib1e76687054a254a9918a4391a1cb512c59c6515
This commit is contained in:
Tor Lillqvist
2012-06-20 12:35:07 +03:00
parent 7f3c614d2b
commit f3748935be

View File

@@ -39,7 +39,6 @@ import android.view.GestureDetector;
import android.view.Gravity; import android.view.Gravity;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation; import android.view.animation.Animation;
import android.view.animation.AnimationSet; import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation; import android.view.animation.TranslateAnimation;
@@ -107,17 +106,13 @@ public class DocumentLoader
if (((PageViewer)flipper.getCurrentView()).currentPageNumber == pageCount-1) if (((PageViewer)flipper.getCurrentView()).currentPageNumber == pageCount-1)
return false; return false;
AnimationSet inFromRight = new AnimationSet(true); Animation inFromRight = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0,
inFromRight.addAnimation(new AlphaAnimation(0.1f, 1.0f)); Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
inFromRight.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0));
inFromRight.setDuration(500); inFromRight.setDuration(500);
flipper.setInAnimation(inFromRight); flipper.setInAnimation(inFromRight);
AnimationSet outToLeft = new AnimationSet(true); Animation outToLeft = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1,
outToLeft.addAnimation(new AlphaAnimation(1f, 0.1f)); Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
outToLeft.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0));
outToLeft.setDuration(500); outToLeft.setDuration(500);
flipper.setOutAnimation(outToLeft); flipper.setOutAnimation(outToLeft);
@@ -129,17 +124,13 @@ public class DocumentLoader
if (((PageViewer)flipper.getCurrentView()).currentPageNumber == 0) if (((PageViewer)flipper.getCurrentView()).currentPageNumber == 0)
return false; return false;
AnimationSet inFromLeft = new AnimationSet(true); Animation inFromLeft = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 0,
inFromLeft.addAnimation(new AlphaAnimation(0.1f, 1.0f)); Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
inFromLeft.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0));
inFromLeft.setDuration(500); inFromLeft.setDuration(500);
flipper.setInAnimation(inFromLeft); flipper.setInAnimation(inFromLeft);
AnimationSet outToRight = new AnimationSet(true); Animation outToRight = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1,
outToRight.addAnimation(new AlphaAnimation(1f, 0.1f)); Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
outToRight.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0));
outToRight.setDuration(500); outToRight.setDuration(500);
flipper.setOutAnimation(outToRight); flipper.setOutAnimation(outToRight);
@@ -242,14 +233,14 @@ public class DocumentLoader
long t0 = System.currentTimeMillis(); long t0 = System.currentTimeMillis();
PropertyValue rendererProps[] = renderable.getRenderer(number, doc, renderProps); PropertyValue rendererProps[] = renderable.getRenderer(number, doc, renderProps);
long t1 = System.currentTimeMillis(); long t1 = System.currentTimeMillis();
Log.i(TAG, "renderer properties: (took " + ((t1-t0)-timingOverhead) + " ms)"); Log.i(TAG, "getRenderer took " + ((t1-t0)-timingOverhead) + " ms");
int pageWidth = 0, pageHeight = 0; int pageWidth = 0, pageHeight = 0;
for (int i = 0; i < rendererProps.length; i++) { for (int i = 0; i < rendererProps.length; i++) {
if (rendererProps[i].Name.equals("PageSize")) { if (rendererProps[i].Name.equals("PageSize")) {
pageWidth = ((Size) rendererProps[i].Value).Width; pageWidth = ((Size) rendererProps[i].Value).Width;
pageHeight = ((Size) rendererProps[i].Value).Height; pageHeight = ((Size) rendererProps[i].Value).Height;
Log.i(TAG, " PageSize: " + pageWidth + "x" + pageHeight); Log.i(TAG, "PageSize: " + pageWidth + "x" + pageHeight);
} }
} }
@@ -257,8 +248,6 @@ public class DocumentLoader
ByteBuffer bb = ByteBuffer.allocateDirect(1024*1024*4); ByteBuffer bb = ByteBuffer.allocateDirect(1024*1024*4);
long wrapped_bb = Bootstrap.new_byte_buffer_wrapper(bb); long wrapped_bb = Bootstrap.new_byte_buffer_wrapper(bb);
Log.i(TAG, "bb is " + bb);
if (pageWidth == 0) { if (pageWidth == 0) {
// Huh? // Huh?
device = toolkit.createScreenCompatibleDeviceUsingBuffer(1024, 1024, 1, 1, 0, 0, wrapped_bb); device = toolkit.createScreenCompatibleDeviceUsingBuffer(1024, 1024, 1, 1, 0, 0, wrapped_bb);
@@ -299,34 +288,33 @@ public class DocumentLoader
int currentPageNumber = -1; int currentPageNumber = -1;
TextView waitView; TextView waitView;
PageState state = PageState.NONEXISTENT; PageState state = PageState.NONEXISTENT;
ByteBuffer bb; Bitmap bm;
class PageLoadTask class PageLoadTask
extends AsyncTask<Void, Void, Void> extends AsyncTask<Integer, Void, Integer>
{ {
protected Void doInBackground(Void... params) protected Integer doInBackground(Integer... params)
{ {
if (currentPageNumber == pageCount) int number = params[0];
return null;
if (number == pageCount)
return -1;
try {
Thread.sleep(5000);
}
catch (InterruptedException e) {
}
state = PageState.LOADING; state = PageState.LOADING;
bb = renderPage(currentPageNumber); currentPageNumber = number;
return null; ByteBuffer bb = renderPage(currentPageNumber);
bm = Bitmap.createBitmap(1024, 1024, Bitmap.Config.ARGB_8888);
bm.copyPixelsFromBuffer(bb);
return currentPageNumber;
} }
protected void onPostExecute(Void result) protected void onPostExecute(Integer result)
{ {
if (currentPageNumber == pageCount) Log.i(TAG, "onPostExecute: " + result);
if (result == -1)
return; return;
Bitmap bm = Bitmap.createBitmap(1024, 1024, Bitmap.Config.ARGB_8888);
bm.copyPixelsFromBuffer(bb);
ImageView imageView = new ImageView(DocumentLoader.this); ImageView imageView = new ImageView(DocumentLoader.this);
imageView.setImageBitmap(bm); imageView.setImageBitmap(bm);
@@ -342,12 +330,9 @@ public class DocumentLoader
void display(int number) void display(int number)
{ {
if (number == currentPageNumber) Log.i(TAG, "PageViewer display(" + number + ")");
return; if (number >= 0)
waitView.setText("Page " + (number+1) + ", wait...");
currentPageNumber = number;
waitView.setText("Page " + (currentPageNumber + 1) + ", wait...");
state = PageState.NONEXISTENT; state = PageState.NONEXISTENT;
if (getDisplayedChild() == 1) { if (getDisplayedChild() == 1) {
@@ -355,9 +340,8 @@ public class DocumentLoader
removeViewAt(1); removeViewAt(1);
} }
Log.i(TAG, "PageViewer display(" + number + ")"); if (number >= 0) {
if (currentPageNumber >= 0) { new PageLoadTask().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, number);
new PageLoadTask().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
} }
} }