From cef556c0cb83ff76983f16638b46173d0f920e08 Mon Sep 17 00:00:00 2001 From: yangkun <919079498@qq.com> Date: Tue, 28 May 2019 15:08:02 +0800 Subject: [PATCH] =?UTF-8?q?1.0.2=20=E5=A2=9E=E5=8A=A0=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E9=95=BF=E5=9B=BE=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=BF=9B=E5=BA=A6=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/vcs.xml | 6 + README.md | 2 + app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 1 + .../java/com/yk/big_picture/MainActivity.java | 43 +++- .../com/yk/big_picture_library/BigView.java | 235 +++++++++++++----- .../big_picture_library/HttpBitmapUtils.java | 221 ++++++++++++++++ .../LoadNetImageCallBack.java | 11 + build.gradle | 1 + 9 files changed, 458 insertions(+), 63 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 big_picture_library/src/main/java/com/yk/big_picture_library/HttpBitmapUtils.java create mode 100644 big_picture_library/src/main/java/com/yk/big_picture_library/LoadNetImageCallBack.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 881d14d..d6b2231 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ bigView.setImage(BIG_IMAGE_PAHT); //加载 InputStream bigView.setImage(InputStream is) + //加载网络图片 callBack : 加载中的回调 + bigView.setNetUrl(String url,LoadNetImageCallBack callBack); ``` ### 效果对比 diff --git a/app/build.gradle b/app/build.gradle index 1c8e003..adef68a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,4 +26,5 @@ dependencies { androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation project(path: ':big_picture_library') +// implementation 'com.github.yangkun19921001:long_picture_view:1.0.1' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 48b648f..134d3b9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ + mImageHeight){ - mRect.bottom=mImageHeight; - mRect.top=mImageHeight-(int)(mViewHeight/mScale); + if (mRect.bottom > mImageHeight) { + mRect.bottom = mImageHeight; + mRect.top = mImageHeight - (int) (mViewHeight / mScale); } - if(mRect.top<0){ - mRect.top=0; - mRect.bottom=(int)(mViewHeight/mScale); + if (mRect.top < 0) { + mRect.top = 0; + mRect.bottom = (int) (mViewHeight / mScale); } invalidate(); return false; @@ -206,33 +294,35 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d /** * 处理惯性问题 + * * @param e1 * @param e2 - * @param velocityX 每秒移动的x点 + * @param velocityX 每秒移动的x点 * @param velocityY * @return */ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //做计算 - mScroller.fling(0,mRect.top, - 0,(int)-velocityY, - 0,0, - 0,mImageHeight-(int)(mViewHeight/mScale)); + mScroller.fling(0, mRect.top, + 0, (int) -velocityY, + 0, 0, + 0, mImageHeight - (int) (mViewHeight / mScale)); return false; } + /* 使用上一个接口的计算结果 */ @Override public void computeScroll() { - if(mScroller.isFinished()){ + if (mScroller.isFinished()) { return; } //true 表示当前滑动还没有结束 - if(mScroller.computeScrollOffset()){ - mRect.top=mScroller.getCurrY(); - mRect.bottom=mRect.top+(int)(mViewHeight/mScale); + if (mScroller.computeScrollOffset()) { + mRect.top = mScroller.getCurrY(); + mRect.bottom = mRect.top + (int) (mViewHeight / mScale); invalidate(); } } @@ -240,19 +330,48 @@ public void computeScroll() { @Override public void onShowPress(MotionEvent e) { } + @Override public boolean onSingleTapUp(MotionEvent e) { return false; } + @Override public void onLongPress(MotionEvent e) { } + //下载图片 + private InputStream downloadBitmap(String url) { + Bitmap bitmap = null; + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(3000); + conn.setReadTimeout(6000); + conn.connect(); + int responseCode = conn.getResponseCode(); + if (responseCode == 200) { + InputStream inputStream = conn.getInputStream(); + //把流转换成Bitmap对象 +// bitmap = BitmapFactory.decodeStream(inputStream); + return inputStream; + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + return null; + } +} + -} diff --git a/big_picture_library/src/main/java/com/yk/big_picture_library/HttpBitmapUtils.java b/big_picture_library/src/main/java/com/yk/big_picture_library/HttpBitmapUtils.java new file mode 100644 index 0000000..2d02f44 --- /dev/null +++ b/big_picture_library/src/main/java/com/yk/big_picture_library/HttpBitmapUtils.java @@ -0,0 +1,221 @@ +package com.yk.big_picture_library; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.AsyncTask; +import android.os.Environment; +import android.os.SystemClock; +import android.util.Log; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import static android.content.ContentValues.TAG; + +/** + * 获取服务器图片工具类 + */ +public class HttpBitmapUtils { + + private static DownCallListener downCallListener; + private static DownLoadAsyncTask mDownLoadAsyncTask; + private static final String ALBUM_PATH = Environment.getExternalStorageDirectory() + "/BigView/"; + + + public Bitmap downBitmap(String urlPath) { + Bitmap bitmap = null; + try { + URL url = new URL(urlPath); + HttpURLConnection httpc = (HttpURLConnection) url.openConnection(); + httpc.setConnectTimeout(60 * 1000); + httpc.setReadTimeout(60 * 1000); + if (httpc.getResponseCode() == 200) { + InputStream in = httpc.getInputStream(); + //BitmapFactory->不同方式读取图片进入程序中 + bitmap = BitmapFactory.decodeStream(in); + } + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return bitmap; + } + + + public static void loadImage(String url, DownCallListener down) { + downCallListener = down; + mDownLoadAsyncTask = new DownLoadAsyncTask(); + mDownLoadAsyncTask.execute(url); + } + + public void cancel() { + if (mDownLoadAsyncTask != null && mDownLoadAsyncTask.getStatus() == AsyncTask.Status.RUNNING) { + mDownLoadAsyncTask.cancel(true); + } + } + + + /** + * 使用 异步任务下载图片 并显示进度 + * 参数1 String 就是 doInbackground() 的参数类型 我们的代码就在这里写 系统默认调用 + * 参数2 Integer onProgressUpdate() 的参数类型 系统不会自动调用此方法 手动调用:publishProgress() + * 参数3 Bitmap doInbackground() 的返回值类型 也是 onPostExecute() 的参数类型 + */ + static class DownLoadAsyncTask extends AsyncTask { + + + /** + * 在 doInbackground() 执行前,系统自动调用 在主线程运行 + */ + @Override + protected void onPreExecute() { + Log.i(TAG, "开始下载----"); + if (downCallListener != null) { + downCallListener.onPreExecute(); + } + } + + /** + * 不在主线程 执行 + * + * @param strUrl url + * @return 位图 + */ + @Override + protected String doInBackground(String... strUrl) { + HttpURLConnection connection = null; + try { + URL url = new URL(strUrl[0]); + connection = (HttpURLConnection) url.openConnection(); + connection.setDoInput(true); + connection.setConnectTimeout(20000); + int code = connection.getResponseCode(); + if (code == 200) { + //为了显示进度条这里使用 字节数组输出流 + InputStream is = connection.getInputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int length = -1; + int progress = 0; //进度 + int count = connection.getContentLength(); //获取内容产固定 + byte[] bs = new byte[1024]; + while ((length = is.read(bs)) != -1) { + progress += length; //进度累加 + if (count == 0) { + publishProgress(-1); + } else { + //进度值改变通知 + publishProgress((int) ((float) progress / count * 100)); + } + + Log.d("Tag", "=任务是否取消:" + isCancelled() + "=======任务进度:" + (int) ((float) progress / count * 100) + "%"); + if (isCancelled()) {//如果取消了任务 就不执行 + return null; + } + + bos.write(bs, 0, length); + } + Log.d("Tag", "=========任务完成"); + return saveFile(BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.size())); + + + } + } catch (Exception e) { + e.printStackTrace(); + if (downCallListener != null) { + downCallListener.onLoadError(e); + } + } finally { + if (connection != null) { + connection.disconnect(); + } + } + return null; + } + + /** + * 在 doInbackground() 执行后 系统自动调用 在主线程运行 + * + * @param filePathName 位图 + */ + @Override + protected void onPostExecute(String filePathName) { + Log.d("Tag", "===============任务是否取消:" + isCancelled()); + if (downCallListener != null) { + downCallListener.onPostExecute(filePathName); + } + } + + /** + * 系统不会自动调用 使用 publishProgress() 调用 + * 在主线程执行 + * + * @param values + */ + @Override + protected void onProgressUpdate(Integer... values) { + int progress = values[0]; //进度值 + if (progress != -1) { + Log.d(TAG, "下载的进度--》" + values + ""); + if (downCallListener != null) { + downCallListener.onProgressUpdate(progress); + } + } + } + } + + /** + * 保存文件 + * @param bm + * @throws IOException + */ + public static String saveFile(Bitmap bm) throws IOException { + File dirFile = new File(ALBUM_PATH); + if(!dirFile.exists()){ + dirFile.mkdir(); + } + File myCaptureFile = new File(ALBUM_PATH + SystemClock.currentThreadTimeMillis() +".png"); + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); + bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); + bos.flush(); + bos.close(); + return myCaptureFile.getAbsolutePath(); + } + + + public interface DownCallListener { + + /** + * 开始下载 + */ + void onPreExecute(); + + /** + * 下载进度 + */ + void onProgressUpdate(int progress); + + /** + * 下载完成 + */ + + void onPostExecute(String bitmap); + + /** + * 加载失败 + * @param e + */ + void onLoadError(Exception e); + } + +} + diff --git a/big_picture_library/src/main/java/com/yk/big_picture_library/LoadNetImageCallBack.java b/big_picture_library/src/main/java/com/yk/big_picture_library/LoadNetImageCallBack.java new file mode 100644 index 0000000..b600821 --- /dev/null +++ b/big_picture_library/src/main/java/com/yk/big_picture_library/LoadNetImageCallBack.java @@ -0,0 +1,11 @@ +package com.yk.big_picture_library; + +public interface LoadNetImageCallBack { + void onStart(); + + void onLoadSucceed(); + + void onLoadFail(Exception e); + + void onLoadProgress(int progress); +} diff --git a/build.gradle b/build.gradle index fafc1b9..8cbe0fb 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,7 @@ allprojects { repositories { google() jcenter() + maven { url 'https://jitpack.io' } } }