diff --git a/.gradle/2.8/taskArtifacts/cache.properties.lock b/.gradle/2.8/taskArtifacts/cache.properties.lock index bb5a154..4664387 100644 Binary files a/.gradle/2.8/taskArtifacts/cache.properties.lock and b/.gradle/2.8/taskArtifacts/cache.properties.lock differ diff --git a/.gradle/2.8/taskArtifacts/fileHashes.bin b/.gradle/2.8/taskArtifacts/fileHashes.bin index 7b8dc50..0531d73 100644 Binary files a/.gradle/2.8/taskArtifacts/fileHashes.bin and b/.gradle/2.8/taskArtifacts/fileHashes.bin differ diff --git a/.gradle/2.8/taskArtifacts/fileSnapshots.bin b/.gradle/2.8/taskArtifacts/fileSnapshots.bin index 9c0655b..3e1f3fa 100644 Binary files a/.gradle/2.8/taskArtifacts/fileSnapshots.bin and b/.gradle/2.8/taskArtifacts/fileSnapshots.bin differ diff --git a/.gradle/2.8/taskArtifacts/taskArtifacts.bin b/.gradle/2.8/taskArtifacts/taskArtifacts.bin index 56016ad..93c1704 100644 Binary files a/.gradle/2.8/taskArtifacts/taskArtifacts.bin and b/.gradle/2.8/taskArtifacts/taskArtifacts.bin differ diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4813806..5fd87d7 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -36,35 +36,41 @@ - + - - - - - - - - - - - - - - - - + + - - + + - + + + + + + + + + + + + + + + + + + + + + @@ -2850,27 +2856,36 @@ + + + - @@ -3022,7 +3110,7 @@ - + - + - - - - - - - - - - - - + + + + + + - + + - + + + + + + @@ -3275,10 +3363,10 @@ - + - + @@ -3288,17 +3376,17 @@ - + - + - + @@ -3326,74 +3414,21 @@ file://$PROJECT_DIR$/gestureimageview/src/main/java/com/polites/android/GestureImageView.java - 387 + 317 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -3405,7 +3440,6 @@ - @@ -3448,131 +3482,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -3597,19 +3506,10 @@ - - - - - - - - - @@ -3621,21 +3521,10 @@ - - - - - - - - - - - @@ -3643,7 +3532,6 @@ - @@ -3651,10 +3539,6 @@ - - - - @@ -3662,7 +3546,6 @@ - @@ -3670,7 +3553,6 @@ - @@ -3678,7 +3560,6 @@ - @@ -3686,7 +3567,6 @@ - @@ -3694,18 +3574,6 @@ - - - - - - - - - - - - @@ -3713,7 +3581,6 @@ - @@ -3721,7 +3588,6 @@ - @@ -3729,13 +3595,6 @@ - - - - - - - @@ -3743,7 +3602,6 @@ - @@ -3751,39 +3609,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -3791,7 +3616,6 @@ - @@ -3799,7 +3623,6 @@ - @@ -3807,7 +3630,6 @@ - @@ -3823,7 +3645,6 @@ - @@ -3831,7 +3652,6 @@ - @@ -3839,6 +3659,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3846,38 +3794,32 @@ - - - - - - - - - - - + - + - - + + - - - - - - - + + + + + + + + + + + diff --git a/PdfView/build/intermediates/bundles/release/classes.jar b/PdfView/build/intermediates/bundles/release/classes.jar index 780e726..2f4b0cc 100644 Binary files a/PdfView/build/intermediates/bundles/release/classes.jar and b/PdfView/build/intermediates/bundles/release/classes.jar differ diff --git a/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$1.class b/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$1.class index 8f10343..a1d79ed 100644 Binary files a/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$1.class and b/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$1.class differ diff --git a/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$2.class b/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$2.class index 4f49bbe..fa293b5 100644 Binary files a/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$2.class and b/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$2.class differ diff --git a/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$3.class b/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$3.class index 24369f9..1d32bf3 100644 Binary files a/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$3.class and b/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView$3.class differ diff --git a/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView.class b/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView.class index e3f30d6..a509ec9 100644 Binary files a/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView.class and b/PdfView/build/intermediates/classes/release/net/sf/andpdf/pdfviewer/gui/PdfView.class differ diff --git a/PdfView/build/intermediates/exploded-aar/Android-Pdf-Viewer-Library/gestureimageview/unspecified/jars/classes.jar b/PdfView/build/intermediates/exploded-aar/Android-Pdf-Viewer-Library/gestureimageview/unspecified/jars/classes.jar index 8662373..c19efcd 100644 Binary files a/PdfView/build/intermediates/exploded-aar/Android-Pdf-Viewer-Library/gestureimageview/unspecified/jars/classes.jar and b/PdfView/build/intermediates/exploded-aar/Android-Pdf-Viewer-Library/gestureimageview/unspecified/jars/classes.jar differ diff --git a/PdfView/build/outputs/aar/PdfView-release.aar b/PdfView/build/outputs/aar/PdfView-release.aar index 016f94c..35a547d 100644 Binary files a/PdfView/build/outputs/aar/PdfView-release.aar and b/PdfView/build/outputs/aar/PdfView-release.aar differ diff --git a/PdfView/src/main/java/net/sf/andpdf/pdfviewer/gui/PdfView.java b/PdfView/src/main/java/net/sf/andpdf/pdfviewer/gui/PdfView.java index faae42d..06ac7b0 100644 --- a/PdfView/src/main/java/net/sf/andpdf/pdfviewer/gui/PdfView.java +++ b/PdfView/src/main/java/net/sf/andpdf/pdfviewer/gui/PdfView.java @@ -30,266 +30,258 @@ import java.nio.channels.FileChannel; public class PdfView extends FullScrollView { - private static final int STARTPAGE = 1; - private static final float STARTZOOM = 1.0f; + private static final int STARTPAGE = 1; + private static final float STARTZOOM = 1.0f; - private static final float MIN_ZOOM = 0.25f; - private static final float MAX_ZOOM = 3.0f; - private static final float ZOOM_INCREMENT = 1.5f; + private static final float MIN_ZOOM = 0.25f; + private static final float MAX_ZOOM = 3.0f; + private static final float ZOOM_INCREMENT = 1.5f; - private Bitmap mBi; - private GestureImageView mImageView; - private Handler uiHandler; - ImageButton bZoomOut; - ImageButton bZoomIn; - private PDFFile mPdfFile; - private PDFPage mPdfPage; - private Thread backgroundThread; - private int mPage; - private float mZoom; + private Bitmap mBi; + public GestureImageView mImageView; + private Handler uiHandler; + ImageButton bZoomOut; + ImageButton bZoomIn; + private PDFFile mPdfFile; + private PDFPage mPdfPage; + private Thread backgroundThread; + private int mPage; + private float mZoom; - public PdfView(Context context) { - this(context, null); + public PdfView(Context context) { + this(context, null); + } + + public PdfView(Context context, AttributeSet attrs) { + this(context, attrs, android.R.attr.scrollViewStyle); + } + + public PdfView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + PDFImage.sShowImages = true; + PDFPaint.s_doAntiAlias = true; + uiHandler = new Handler(); + LayoutParams matchLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); + mImageView = new GestureImageView(context); + mImageView.setLayoutParams(matchLp); + addView(mImageView); + setLayoutParams(matchLp); + setBackgroundColor(Color.LTGRAY); + setHorizontalScrollBarEnabled(true); + setHorizontalFadingEdgeEnabled(true); + setVerticalScrollBarEnabled(true); + setVerticalFadingEdgeEnabled(true); + } + + public PDFFile getmPdfFile() { + return mPdfFile; + } + + public void setmPdfFile(PDFFile mPdfFile) { + this.mPdfFile = mPdfFile; + } + + private int getDeviceWidth() { + DisplayMetrics metric = new DisplayMetrics(); + WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); + wm.getDefaultDisplay().getMetrics(metric); + return metric.widthPixels; // 屏幕宽度(像素) + } + + private int getDeviceHeight() { + DisplayMetrics metric = new DisplayMetrics(); + WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); + wm.getDefaultDisplay().getMetrics(metric); + return metric.heightPixels; // 屏幕高度(像素) + } + + public void showPage(int page, float zoom) throws Exception { + try { + // free memory from previous page + updateImage(); + // Only load the page if it's a different page (i.e. not just changing the zoom level) + if (mPdfPage == null || mPdfPage.getPageNumber() != page) { + mPdfPage = mPdfFile.getPage(page, true); + } + float width = mPdfPage.getWidth(); + float height = mPdfPage.getHeight(); + if (getLayoutParams().height == ViewGroup.LayoutParams.MATCH_PARENT) { + height *= getDeviceWidth() / width; + } + if (getLayoutParams().width == LayoutParams.MATCH_PARENT) { + width = getDeviceWidth(); + } + + RectF clip = null; + Bitmap bi = mPdfPage.getImage((int) (width * zoom), (int) (height * zoom), clip, true, true); + setPageBitmap(bi); + updateImage(); + } catch (Throwable e) { + Log.e(TAG, e.getMessage(), e); } + } - public PdfView(Context context, AttributeSet attrs) { - this(context, attrs, android.R.attr.scrollViewStyle); + private void updateImage() { + uiHandler.post(new Runnable() { + public void run() { + mImageView.setImageBitmap(mBi); + } + }); + } + + private void setPageBitmap(Bitmap bi) { + if (bi != null) { + mBi = bi; } + } - public PdfView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - PDFImage.sShowImages = true; - PDFPaint.s_doAntiAlias = true; - uiHandler = new Handler(); - LayoutParams matchLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); - mImageView = new GestureImageView(context); + private void zoomIn() { + if (mPdfFile != null) { + if (mZoom < MAX_ZOOM) { + mZoom *= ZOOM_INCREMENT; + if (mZoom > MAX_ZOOM) mZoom = MAX_ZOOM; - setPageBitmap(null); - updateImage(); - addView(mImageView, matchLp); - setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); - setBackgroundColor(Color.LTGRAY); - setHorizontalScrollBarEnabled(true); - setHorizontalFadingEdgeEnabled(true); - setVerticalScrollBarEnabled(true); - setVerticalFadingEdgeEnabled(true); - } - - public PDFFile getmPdfFile() { - return mPdfFile; - } - - public void setmPdfFile(PDFFile mPdfFile) { - this.mPdfFile = mPdfFile; - } - - private int getDeviceWidth() { - DisplayMetrics metric = new DisplayMetrics(); - WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); - wm.getDefaultDisplay().getMetrics(metric); - return metric.widthPixels; // 屏幕宽度(像素) - - } - - private int getDeviceHeight() { - DisplayMetrics metric = new DisplayMetrics(); - WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); - wm.getDefaultDisplay().getMetrics(metric); - return metric.heightPixels; // 屏幕高度(像素) - } - - public void showPage(int page, float zoom) throws Exception { - try { - // free memory from previous page - setPageBitmap(null); - updateImage(); - // Only load the page if it's a different page (i.e. not just changing the zoom level) - if (mPdfPage == null || mPdfPage.getPageNumber() != page) { - mPdfPage = mPdfFile.getPage(page, true); - } - float width = mPdfPage.getWidth(); - float height = mPdfPage.getHeight(); - if (getLayoutParams().height == ViewGroup.LayoutParams.MATCH_PARENT) { - height *= getDeviceWidth() / width; - } - if (getLayoutParams().width == LayoutParams.MATCH_PARENT) { - width = getDeviceWidth(); - } - - RectF clip = null; - Bitmap bi = mPdfPage.getImage((int) (width * zoom), (int) (height * zoom), clip, true, true); - setPageBitmap(bi); - updateImage(); - - } catch (Throwable e) { - Log.e(TAG, e.getMessage(), e); - } - } - - - private void updateImage() { - uiHandler.post(new Runnable() { - public void run() { - mImageView.setImageBitmap(mBi); - } - }); - } - - private void setPageBitmap(Bitmap bi) { - if (bi != null) { - mBi = bi; - } - } - - - private void zoomIn() { - if (mPdfFile != null) { - if (mZoom < MAX_ZOOM) { - mZoom *= ZOOM_INCREMENT; - if (mZoom > MAX_ZOOM) mZoom = MAX_ZOOM; - - if (mZoom >= MAX_ZOOM) { - Log.d(TAG, "Disabling zoom in button"); - bZoomIn.setEnabled(false); - } else { - bZoomIn.setEnabled(true); - } - - bZoomOut.setEnabled(true); - startRenderThread(mPage, mZoom); - } - } - } - - private void zoomOut() { - if (mPdfFile != null) { - if (mZoom > MIN_ZOOM) { - mZoom /= ZOOM_INCREMENT; - if (mZoom < MIN_ZOOM) mZoom = MIN_ZOOM; - - if (mZoom <= MIN_ZOOM) { - Log.d(TAG, "Disabling zoom out button"); - bZoomOut.setEnabled(false); - } else { - bZoomOut.setEnabled(true); - } - - bZoomIn.setEnabled(true); - startRenderThread(mPage, mZoom); - } - } - } - - private void nextPage() { - if (mPdfFile != null) { - if (mPage < mPdfFile.getNumPages()) { - mPage += 1; - bZoomOut.setEnabled(true); - bZoomIn.setEnabled(true); - startRenderThread(mPage, mZoom); - } - } - } - - private void prevPage() { - if (mPdfFile != null) { - if (mPage > 1) { - mPage -= 1; - bZoomOut.setEnabled(true); - bZoomIn.setEnabled(true); - startRenderThread(mPage, mZoom); - } - } - } - - private void gotoPage() { - if (mPdfFile != null) { -// showDialog(DIALOG_PAGENUM); - } - } - - public synchronized void startRenderThread(final int page, final float zoom) { - if (backgroundThread != null) return; - backgroundThread = new Thread(new Runnable() { - public void run() { - try { - if (mPdfFile != null) { - showPage(page, zoom); - } - } catch (Exception e) { - Log.e(TAG, e.getMessage(), e); - } - backgroundThread = null; - } - }); - updateImageStatus(); - backgroundThread.start(); - } - - private void updateImageStatus() { - if (backgroundThread == null) { - return; - } - postDelayed(new Runnable() { - public void run() { - updateImageStatus(); - } - }, 1000); - } - - public void parsePDF(File f, String password) throws PDFAuthenticationFailureException { - try { - long len = f.length(); - if (len == 0) { - toastMessage("file '" + f.getName() + "' not found"); - } else { - toastMessage("file '" + f.getName() + "' has " + len + " bytes"); - openFile(f, password); - } - } catch (PDFAuthenticationFailureException e) { - throw e; - } catch (Throwable e) { - e.printStackTrace(); - toastMessage("Exception: " + e.getMessage()); - } - } - - - public void parsePDF(String filename, String password) throws PDFAuthenticationFailureException { - try { - File f = new File(filename); - long len = f.length(); - if (len == 0) { - toastMessage("file '" + filename + "' not found"); - } else { - toastMessage("file '" + filename + "' has " + len + " bytes"); - openFile(f, password); - } - } catch (PDFAuthenticationFailureException e) { - throw e; - } catch (Throwable e) { - e.printStackTrace(); - toastMessage("Exception: " + e.getMessage()); - } - } - - public void openFile(File file, String password) throws IOException { - // first open the file for random access - RandomAccessFile raf = new RandomAccessFile(file, "r"); - // extract a file channel - FileChannel channel = raf.getChannel(); - // now memory-map a byte-buffer - ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size())); - // create a PDFFile from the data - if (password == null) { - mPdfFile = new PDFFile(bb); + if (mZoom >= MAX_ZOOM) { + Log.d(TAG, "Disabling zoom in button"); + bZoomIn.setEnabled(false); } else { - mPdfFile = new PDFFile(bb, new PDFPassword(password)); + bZoomIn.setEnabled(true); } - toastMessage("Anzahl Seiten:" + mPdfFile.getNumPages()); - } - public void toastMessage(String msg) { - Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show(); + bZoomOut.setEnabled(true); + startRenderThread(mPage, mZoom); + } } + } + + private void zoomOut() { + if (mPdfFile != null) { + if (mZoom > MIN_ZOOM) { + mZoom /= ZOOM_INCREMENT; + if (mZoom < MIN_ZOOM) mZoom = MIN_ZOOM; + + if (mZoom <= MIN_ZOOM) { + Log.d(TAG, "Disabling zoom out button"); + bZoomOut.setEnabled(false); + } else { + bZoomOut.setEnabled(true); + } + + bZoomIn.setEnabled(true); + startRenderThread(mPage, mZoom); + } + } + } + + private void nextPage() { + if (mPdfFile != null) { + if (mPage < mPdfFile.getNumPages()) { + mPage += 1; + bZoomOut.setEnabled(true); + bZoomIn.setEnabled(true); + startRenderThread(mPage, mZoom); + } + } + } + + private void prevPage() { + if (mPdfFile != null) { + if (mPage > 1) { + mPage -= 1; + bZoomOut.setEnabled(true); + bZoomIn.setEnabled(true); + startRenderThread(mPage, mZoom); + } + } + } + + private void gotoPage() { + if (mPdfFile != null) { + // showDialog(DIALOG_PAGENUM); + } + } + + public synchronized void startRenderThread(final int page, final float zoom) { + if (backgroundThread != null) return; + backgroundThread = new Thread(new Runnable() { + public void run() { + try { + if (mPdfFile != null) { + showPage(page, zoom); + } + } catch (Exception e) { + Log.e(TAG, e.getMessage(), e); + } + backgroundThread = null; + } + }); + updateImageStatus(); + backgroundThread.start(); + } + + private void updateImageStatus() { + if (backgroundThread == null) { + return; + } + postDelayed(new Runnable() { + public void run() { + updateImageStatus(); + } + }, 1000); + } + + public void parsePDF(File f, String password) throws PDFAuthenticationFailureException { + try { + long len = f.length(); + if (len == 0) { + toastMessage("file '" + f.getName() + "' not found"); + } else { + toastMessage("file '" + f.getName() + "' has " + len + " bytes"); + openFile(f, password); + } + } catch (PDFAuthenticationFailureException e) { + throw e; + } catch (Throwable e) { + e.printStackTrace(); + toastMessage("Exception: " + e.getMessage()); + } + } + + public void parsePDF(String filename, String password) throws PDFAuthenticationFailureException { + try { + File f = new File(filename); + long len = f.length(); + if (len == 0) { + toastMessage("file '" + filename + "' not found"); + } else { + toastMessage("file '" + filename + "' has " + len + " bytes"); + openFile(f, password); + } + } catch (PDFAuthenticationFailureException e) { + throw e; + } catch (Throwable e) { + e.printStackTrace(); + toastMessage("Exception: " + e.getMessage()); + } + } + + public void openFile(File file, String password) throws IOException { + // first open the file for random access + RandomAccessFile raf = new RandomAccessFile(file, "r"); + // extract a file channel + FileChannel channel = raf.getChannel(); + // now memory-map a byte-buffer + ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size())); + // create a PDFFile from the data + if (password == null) { + mPdfFile = new PDFFile(bb); + } else { + mPdfFile = new PDFFile(bb, new PDFPassword(password)); + } + toastMessage("Anzahl Seiten:" + mPdfFile.getNumPages()); + } + + public void toastMessage(String msg) { + Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show(); + } } diff --git a/build/intermediates/dex-cache/cache.xml b/build/intermediates/dex-cache/cache.xml index fc4d970..e34a50f 100644 --- a/build/intermediates/dex-cache/cache.xml +++ b/build/intermediates/dex-cache/cache.xml @@ -2,11 +2,11 @@ - + sha1="2f2ce378a301d9d7e7e28471e3b2ca00a2a7a295"> + diff --git a/gestureimageview/build/intermediates/bundles/release/classes.jar b/gestureimageview/build/intermediates/bundles/release/classes.jar index 8662373..c19efcd 100644 Binary files a/gestureimageview/build/intermediates/bundles/release/classes.jar and b/gestureimageview/build/intermediates/bundles/release/classes.jar differ diff --git a/gestureimageview/build/intermediates/classes/release/com/polites/android/GestureImageView$1.class b/gestureimageview/build/intermediates/classes/release/com/polites/android/GestureImageView$1.class index 8b5775e..eb93d90 100644 Binary files a/gestureimageview/build/intermediates/classes/release/com/polites/android/GestureImageView$1.class and b/gestureimageview/build/intermediates/classes/release/com/polites/android/GestureImageView$1.class differ diff --git a/gestureimageview/build/intermediates/classes/release/com/polites/android/GestureImageView.class b/gestureimageview/build/intermediates/classes/release/com/polites/android/GestureImageView.class index de954bb..d5c3f12 100644 Binary files a/gestureimageview/build/intermediates/classes/release/com/polites/android/GestureImageView.class and b/gestureimageview/build/intermediates/classes/release/com/polites/android/GestureImageView.class differ diff --git a/gestureimageview/build/outputs/aar/gestureimageview-release.aar b/gestureimageview/build/outputs/aar/gestureimageview-release.aar index 280affc..09ab6a5 100644 Binary files a/gestureimageview/build/outputs/aar/gestureimageview-release.aar and b/gestureimageview/build/outputs/aar/gestureimageview-release.aar differ diff --git a/gestureimageview/src/main/java/com/polites/android/GestureImageView.java b/gestureimageview/src/main/java/com/polites/android/GestureImageView.java index 05d4b81..c6b6c37 100644 --- a/gestureimageview/src/main/java/com/polites/android/GestureImageView.java +++ b/gestureimageview/src/main/java/com/polites/android/GestureImageView.java @@ -23,6 +23,7 @@ import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Matrix; +import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; @@ -40,674 +41,658 @@ import java.util.concurrent.TimeUnit; public class GestureImageView extends ImageView { - public static final String GLOBAL_NS = "http://schemas.android.com/apk/res/android"; - public static final String LOCAL_NS = "http://schemas.polites.com/android"; + public static final String GLOBAL_NS = "http://schemas.android.com/apk/res/android"; + public static final String LOCAL_NS = "http://schemas.polites.com/android"; - private final Semaphore drawLock = new Semaphore(0); - private Animator animator; + private final Semaphore drawLock = new Semaphore(0); + private Animator animator; - private Drawable drawable; + private Drawable drawable; + private int drawableWidth = 0; + private int drawableHeight = 0; - private float x = 0, y = 0; + private float x = 0, y = 0; - private boolean layout = false; + private float scaleAdjust = 1.0f; + private float startingScale = -1.0f; - private float scaleAdjust = 1.0f; - private float startingScale = -1.0f; + private float scale = 1.0f; + private float maxScale = 5.0f; + private float minScale = 0.75f; + private float fitScaleHorizontal = 1.0f; + private float fitScaleVertical = 1.0f; + private float rotation = 0.0f; - private float scale = 1.0f; - private float maxScale = 5.0f; - private float minScale = 0.75f; - private float fitScaleHorizontal = 1.0f; - private float fitScaleVertical = 1.0f; - private float rotation = 0.0f; + private float centerX; + private float centerY; - private float centerX; - private float centerY; + private Float startX, startY; - private Float startX, startY; + private int hWidth; + private int hHeight; - private int hWidth; - private int hHeight; + private int resId = -1; + private boolean recycle = false; + private boolean strict = false; - private int resId = -1; - private boolean recycle = false; - private boolean strict = false; + private int displayHeight; + private int displayWidth; - private int displayHeight; - private int displayWidth; + private int alpha = 255; + private ColorFilter colorFilter; - private int alpha = 255; - private ColorFilter colorFilter; + private int deviceOrientation = -1; + private int imageOrientation; - private int deviceOrientation = -1; - private int imageOrientation; + private GestureImageViewListener gestureImageViewListener; + private GestureImageViewTouchListener gestureImageViewTouchListener; - private GestureImageViewListener gestureImageViewListener; - private GestureImageViewTouchListener gestureImageViewTouchListener; + private OnTouchListener customOnTouchListener; + private OnClickListener onClickListener; - private OnTouchListener customOnTouchListener; - private OnClickListener onClickListener; + public GestureImageView(Context context, AttributeSet attrs, int defStyle) { + this(context, attrs); + } - public GestureImageView(Context context, AttributeSet attrs, int defStyle) { - this(context, attrs); + public GestureImageView(Context context, AttributeSet attrs) { + super(context, attrs); + + String scaleType = attrs.getAttributeValue(GLOBAL_NS, "scaleType"); + + if (scaleType == null || scaleType.trim().length() == 0) { + setScaleType(ScaleType.CENTER_INSIDE); } - public GestureImageView(Context context, AttributeSet attrs) { - super(context, attrs); + String strStartX = attrs.getAttributeValue(LOCAL_NS, "start-x"); + String strStartY = attrs.getAttributeValue(LOCAL_NS, "start-y"); - String scaleType = attrs.getAttributeValue(GLOBAL_NS, "scaleType"); - - if (scaleType == null || scaleType.trim().length() == 0) { - setScaleType(ScaleType.CENTER_INSIDE); - } - - String strStartX = attrs.getAttributeValue(LOCAL_NS, "start-x"); - String strStartY = attrs.getAttributeValue(LOCAL_NS, "start-y"); - - if (strStartX != null && strStartX.trim().length() > 0) { - startX = Float.parseFloat(strStartX); - } - - if (strStartY != null && strStartY.trim().length() > 0) { - startY = Float.parseFloat(strStartY); - } - - setStartingScale(attrs.getAttributeFloatValue(LOCAL_NS, "start-scale", startingScale)); - setMinScale(attrs.getAttributeFloatValue(LOCAL_NS, "min-scale", minScale)); - setMaxScale(attrs.getAttributeFloatValue(LOCAL_NS, "max-scale", maxScale)); - setStrict(attrs.getAttributeBooleanValue(LOCAL_NS, "strict", strict)); - setRecycle(attrs.getAttributeBooleanValue(LOCAL_NS, "recycle", recycle)); - - initImage(); + if (strStartX != null && strStartX.trim().length() > 0) { + startX = Float.parseFloat(strStartX); } - public GestureImageView(Context context) { - super(context); - setScaleType(ScaleType.CENTER_INSIDE); - initImage(); + if (strStartY != null && strStartY.trim().length() > 0) { + startY = Float.parseFloat(strStartY); } - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + setStartingScale(attrs.getAttributeFloatValue(LOCAL_NS, "start-scale", startingScale)); + setMinScale(attrs.getAttributeFloatValue(LOCAL_NS, "min-scale", minScale)); + setMaxScale(attrs.getAttributeFloatValue(LOCAL_NS, "max-scale", maxScale)); + setStrict(attrs.getAttributeBooleanValue(LOCAL_NS, "strict", strict)); + setRecycle(attrs.getAttributeBooleanValue(LOCAL_NS, "recycle", recycle)); - if (drawable != null) { - int orientation = getResources().getConfiguration().orientation; - if (orientation == Configuration.ORIENTATION_LANDSCAPE) { - displayHeight = MeasureSpec.getSize(heightMeasureSpec); + initImage(); + } - if (getLayoutParams().width == LayoutParams.WRAP_CONTENT) { - float ratio = (float) getImageWidth() / (float) getImageHeight(); - displayWidth = Math.round((float) displayHeight * ratio); - } else { - displayWidth = MeasureSpec.getSize(widthMeasureSpec); - } - } else { - displayWidth = MeasureSpec.getSize(widthMeasureSpec); - if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) { - float ratio = (float) getImageHeight() / (float) getImageWidth(); - displayHeight = Math.round((float) displayWidth * ratio); - } else { - displayHeight = MeasureSpec.getSize(heightMeasureSpec); - } - } + public GestureImageView(Context context) { + super(context); + setScaleType(ScaleType.CENTER_INSIDE); + initImage(); + } + + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + if (drawable != null) { + int orientation = getResources().getConfiguration().orientation; + if (orientation == Configuration.ORIENTATION_LANDSCAPE) { + displayHeight = MeasureSpec.getSize(heightMeasureSpec); + + if (getLayoutParams().width == LayoutParams.WRAP_CONTENT) { + float ratio = (float) getImageWidth() / (float) getImageHeight(); + displayWidth = Math.round((float) displayHeight * ratio); } else { - displayHeight = MeasureSpec.getSize(heightMeasureSpec); - displayWidth = MeasureSpec.getSize(widthMeasureSpec); + displayWidth = MeasureSpec.getSize(widthMeasureSpec); } + } else { + displayWidth = MeasureSpec.getSize(widthMeasureSpec); - setMeasuredDimension(displayWidth, displayHeight); - } - - @Override - protected void onLayout(boolean changed, int left, int top, int right, int bottom) { - super.onLayout(changed, left, top, right, bottom); - if (changed || !layout) { - setupCanvas(displayWidth, displayHeight, getResources().getConfiguration().orientation); + if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) { + float ratio = (float) getImageHeight() / (float) getImageWidth(); + displayHeight = Math.round((float) displayWidth * ratio); + } else { + displayHeight = MeasureSpec.getSize(heightMeasureSpec); } + } + } else { + displayHeight = MeasureSpec.getSize(heightMeasureSpec); + displayWidth = MeasureSpec.getSize(widthMeasureSpec); + } + if (displayWidth == 0 && getImageWidth() != 0) { + displayWidth = getImageWidth(); + } + if (displayHeight == 0 && getImageHeight() != 0) { + displayHeight = getImageHeight(); } - protected void setupCanvas(int measuredWidth, int measuredHeight, int orientation) { + setMeasuredDimension(displayWidth, displayHeight); + } - if (deviceOrientation != orientation) { - layout = false; - deviceOrientation = orientation; - } + @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + super.onLayout(changed, left, top, right, bottom); + if (changed) { + setupCanvas(displayWidth, displayHeight, getResources().getConfiguration().orientation); + } + } - if (drawable != null && !layout) { - int imageWidth = getImageWidth(); - int imageHeight = getImageHeight(); + protected void setupCanvas(int measuredWidth, int measuredHeight, int orientation) { - hWidth = Math.round(((float) imageWidth / 2.0f)); - hHeight = Math.round(((float) imageHeight / 2.0f)); - - measuredWidth -= (getPaddingLeft() + getPaddingRight()); - measuredHeight -= (getPaddingTop() + getPaddingBottom()); - - computeCropScale(imageWidth, imageHeight, measuredWidth, measuredHeight); - - if (startingScale <= 0.0f) { - computeStartingScale(imageWidth, imageHeight, measuredWidth, measuredHeight); - } - - scaleAdjust = startingScale; - - this.centerX = (float) measuredWidth / 2.0f; - this.centerY = (float) measuredHeight / 2.0f; - - if (startX == null) { - x = centerX; - } else { - x = startX; - } - - if (startY == null) { - y = centerY; - } else { - y = startY; - } - - gestureImageViewTouchListener = new GestureImageViewTouchListener(this, measuredWidth, measuredHeight); - - if (isLandscape()) { - gestureImageViewTouchListener.setMinScale(minScale * fitScaleHorizontal); - } else { - gestureImageViewTouchListener.setMinScale(minScale * fitScaleVertical); - } - - - gestureImageViewTouchListener.setMaxScale(maxScale * startingScale); - - gestureImageViewTouchListener.setFitScaleHorizontal(fitScaleHorizontal); - gestureImageViewTouchListener.setFitScaleVertical(fitScaleVertical); - gestureImageViewTouchListener.setCanvasWidth(measuredWidth); - gestureImageViewTouchListener.setCanvasHeight(measuredHeight); - gestureImageViewTouchListener.setOnClickListener(onClickListener); - - drawable.setBounds(-hWidth, -hHeight, hWidth, hHeight); - - super.setOnTouchListener(new OnTouchListener() { - @Override - public boolean onTouch(View v, MotionEvent event) { - if (customOnTouchListener != null) { - customOnTouchListener.onTouch(v, event); - } - return gestureImageViewTouchListener.onTouch(v, event); - } - }); - - layout = true; - } + if (deviceOrientation != orientation) { + deviceOrientation = orientation; } - protected void computeCropScale(int imageWidth, int imageHeight, int measuredWidth, int measuredHeight) { - fitScaleHorizontal = (float) measuredWidth / (float) imageWidth; - fitScaleVertical = (float) measuredHeight / (float) imageHeight; - } + if (drawable != null) { + int imageWidth = getImageWidth(); + int imageHeight = getImageHeight(); - protected void computeStartingScale(int imageWidth, int imageHeight, int measuredWidth, int measuredHeight) { - switch (getScaleType()) { - case CENTER: - // Center the image in the view, but perform no scaling. - startingScale = 1.0f; - break; + hWidth = Math.round(((float) imageWidth / 2.0f)); + hHeight = Math.round(((float) imageHeight / 2.0f)); - case CENTER_CROP: - // Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions - // (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). - startingScale = Math.max((float) measuredHeight / (float) imageHeight, (float) measuredWidth / (float) imageWidth); - break; + measuredWidth -= (getPaddingLeft() + getPaddingRight()); + measuredHeight -= (getPaddingTop() + getPaddingBottom()); - case CENTER_INSIDE: + computeCropScale(imageWidth, imageHeight, measuredWidth, measuredHeight); - // Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions - // (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). - float wRatio = (float) imageWidth / (float) measuredWidth; - float hRatio = (float) imageHeight / (float) measuredHeight; + if (startingScale <= 0.0f) { + computeStartingScale(imageWidth, imageHeight, measuredWidth, measuredHeight); + } - if (wRatio > hRatio) { - startingScale = fitScaleHorizontal; - } else { - startingScale = fitScaleVertical; - } + scaleAdjust = startingScale; - break; - } - } + this.centerX = (float) measuredWidth / 2.0f; + this.centerY = (float) measuredHeight / 2.0f; - protected boolean isRecycled() { - if (drawable != null && drawable instanceof BitmapDrawable) { - Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); - if (bitmap != null) { - return bitmap.isRecycled(); - } - } - return false; - } - - protected void recycle() { - if (recycle && drawable != null && drawable instanceof BitmapDrawable) { - Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); - if (bitmap != null) { - bitmap.recycle(); - } - } - } - - @Override - protected void onDraw(Canvas canvas) { - if (layout) { - if (drawable != null && !isRecycled()) { - canvas.save(); - - float adjustedScale = scale * scaleAdjust; - - canvas.translate(x, y); - - if (rotation != 0.0f) { - canvas.rotate(rotation); - } - - if (adjustedScale != 1.0f) { - canvas.scale(adjustedScale, adjustedScale); - } - - drawable.draw(canvas); - - canvas.restore(); - } - - if (drawLock.availablePermits() <= 0) { - drawLock.release(); - } - } - } - - /** - * Waits for a draw - * - * @param max time to wait for draw (ms) - * @throws InterruptedException - */ - public boolean waitForDraw(long timeout) throws InterruptedException { - return drawLock.tryAcquire(timeout, TimeUnit.MILLISECONDS); - } - - @Override - protected void onAttachedToWindow() { - animator = new Animator(this, "GestureImageViewAnimator"); - animator.start(); - - if (resId >= 0 && drawable == null) { - setImageResource(resId); - } - - super.onAttachedToWindow(); - } - - public void animationStart(Animation animation) { - if (animator != null) { - animator.play(animation); - } - } - - public void animationStop() { - if (animator != null) { - animator.cancel(); - } - } - - @Override - protected void onDetachedFromWindow() { - if (animator != null) { - animator.finish(); - } - if (recycle && drawable != null && !isRecycled()) { - recycle(); - drawable = null; - } - super.onDetachedFromWindow(); - } - - protected void initImage() { - if (this.drawable != null) { - this.drawable.setAlpha(alpha); - this.drawable.setFilterBitmap(true); - if (colorFilter != null) { - this.drawable.setColorFilter(colorFilter); - } - } - - if (!layout) { - requestLayout(); - redraw(); - } - } - - public void setImageBitmap(Bitmap image) { - this.drawable = new BitmapDrawable(getResources(), image); - initImage(); - } - - @Override - public void setImageDrawable(Drawable drawable) { - this.drawable = drawable; - initImage(); - } - - public void setImageResource(int id) { - if (this.drawable != null) { - this.recycle(); - } - if (id >= 0) { - this.resId = id; - setImageDrawable(getContext().getResources().getDrawable(id)); - } - } - - public int getScaledWidth() { - return Math.round(getImageWidth() * getScale()); - } - - public int getScaledHeight() { - return Math.round(getImageHeight() * getScale()); - } - - public int getImageWidth() { - if (drawable != null) { - return drawable.getIntrinsicWidth(); - } - return 0; - } - - public int getImageHeight() { - if (drawable != null) { - return drawable.getIntrinsicHeight(); - } - return 0; - } - - public void moveBy(float x, float y) { - this.x += x; - this.y += y; - } - - public void setPosition(float x, float y) { - this.x = x; - this.y = y; - } - - public void redraw() { - postInvalidate(); - } - - public void setMinScale(float min) { - this.minScale = min; - if (gestureImageViewTouchListener != null) { - gestureImageViewTouchListener.setMinScale(min * fitScaleHorizontal); - } - } - - public void setMaxScale(float max) { - this.maxScale = max; - if (gestureImageViewTouchListener != null) { - gestureImageViewTouchListener.setMaxScale(max * startingScale); - } - } - - public void setScale(float scale) { - scaleAdjust = scale; - } - - public float getScale() { - return scaleAdjust; - } - - public float getImageX() { - return x; - } - - public float getImageY() { - return y; - } - - public boolean isStrict() { - return strict; - } - - public void setStrict(boolean strict) { - this.strict = strict; - } - - public boolean isRecycle() { - return recycle; - } - - public void setRecycle(boolean recycle) { - this.recycle = recycle; - } - - public void reset() { + if (startX == null) { x = centerX; + } else { + x = startX; + } + + if (startY == null) { y = centerY; - scaleAdjust = startingScale; - if (gestureImageViewTouchListener != null) { - gestureImageViewTouchListener.reset(); + } else { + y = startY; + } + + gestureImageViewTouchListener = new GestureImageViewTouchListener(this, measuredWidth, measuredHeight); + + if (isLandscape()) { + gestureImageViewTouchListener.setMinScale(minScale * fitScaleHorizontal); + } else { + gestureImageViewTouchListener.setMinScale(minScale * fitScaleVertical); + } + + gestureImageViewTouchListener.setMaxScale(maxScale * startingScale); + + gestureImageViewTouchListener.setFitScaleHorizontal(fitScaleHorizontal); + gestureImageViewTouchListener.setFitScaleVertical(fitScaleVertical); + gestureImageViewTouchListener.setCanvasWidth(measuredWidth); + gestureImageViewTouchListener.setCanvasHeight(measuredHeight); + gestureImageViewTouchListener.setOnClickListener(onClickListener); + + drawable.setBounds(-hWidth, -hHeight, hWidth, hHeight); + + super.setOnTouchListener(new OnTouchListener() { + @Override public boolean onTouch(View v, MotionEvent event) { + if (customOnTouchListener != null) { + customOnTouchListener.onTouch(v, event); + } + return gestureImageViewTouchListener.onTouch(v, event); } - redraw(); + }); } + } - public void setRotation(float rotation) { - this.rotation = rotation; - } + protected void computeCropScale(int imageWidth, int imageHeight, int measuredWidth, int measuredHeight) { + fitScaleHorizontal = (float) measuredWidth / (float) imageWidth; + fitScaleVertical = (float) measuredHeight / (float) imageHeight; + } - public void setGestureImageViewListener(GestureImageViewListener pinchImageViewListener) { - this.gestureImageViewListener = pinchImageViewListener; - } + protected void computeStartingScale(int imageWidth, int imageHeight, int measuredWidth, int measuredHeight) { + switch (getScaleType()) { + case CENTER: + // Center the image in the view, but perform no scaling. + startingScale = 1.0f; + break; - public GestureImageViewListener getGestureImageViewListener() { - return gestureImageViewListener; - } + case CENTER_CROP: + // Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions + // (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). + startingScale = + Math.max((float) measuredHeight / (float) imageHeight, (float) measuredWidth / (float) imageWidth); + break; - @Override - public Drawable getDrawable() { - return drawable; - } + case CENTER_INSIDE: - @Override - public void setAlpha(int alpha) { - this.alpha = alpha; - if (drawable != null) { - drawable.setAlpha(alpha); - } - } + // Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions + // (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). + float wRatio = (float) imageWidth / (float) measuredWidth; + float hRatio = (float) imageHeight / (float) measuredHeight; - @Override - public void setColorFilter(ColorFilter cf) { - this.colorFilter = cf; - if (drawable != null) { - drawable.setColorFilter(cf); - } - } - - @Override - public void setImageURI(Uri mUri) { - if ("content".equals(mUri.getScheme())) { - try { - String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; - - Cursor cur = getContext().getContentResolver().query(mUri, orientationColumn, null, null, null); - - if (cur != null && cur.moveToFirst()) { - imageOrientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); - } - - InputStream in = null; - - try { - in = getContext().getContentResolver().openInputStream(mUri); - Bitmap bmp = BitmapFactory.decodeStream(in); - - if (imageOrientation != 0) { - Matrix m = new Matrix(); - m.postRotate(imageOrientation); - Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true); - bmp.recycle(); - setImageDrawable(new BitmapDrawable(getResources(), rotated)); - } else { - setImageDrawable(new BitmapDrawable(getResources(), bmp)); - } - } finally { - if (in != null) { - in.close(); - } - - if (cur != null) { - cur.close(); - } - } - } catch (Exception e) { - Log.w("GestureImageView", "Unable to open content: " + mUri, e); - } + if (wRatio > hRatio) { + startingScale = fitScaleHorizontal; } else { - setImageDrawable(Drawable.createFromPath(mUri.toString())); + startingScale = fitScaleVertical; } - if (drawable == null) { - Log.e("GestureImageView", "resolveUri failed on bad bitmap uri: " + mUri); - // Don't try again. - mUri = null; + break; + } + } + + protected boolean isRecycled() { + if (drawable != null && drawable instanceof BitmapDrawable) { + Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); + if (bitmap != null) { + return bitmap.isRecycled(); + } + } + return false; + } + + protected void recycle() { + if (recycle && drawable != null && drawable instanceof BitmapDrawable) { + Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); + if (bitmap != null) { + bitmap.recycle(); + } + } + } + + @Override protected void onDraw(Canvas canvas) { + if (true) { + if (drawable != null && !isRecycled()) { + if (drawableHeight == 0 && drawableWidth == 0) { + return; } - } + canvas.save(); - @Override - public Matrix getImageMatrix() { - if (strict) { - throw new UnsupportedOperationException("Not supported"); + float adjustedScale = scale * scaleAdjust; + + canvas.translate(x, y); + + if (rotation != 0.0f) { + canvas.rotate(rotation); } - return super.getImageMatrix(); - } - @Override - public void setScaleType(ScaleType scaleType) { - if (scaleType == ScaleType.CENTER || - scaleType == ScaleType.CENTER_CROP || - scaleType == ScaleType.CENTER_INSIDE) { - - super.setScaleType(scaleType); - } else if (strict) { - throw new UnsupportedOperationException("Not supported"); + if (adjustedScale != 1.0f) { + canvas.scale(adjustedScale, adjustedScale); } + //drawable.setBounds(0, 0, drawableWidth, drawableHeight); + drawable.draw(canvas); + + canvas.restore(); + } + + if (drawLock.availablePermits() <= 0) { + drawLock.release(); + } + } + } + + /** + * Waits for a draw + * + * @param max time to wait for draw (ms) + * @throws InterruptedException + */ + public boolean waitForDraw(long timeout) throws InterruptedException { + return drawLock.tryAcquire(timeout, TimeUnit.MILLISECONDS); + } + + @Override protected void onAttachedToWindow() { + animator = new Animator(this, "GestureImageViewAnimator"); + animator.start(); + + if (resId >= 0 && drawable == null) { + setImageResource(resId); } - @Override - public void invalidateDrawable(Drawable dr) { - if (strict) { - throw new UnsupportedOperationException("Not supported"); + super.onAttachedToWindow(); + } + + public void animationStart(Animation animation) { + if (animator != null) { + animator.play(animation); + } + } + + public void animationStop() { + if (animator != null) { + animator.cancel(); + } + } + + @Override protected void onDetachedFromWindow() { + if (animator != null) { + animator.finish(); + } + if (recycle && drawable != null && !isRecycled()) { + recycle(); + drawable = null; + } + super.onDetachedFromWindow(); + } + + protected void initImage() { + if (this.drawable != null) { + drawableWidth = drawable.getIntrinsicWidth(); + drawableHeight = drawable.getIntrinsicHeight(); + this.drawable.setAlpha(alpha); + this.drawable.setFilterBitmap(true); + if (colorFilter != null) { + this.drawable.setColorFilter(colorFilter); + } + } + + //if (!layout) { + requestLayout(); + redraw(); + //} + } + + public void setImageBitmap(Bitmap image) { + this.drawable = new BitmapDrawable(getResources(), image); + initImage(); + } + + @Override public void setImageDrawable(Drawable drawable) { + this.drawable = drawable; + initImage(); + } + + public void setImageResource(int id) { + if (this.drawable != null) { + this.recycle(); + } + if (id >= 0) { + this.resId = id; + setImageDrawable(getContext().getResources().getDrawable(id)); + } + } + + public int getScaledWidth() { + return Math.round(getImageWidth() * getScale()); + } + + public int getScaledHeight() { + return Math.round(getImageHeight() * getScale()); + } + + public int getImageWidth() { + if (drawable != null) { + return drawable.getIntrinsicWidth(); + } + return 0; + } + + public int getImageHeight() { + if (drawable != null) { + return drawable.getIntrinsicHeight(); + } + return 0; + } + + public void moveBy(float x, float y) { + this.x += x; + this.y += y; + } + + public void setPosition(float x, float y) { + this.x = x; + this.y = y; + } + + public void redraw() { + postInvalidate(); + } + + public void setMinScale(float min) { + this.minScale = min; + if (gestureImageViewTouchListener != null) { + gestureImageViewTouchListener.setMinScale(min * fitScaleHorizontal); + } + } + + public void setMaxScale(float max) { + this.maxScale = max; + if (gestureImageViewTouchListener != null) { + gestureImageViewTouchListener.setMaxScale(max * startingScale); + } + } + + public void setScale(float scale) { + scaleAdjust = scale; + } + + public float getScale() { + return scaleAdjust; + } + + public float getImageX() { + return x; + } + + public float getImageY() { + return y; + } + + public boolean isStrict() { + return strict; + } + + public void setStrict(boolean strict) { + this.strict = strict; + } + + public boolean isRecycle() { + return recycle; + } + + public void setRecycle(boolean recycle) { + this.recycle = recycle; + } + + public void reset() { + x = centerX; + y = centerY; + scaleAdjust = startingScale; + if (gestureImageViewTouchListener != null) { + gestureImageViewTouchListener.reset(); + } + redraw(); + } + + public void setRotation(float rotation) { + this.rotation = rotation; + } + + public void setGestureImageViewListener(GestureImageViewListener pinchImageViewListener) { + this.gestureImageViewListener = pinchImageViewListener; + } + + public GestureImageViewListener getGestureImageViewListener() { + return gestureImageViewListener; + } + + @Override public Drawable getDrawable() { + return drawable; + } + + @Override public void setAlpha(int alpha) { + this.alpha = alpha; + if (drawable != null) { + drawable.setAlpha(alpha); + } + } + + @Override public void setColorFilter(ColorFilter cf) { + this.colorFilter = cf; + if (drawable != null) { + drawable.setColorFilter(cf); + } + } + + @Override public void setImageURI(Uri mUri) { + if ("content".equals(mUri.getScheme())) { + try { + String[] orientationColumn = { MediaStore.Images.Media.ORIENTATION }; + + Cursor cur = getContext().getContentResolver().query(mUri, orientationColumn, null, null, null); + + if (cur != null && cur.moveToFirst()) { + imageOrientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); } - super.invalidateDrawable(dr); - } - @Override - public int[] onCreateDrawableState(int extraSpace) { - if (strict) { - throw new UnsupportedOperationException("Not supported"); + InputStream in = null; + + try { + in = getContext().getContentResolver().openInputStream(mUri); + Bitmap bmp = BitmapFactory.decodeStream(in); + + if (imageOrientation != 0) { + Matrix m = new Matrix(); + m.postRotate(imageOrientation); + Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true); + bmp.recycle(); + setImageDrawable(new BitmapDrawable(getResources(), rotated)); + } else { + setImageDrawable(new BitmapDrawable(getResources(), bmp)); + } + } finally { + if (in != null) { + in.close(); + } + + if (cur != null) { + cur.close(); + } } - return super.onCreateDrawableState(extraSpace); + } catch (Exception e) { + Log.w("GestureImageView", "Unable to open content: " + mUri, e); + } + } else { + setImageDrawable(Drawable.createFromPath(mUri.toString())); } - @Override - public void setAdjustViewBounds(boolean adjustViewBounds) { - if (strict) { - throw new UnsupportedOperationException("Not supported"); - } - super.setAdjustViewBounds(adjustViewBounds); + if (drawable == null) { + Log.e("GestureImageView", "resolveUri failed on bad bitmap uri: " + mUri); + // Don't try again. + mUri = null; } + } - @Override - public void setImageLevel(int level) { - if (strict) { - throw new UnsupportedOperationException("Not supported"); - } - super.setImageLevel(level); + @Override public Matrix getImageMatrix() { + if (strict) { + throw new UnsupportedOperationException("Not supported"); } + return super.getImageMatrix(); + } - @Override - public void setImageMatrix(Matrix matrix) { - if (strict) { - throw new UnsupportedOperationException("Not supported"); - } + @Override public void setScaleType(ScaleType scaleType) { + if (scaleType == ScaleType.CENTER || + scaleType == ScaleType.CENTER_CROP || + scaleType == ScaleType.CENTER_INSIDE) { + + super.setScaleType(scaleType); + } else if (strict) { + throw new UnsupportedOperationException("Not supported"); } + } - @Override - public void setImageState(int[] state, boolean merge) { - if (strict) { - throw new UnsupportedOperationException("Not supported"); - } + @Override public void invalidateDrawable(Drawable dr) { + if (strict) { + throw new UnsupportedOperationException("Not supported"); } + super.invalidateDrawable(dr); + } - @Override - public void setSelected(boolean selected) { - if (strict) { - throw new UnsupportedOperationException("Not supported"); - } - super.setSelected(selected); + @Override public int[] onCreateDrawableState(int extraSpace) { + if (strict) { + throw new UnsupportedOperationException("Not supported"); } + return super.onCreateDrawableState(extraSpace); + } - @Override - public void setOnTouchListener(OnTouchListener l) { - this.customOnTouchListener = l; + @Override public void setAdjustViewBounds(boolean adjustViewBounds) { + if (strict) { + throw new UnsupportedOperationException("Not supported"); } + super.setAdjustViewBounds(adjustViewBounds); + } - public float getCenterX() { - return centerX; + @Override public void setImageLevel(int level) { + if (strict) { + throw new UnsupportedOperationException("Not supported"); } + super.setImageLevel(level); + } - public float getCenterY() { - return centerY; + @Override public void setImageMatrix(Matrix matrix) { + if (strict) { + throw new UnsupportedOperationException("Not supported"); } + } - public boolean isLandscape() { - return getImageWidth() >= getImageHeight(); + @Override public void setImageState(int[] state, boolean merge) { + if (strict) { + throw new UnsupportedOperationException("Not supported"); } + } - public boolean isPortrait() { - return getImageWidth() <= getImageHeight(); + @Override public void setSelected(boolean selected) { + if (strict) { + throw new UnsupportedOperationException("Not supported"); } + super.setSelected(selected); + } - public void setStartingScale(float startingScale) { - this.startingScale = startingScale; + @Override public void setOnTouchListener(OnTouchListener l) { + this.customOnTouchListener = l; + } + + public float getCenterX() { + return centerX; + } + + public float getCenterY() { + return centerY; + } + + public boolean isLandscape() { + return getImageWidth() >= getImageHeight(); + } + + public boolean isPortrait() { + return getImageWidth() <= getImageHeight(); + } + + public void setStartingScale(float startingScale) { + this.startingScale = startingScale; + } + + public void setStartingPosition(float x, float y) { + this.startX = x; + this.startY = y; + } + + @Override public void setOnClickListener(OnClickListener l) { + this.onClickListener = l; + + if (gestureImageViewTouchListener != null) { + gestureImageViewTouchListener.setOnClickListener(l); } + } - public void setStartingPosition(float x, float y) { - this.startX = x; - this.startY = y; + /** + * Returns true if the image dimensions are aligned with the orientation of the device. + */ + public boolean isOrientationAligned() { + if (deviceOrientation == Configuration.ORIENTATION_LANDSCAPE) { + return isLandscape(); + } else if (deviceOrientation == Configuration.ORIENTATION_PORTRAIT) { + return isPortrait(); } + return true; + } - @Override - public void setOnClickListener(OnClickListener l) { - this.onClickListener = l; - - if (gestureImageViewTouchListener != null) { - gestureImageViewTouchListener.setOnClickListener(l); - } - } - - /** - * Returns true if the image dimensions are aligned with the orientation of the device. - * - * @return - */ - public boolean isOrientationAligned() { - if (deviceOrientation == Configuration.ORIENTATION_LANDSCAPE) { - return isLandscape(); - } else if (deviceOrientation == Configuration.ORIENTATION_PORTRAIT) { - return isPortrait(); - } - return true; - } - - public int getDeviceOrientation() { - return deviceOrientation; - } + public int getDeviceOrientation() { + return deviceOrientation; + } } diff --git a/pdfviewsample/src/main/AndroidManifest.xml b/pdfviewsample/src/main/AndroidManifest.xml index 34c1e06..e26ea16 100644 --- a/pdfviewsample/src/main/AndroidManifest.xml +++ b/pdfviewsample/src/main/AndroidManifest.xml @@ -1,21 +1,23 @@ + package="com.wyx.pdfviewsample" + > - - - - + + - - - - + + + + - - + + + + diff --git a/pdfviewsample/src/main/java/com/wyx/pdfviewsample/L.java b/pdfviewsample/src/main/java/com/wyx/pdfviewsample/L.java new file mode 100644 index 0000000..62a1242 --- /dev/null +++ b/pdfviewsample/src/main/java/com/wyx/pdfviewsample/L.java @@ -0,0 +1,242 @@ +package com.wyx.pdfviewsample; + +import android.os.Environment; +import android.text.TextUtils; +import android.util.Log; +import android.view.MotionEvent; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +/** + * Log record tool + */ +@SuppressWarnings({ "unused", "ResultOfMethodCallIgnored" }) public class L { + + private static final int LOG_CAT_MAX_LENGTH = 3900; + + private static final String TAG_LINE_BREAK = "****"; + private static final String EMPTY_LOG = "---"; + + private static final String ROOT = Environment.getExternalStorageDirectory().getAbsolutePath(); + private static final String FILE_NAME = "logger.log"; + private static final int WRITE_TO_SD_PRIORITY_LEVEL = Log.DEBUG; + + private static String logFile = ROOT + "/" + FILE_NAME; + private static boolean write2SdCard = false; + private static int write2SdPriorityLevel = WRITE_TO_SD_PRIORITY_LEVEL; + + private static boolean debug = true; + + public static void setDebug(boolean debug) { + L.debug = debug; + } + + public static void setWrite2SdCard(boolean sdCard) { + write2SdCard = sdCard; + } + + public static void setWriteToSdPriorityLevel(int level) { + write2SdPriorityLevel = level; + } + + public static void exception(Throwable e) { + if (debug && e != null) { + e.printStackTrace(); + } + } + + public static void exception(Throwable e, String s) { + if (debug && e != null) { + e.printStackTrace(); + e(TAG_LINE_BREAK, s); + } + } + + public static void w(Object object, Object msg) { + if (debug) { + print(Log.WARN, object, msg); + } + } + + public static void w(Object msg) { + if (debug) { + print(Log.WARN, TAG_LINE_BREAK, msg); + } + } + + public static void v(Object object, Object msg) { + if (debug) { + print(Log.VERBOSE, object, msg); + } + } + + public static void v(Object msg) { + if (debug) { + print(Log.VERBOSE, TAG_LINE_BREAK, msg); + } + } + + public static void d(Object object, Object msg) { + if (debug) { + print(Log.DEBUG, object, msg); + } + } + + public static void d(Object msg) { + if (debug) { + print(Log.DEBUG, TAG_LINE_BREAK, msg); + } + } + + public static void i(Object object, Object msg) { + if (debug) { + print(Log.INFO, object, msg); + } + } + + public static void i(Object msg) { + if (debug) { + print(Log.INFO, TAG_LINE_BREAK, msg); + } + } + + public static void e(Object object, Object msg) { + if (debug) { + print(Log.ERROR, object, msg); + } + } + + public static void e(Object msg) { + if (debug) { + print(Log.ERROR, TAG_LINE_BREAK, msg); + } + } + + private static void print(int priority, Object tag, Object msg) { + String s = toString(msg); + printToLogCat(priority, tag, s); + if (write2SdCard) { + writeLog(priority, tag, s); + } + } + + private static void printToLogCat(int priority, Object tag, String s) { + if (s.length() > LOG_CAT_MAX_LENGTH) { + println(priority, tag, "log length - " + String.valueOf(s.length())); + int chunkCount = s.length() / LOG_CAT_MAX_LENGTH; // integer division + for (int i = 0; i <= chunkCount; i++) { + int max = LOG_CAT_MAX_LENGTH * (i + 1); + if (max >= s.length()) { + println(priority, "chunk " + i + " of " + chunkCount, s.substring(LOG_CAT_MAX_LENGTH * i, s.length())); + } else { + println(priority, "chunk " + i + " of " + chunkCount, s.substring(LOG_CAT_MAX_LENGTH * i, max)); + } + } + } else { + println(priority, tag, s); + } + } + + public static void resetLogFile() { + File file = new File(logFile); + file.delete(); + try { + file.createNewFile(); + } catch (IOException e) { + exception(e); + } + } + + private static void writeLog(int priority, Object tag, String s) { + if (TextUtils.isEmpty(s)) { + return; + } + + if (priority < write2SdPriorityLevel) { + return; + } + + try { + File file = new File(logFile); + if (!file.exists()) { + file.createNewFile(); + } + FileWriter writer = new FileWriter(file, true); + writer.flush(); + writer.close(); + } catch (IOException e) { + exception(e); + } + } + + private static void println(int priority, Object tag, String s) { + Log.println(priority, getTagName(tag), s); + } + + private static String getTagName(Object tag) { + if (tag instanceof String) { + return (String) tag; + } + + if (tag instanceof Class) { + return ((Class) tag).getSimpleName(); + } else { + return getTagName(tag.getClass()); + } + } + + private static String toString(Object msg) { + if (msg == null) { + return EMPTY_LOG; + } + String s = msg.toString(); + if (s.isEmpty()) { + return EMPTY_LOG; + } else { + return s; + } + } + + public static void printTouchEvent(MotionEvent ev) { + L.e("touch event", actionToString(ev.getAction())); + final int pointerCount = ev.getPointerCount(); + for (int i = 0; i < pointerCount; i++) { + L.d("point", + "id[" + i + "]=" + ev.getPointerId(i) + ", x[" + i + "]=" + ev.getX(i) + ", y[" + i + "]=" + ev.getY(i)); + } + // L.d("pointer count", pointerCount); + } + + public static String actionToString(int action) { + switch (action) { + case MotionEvent.ACTION_DOWN: + return "ACTION_DOWN"; + case MotionEvent.ACTION_UP: + return "ACTION_UP"; + case MotionEvent.ACTION_CANCEL: + return "ACTION_CANCEL"; + case MotionEvent.ACTION_OUTSIDE: + return "ACTION_OUTSIDE"; + case MotionEvent.ACTION_MOVE: + return "ACTION_MOVE"; + case MotionEvent.ACTION_HOVER_MOVE: + return "ACTION_HOVER_MOVE"; + case MotionEvent.ACTION_SCROLL: + return "ACTION_SCROLL"; + case MotionEvent.ACTION_HOVER_ENTER: + return "ACTION_HOVER_ENTER"; + case MotionEvent.ACTION_HOVER_EXIT: + return "ACTION_HOVER_EXIT"; + } + int index = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; + switch (action & MotionEvent.ACTION_MASK) { + case MotionEvent.ACTION_POINTER_DOWN: + return "ACTION_POINTER_DOWN(" + index + ")"; + case MotionEvent.ACTION_POINTER_UP: + return "ACTION_POINTER_UP(" + index + ")"; + default: + return Integer.toString(action); + } + } +} diff --git a/pdfviewsample/src/main/java/com/wyx/pdfviewsample/Main2Activity.java b/pdfviewsample/src/main/java/com/wyx/pdfviewsample/Main2Activity.java index 2648f75..2a6f2b3 100644 --- a/pdfviewsample/src/main/java/com/wyx/pdfviewsample/Main2Activity.java +++ b/pdfviewsample/src/main/java/com/wyx/pdfviewsample/Main2Activity.java @@ -2,30 +2,40 @@ package com.wyx.pdfviewsample; import android.app.Activity; import android.os.Bundle; - +import android.view.View; +import java.io.IOException; import net.sf.andpdf.pdfviewer.gui.PdfView; import net.sf.andpdf.utils.FileUtils; -import java.io.IOException; - public class Main2Activity extends Activity { - PdfView pdfView; + PdfView pdfView; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main2); + @Override protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main2); - pdfView = (PdfView) findViewById(R.id.pdf_view); + pdfView = (PdfView) findViewById(R.id.pdf_view); - try { - pdfView.parsePDF(FileUtils.fileFromAsset(this, "about.pdf"), null); - } catch (IOException e) { - e.printStackTrace(); - } - pdfView.startRenderThread(1, 1.0f); + //ViewGroup.LayoutParams params = + // new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); + //GestureImageView view = pdfView.mImageView; + //view.setImageResource(R.drawable.back02); + //view.setLayoutParams(params); + //ViewGroup layout = (ViewGroup) findViewById(R.id.layout); + //layout.addView(view); + View view = pdfView.mImageView; + } + + @Override protected void onStart() { + super.onStart(); + try { + pdfView.parsePDF(FileUtils.fileFromAsset(this, "about.pdf"), null); + } catch (IOException e) { + e.printStackTrace(); } + pdfView.startRenderThread(1, 1.0f); + } } diff --git a/pdfviewsample/src/main/res/layout/activity_main2.xml b/pdfviewsample/src/main/res/layout/activity_main2.xml index fc0e60a..45a1003 100644 --- a/pdfviewsample/src/main/res/layout/activity_main2.xml +++ b/pdfviewsample/src/main/res/layout/activity_main2.xml @@ -1,14 +1,18 @@ + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:id="@+id/layout" + android:orientation="vertical" + > + + -