有遇到沒有這樣的1種需求:閱讀的大圖后,點擊保存下載高清原圖到相冊的需求。
現在的圖片緩存大多都是Universal-Imager-Loader為多。但是我們在公司的某些離譜的需求(圈子系的需求,要求每條動態展現的圖片不止6張,有1條30多張,直接報了Universal-Imager-Loader的OOM,怎樣改配置都不能解決)上,后來調研了Glide和Fresco,發現Glide基本是完虐的節奏。
這里給大家提1個Glide小問題:Glide自帶能把圖片加載成圓角,Glide加載本地圖片不能實現圓角。
dialog_progressbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:padding="10dip" /> <TextView android:id="@+id/load_info_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dip" android:text="正在提交..." android:textColor="#000000" /> LinearLayout>
CustomProgressBarDialog
/** * 自定義進度條Dialog */ public class CustomProgressBarDialog extends Dialog { private LayoutInflater mInflater; private Context mContext; private WindowManager.LayoutParams params; private View mView; private TextView promptTV; public CustomProgressBarDialog(Context context) { super(context); this.init(context); } public CustomProgressBarDialog(Context context, int themeResId) { super(context, themeResId); this.init(context); } protected CustomProgressBarDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); this.init(context); } private void init(Context context) { requestWindowFeature(Window.FEATURE_NO_TITLE); this.mContext = context; this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mView = this.mInflater.inflate(R.layout.dialog_progressbar, null); setContentView(this.mView); // 設置window屬性 this.params = getWindow().getAttributes(); this.params.gravity = Gravity.CENTER; // 去背景遮蓋 this.params.dimAmount = 0; this.params.alpha = 1.0f; // 不能關掉 this.setCancelable(false); this.getWindow().setAttributes(this.params); this.promptTV = (TextView) findViewById(R.id.load_info_text); } /** * 設置內容 * * @param prompt */ public void setLoadPrompt(String prompt) { this.promptTV.setText(prompt); } }
注意:這里可以參考Handler 通用模板。
private static final int HANDLER_LOADING = 262; /** * 刷新Dialog顯示的進度Handler */ private static class LoadingHandler extends Handler { private final WeakReferencemActivity; public LoadingHandler(DownloadImageToGalleryActivity activity) { mActivity = new WeakReference<>(activity); } /** * Subclasses must implement this to receive messages. * * @param msg */ @Override public void handleMessage(Message msg) { DownloadImageToGalleryActivity activity = this.mActivity.get(); if (activity != null) { switch (msg.what) { case HANDLER_LOADING: { int progressValue = (int) msg.obj; activity.dialog.setLoadPrompt(progressValue + "%"); activity.dialog.show(); break; } } } } } private final LoadingHandler loadingHandler = new LoadingHandler(DownloadImageToGalleryActivity.this);
注意:這里可以參考AsyncTask 模板。
/** * 下載圖片異步任務 */ public class DownloadImageAsyncTask extends AsyncTask<String, Integer, String> { private Activity activity; private String localFilePath; public DownloadImageAsyncTask(Activity activity) { super(); this.activity = activity; } /** * 對應AsyncTask第1個參數 * 異步操作,不在主UI線程中,不能對控件進行修改 * 可以調用publishProgress方法中轉到onProgressUpdate(這里完成了1個handler.sendMessage(...)的進程) * * @param params The parameters of the task. * @return A result, defined by the subclass of this task. * @see #onPreExecute() * @see #onPostExecute * @see #publishProgress */ @Override protected String doInBackground(String... params) { // TODO 注意這里 /** * 這里接入你所用的網絡框架去下載圖片,只要保證this.localFilePath的值有就能夠了 */ URL fileUrl = null; try { fileUrl = new URL(params[0]); } catch (MalformedURLException e) { e.printStackTrace(); } if (fileUrl == null) return null; try { HttpURLConnection connection = (HttpURLConnection) fileUrl.openConnection(); connection.setRequestMethod("GET"); connection.setDoInput(true); connection.connect(); //計算文件長度 int lengthOfFile = connection.getContentLength(); /** * 不存在SD卡,就放到緩存文件夾內 */ File cacheDir = this.activity.getCacheDir(); File downloadFile = new File(cacheDir, UUID.randomUUID().toString() + ".jpg"); this.localFilePath = downloadFile.getPath(); if (!downloadFile.exists()) { File parent = downloadFile.getParentFile(); if (parent != null) parent.mkdirs(); } FileOutputStream output = new FileOutputStream(downloadFile); InputStream input = connection.getInputStream(); InputStream bitmapInput = connection.getInputStream(); //下載 byte[] buffer = new byte[1024]; int len; long total = 0; // 計算進度 while ((len = input.read(buffer)) > 0) { total += len; this.publishProgress((int) ((total * 100) / lengthOfFile)); output.write(buffer, 0, len); } output.close(); input.close(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 對應AsyncTask第3個參數 (接受doInBackground的返回值) * 在doInBackground方法履行結束以后在運行,此時已回來主UI線程當中 能對UI控件進行修改 * * @param string The result of the operation computed by {@link #doInBackground}. * @see #onPreExecute * @see #doInBackground * @see #onCancelled(Object) */ @Override protected void onPostExecute(String string) { /** * 設置按鈕可用,并隱藏Dialog */ DownloadImageToGalleryActivity.this.saveBT.setEnabled(true); DownloadImageToGalleryActivity.this.dialog.hide(); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int screenWidth = metrics.widthPixels; int screenHeight = metrics.heightPixels; /** * ImageUtil.decodeScaleImage 解析圖片 */ Bitmap bitmap = ImageUtil.decodeScaleImage(this.localFilePath, screenWidth, screenHeight); DownloadImageToGalleryActivity.this.saveIV.setImageBitmap(bitmap); /** * 保存圖片到相冊 */ String imageName = System.currentTimeMillis() + ".jpg"; MediaStore.Images.Media.insertImage(DownloadImageToGalleryActivity.this.getApplicationContext().getContentResolver(), bitmap, imageName, "牙醫助理"); Toast.makeText(this.activity, "已保存:" + imageName, Toast.LENGTH_LONG).show(); } /** * 對應AsyncTask第2個參數 * 在doInBackground方法當中,每次調用publishProgress方法都會中轉(handler.sendMessage(...))到onProgressUpdate * 在主UI線程中,可以對控件進行修改 * * @param values The values indicating progress. * @see #publishProgress * @see #doInBackground */ @Override protected void onProgressUpdate(Integer... values) { // 主線程Handler實例消息 Message message = DownloadImageToGalleryActivity.this.loadingHandler.obtainMessage(); message.obj = values[0]; message.what = HANDLER_LOADING; // 給主線程Handler發送消息 DownloadImageToGalleryActivity.this.loadingHandler.handleMessage(message); } /** * 運行在主UI線程中,此時是預履行狀態,下1步是doInBackground * * @see #onPostExecute * @see #doInBackground */ @Override protected void onPreExecute() { super.onPreExecute(); } /** *
Applications should preferably override {@link #onCancelled(Object)}. * This method is invoked by the default implementation of * {@link #onCancelled(Object)}.
**
Runs on the UI thread after {@link #cancel(boolean)} is invoked and * {@link #doInBackground(Object[])} has finished.
* * @see #onCancelled(Object) * @see #cancel(boolean) * @see #isCancelled() */ @Override protected void onCancelled() { /** * 設置按鈕可用 */ DownloadImageToGalleryActivity.this.saveBT.setEnabled(true); super.onCancelled(); } }
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
activity_download_image_to_grallery.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" tools:context=".DownloadImageToGrallery"> <TextView android:id="@+id/save_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="6dp" android:padding="6dp" /> <Button android:id="@+id/save_bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="6dp" android:padding="6dp" android:text="保存" /> <ImageView android:id="@+id/save_iv" android:layout_width="360dp" android:layout_height="360dp" android:layout_margin="6dp" /> LinearLayout>
DownloadImageToGalleryActivity
public class DownloadImageToGalleryActivity extends AppCompatActivity implements View.OnClickListener { private static final String OBJECT_IMAGE_URL = "http://img.blog.csdn.net/20150913233900119"; private Button saveBT; private ImageView saveIV; private CustomProgressBarDialog dialog; /** * Called when a view has been clicked. * * @param v The view that was clicked. */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.save_bt: { v.setEnabled(false); /** * 設置按鈕不可用,開始履行任務 */ new DownloadImageAsyncTask(this).execute(OBJECT_IMAGE_URL); break; } } } private static final int HANDLER_LOADING = 262; /** * 刷新Dialog顯示的進度Handler */ private static class LoadingHandler extends Handler { private final WeakReferencemActivity; public LoadingHandler(DownloadImageToGalleryActivity activity) { mActivity = new WeakReference<>(activity); } /** * Subclasses must implement this to receive messages. * * @param msg */ @Override public void handleMessage(Message msg) { DownloadImageToGalleryActivity activity = this.mActivity.get(); if (activity != null) { switch (msg.what) { case HANDLER_LOADING: { int progressValue = (int) msg.obj; activity.dialog.setLoadPrompt(progressValue + "%"); activity.dialog.show(); break; } } } } } private final LoadingHandler loadingHandler = new LoadingHandler(DownloadImageToGalleryActivity.this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_download_image_to_grallery); this.saveBT = (Button) this.findViewById(R.id.save_bt); this.saveIV = (ImageView) this.findViewById(R.id.save_iv); ((TextView) this.findViewById(R.id.save_tv)).setText(OBJECT_IMAGE_URL); this.dialog = new CustomProgressBarDialog(this); this.saveBT.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_download_image_to_grallery, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * 自定義進度條Dialog */ public class CustomProgressBarDialog extends Dialog { private LayoutInflater mInflater; private Context mContext; private WindowManager.LayoutParams params; private View mView; private TextView promptTV; public CustomProgressBarDialog(Context context) { super(context); this.init(context); } public CustomProgressBarDialog(Context context, int themeResId) { super(context, themeResId); this.init(context); } protected CustomProgressBarDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); this.init(context); } private void init(Context context) { requestWindowFeature(Window.FEATURE_NO_TITLE); this.mContext = context; this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mView = this.mInflater.inflate(R.layout.dialog_progressbar, null); setContentView(this.mView); // 設置window屬性 this.params = getWindow().getAttributes(); this.params.gravity = Gravity.CENTER; // 去背景遮蓋 this.params.dimAmount = 0; this.params.alpha = 1.0f; // 不能關掉 this.setCancelable(false); this.getWindow().setAttributes(this.params); this.promptTV = (TextView) findViewById(R.id.load_info_text); } /** * 設置內容 * * @param prompt */ public void setLoadPrompt(String prompt) { this.promptTV.setText(prompt); } } /** * 下載圖片異步任務 */ public class DownloadImageAsyncTask extends AsyncTask<String, Integer, String> { private Activity activity; private String localFilePath; public DownloadImageAsyncTask(Activity activity) { super(); this.activity = activity; } /** * 對應AsyncTask第1個參數 * 異步操作,不在主UI線程中,不能對控件進行修改 * 可以調用publishProgress方法中轉到onProgressUpdate(這里完成了1個handler.sendMessage(...)的進程) * * @param params The parameters of the task. * @return A result, defined by the subclass of this task. * @see #onPreExecute() * @see #onPostExecute * @see #publishProgress */ @Override protected String doInBackground(String... params) { // TODO 注意這里 /** * 這里接入你所用的網絡框架去下載圖片,只要保證this.localFilePath的值有就能夠了 */ URL fileUrl = null; try { fileUrl = new URL(params[0]); } catch (MalformedURLException e) { e.printStackTrace(); } if (fileUrl == null) return null; try { HttpURLConnection connection = (HttpURLConnection) fileUrl.openConnection(); connection.setRequestMethod("GET"); connection.setDoInput(true); connection.connect(); //計算文件長度 int lengthOfFile = connection.getContentLength(); /** * 不存在SD卡,就放到緩存文件夾內 */ File cacheDir = this.activity.getCacheDir(); File downloadFile = new File(cacheDir, UUID.randomUUID().toString() + ".jpg"); this.localFilePath = downloadFile.getPath(); if (!downloadFile.exists()) { File parent = downloadFile.getParentFile(); if (parent != null) parent.mkdirs(); } FileOutputStream output = new FileOutputStream(downloadFile); InputStream input = connection.getInputStream(); InputStream bitmapInput = connection.getInputStream(); //下載 byte[] buffer = new byte[1024]; int len; long total = 0; // 計算進度 while ((len = input.read(buffer)) > 0) { total += len; this.publishProgress((int) ((total * 100) / lengthOfFile)); output.write(buffer, 0, len); } output.close(); input.close(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 對應AsyncTask第3個參數 (接受doInBackground的返回值) * 在doInBackground方法履行結束以后在運行,此時已回來主UI線程當中 能對UI控件進行修改 * * @param string The result of the operation computed by {@link #doInBackground}. * @see #onPreExecute * @see #doInBackground * @see #onCancelled(Object) */ @Override protected void onPostExecute(String string) { /** * 設置按鈕可用,并隱藏Dialog */ DownloadImageToGalleryActivity.this.saveBT.setEnabled(true); DownloadImageToGalleryActivity.this.dialog.hide(); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int screenWidth = metrics.widthPixels; int screenHeight = metrics.heightPixels; /** * ImageUtil.decodeScaleImage 解析圖片 */ Bitmap bitmap = ImageUtil.decodeScaleImage(this.localFilePath, screenWidth, screenHeight); DownloadImageToGalleryActivity.this.saveIV.setImageBitmap(bitmap); /** * 保存圖片到相冊 */ String imageName = System.currentTimeMillis() + ".jpg"; MediaStore.Images.Media.insertImage(DownloadImageToGalleryActivity.this.getApplicationContext().getContentResolver(), bitmap, imageName, "牙醫助理"); Toast.makeText(this.activity, "已保存:" + imageName, Toast.LENGTH_LONG).show(); } /** * 對應AsyncTask第2個參數 * 在doInBackground方法當中,每次調用publishProgress方法都會中轉(handler.sendMessage(...))到onProgressUpdate * 在主UI線程中,可以對控件進行修改 * * @param values The values indicating progress. * @see #publishProgress * @see #doInBackground */ @Override protected void onProgressUpdate(Integer... values) { // 主線程Handler實例消息 Message message = DownloadImageToGalleryActivity.this.loadingHandler.obtainMessage(); message.obj = values[0]; message.what = HANDLER_LOADING; // 給主線程Handler發送消息 DownloadImageToGalleryActivity.this.loadingHandler.handleMessage(message); } /** * 運行在主UI線程中,此時是預履行狀態,下1步是doInBackground * * @see #onPostExecute * @see #doInBackground */ @Override protected void onPreExecute() { super.onPreExecute(); } /** *
Applications should preferably override {@link #onCancelled(Object)}. * This method is invoked by the default implementation of * {@link #onCancelled(Object)}.
**
Runs on the UI thread after {@link #cancel(boolean)} is invoked and * {@link #doInBackground(Object[])} has finished.
* * @see #onCancelled(Object) * @see #cancel(boolean) * @see #isCancelled() */ @Override protected void onCancelled() { /** * 設置按鈕可用 */ DownloadImageToGalleryActivity.this.saveBT.setEnabled(true); super.onCancelled(); } }