編輯:關於Android編程
Android 仿微信 圖片選擇器采用項目的框架上采用mvp模式,並用了我自己寫的一套lib,地址ndileber
首先先定義了數據源,數據源可能來自於網絡也可能來自於本地,於是我定義了兩個數據源,分別處理網絡和本地的數據:
我先創建了數據源接口:
ImageDataSource
public interface ImageDataSource extends BaseDataSource{ void getAllImage(BaseCallback> callback); }
建立model,model建立的原因是我要反向將數據注入到這裡
public class ImageModel { Integer _id; String _display_name; String _data; private boolean check = false; public boolean isCheck() { return check; } public void setCheck(boolean check) { this.check = check; } public Integer get_id() { return _id; } public void set_id(Integer _id) { this._id = _id; } public String get_display_name() { return _display_name; } public void set_display_name(String _display_name) { this._display_name = _display_name; } public String get_data() { return _data; } public void set_data(String _data) { this._data = _data; } }
網絡數據源:當然目前僅僅是測試本地數據源,於是網絡數據源只是簡單的實現了ImageDataSource 方法
ImageRemoteDataSource
public class ImageRemoteDataSource extends BaseRemoteDataSource implements ImageDataSource { private static volatile ImageRemoteDataSource instance; private ImageRemoteDataSource(){ } public static ImageRemoteDataSource getInstance(){ if (instance==null){ synchronized (ImageRemoteDataSource.class){ if(instance==null){ instance = new ImageRemoteDataSource(); } } } return instance; } @Override public void getAllImage(BaseCallback> callback) { } }
本地數據源:數據源來自於Provider,lib中將Provider封裝了,直接可以調取數據並且按照時間來排序
ImageLocalDataSource
public class ImageLocalDataSource extends BaseLocalDataSource implements ImageDataSource { private static volatile ImageLocalDataSource instance; private ImageLocalDataSource(){ } public static ImageLocalDataSource getInstance(){ if (instance==null){ synchronized (ImageLocalDataSource.class){ if(instance==null){ instance = new ImageLocalDataSource(); } } } return instance; } @Override public void getAllImage(final BaseCallback> callback) { ThreadExecutor.getInstance().submit(new Runnable() { @Override public void run() { Logger.d("進入getAllImage"); String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.DATA }; String orderBy = android.provider.ContactsContract.Contacts._ID + " DESC"; Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; List
imageModels = null; try { imageModels = hContentProvider.query(projection,orderBy,uri,ImageModel.class); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } callback.onSuccess(imageModels); } }); } }
之後建立數據倉庫類
public class ImagesRepository extends BaseRepositoryimplements ImageDataSource{ List url; private static volatile ImagesRepository instance; public static ImagesRepository getInstance(){ if (instance==null){ synchronized (ImagesRepository.class){ if(instance==null){ instance = new ImagesRepository(ImageLocalDataSource.getInstance(),ImageRemoteDataSource.getInstance()); } } } return instance; } protected ImagesRepository(ImageLocalDataSource localDataSource, ImageRemoteDataSource remoteDataSource) { super(localDataSource, remoteDataSource); } @Override public void getAllImage(BaseCallback > callback) { localDataSource.getAllImage(callback); } }
寫法是采用在activity中包fragment的方法
之後我們建立了activity的layout
<framelayout android:id="@+id/images_framelayout" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_height="match_parent" android:layout_width="match_parent"></framelayout>
再建立fragment的layout,一如既往的采用RecyclerView
<framelayout android:layout_height="match_parent" android:layout_width="match_parent" tools:context="com.jcb.www.jcbmanager.fragment.ImagesFragment" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> </framelayout>
之後建立我們需要的cell圖像采用 SimpleDraweeView
資料圖
activity代碼如下 (最近lib進行了更改,這塊代碼還沒改掉)<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;"> public class ImagesActivity extends BaseActivity implements ImagesFragment.OnFragmentInteractionListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_images); ActivityUtils.getFragment(getSupportFragmentManager(),R.id.images_framelayout, ImagesFragment.newInstance()); } }
創建adapter
public class ImagesRecyclerAdapter extends RecyclerView.Adapter{ private List urls; private ImagesFragment.OnFragmentInteractionListener interactionListener; public ImagesRecyclerAdapter(List urls,ImagesFragment.OnFragmentInteractionListener interactionListener){ this.urls = urls; this.interactionListener = interactionListener; } @Override public ImageViewHoder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.images_image,parent,false); return new ImageViewHoder(view); } @Override public void onBindViewHolder(final ImageViewHoder holder, int position) { holder.url = urls.get(position); UImage.showThumb(Uri.fromFile(new File(holder.url.get_data())), holder.imageView,200,200); setImage(holder.check,R.drawable.jy_checkbox_off); if(holder.url.isCheck()){ setImage(holder.check,R.drawable.jy_checkbox_on); }else{ setImage(holder.check,R.drawable.jy_checkbox_off); } holder.item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent it = new Intent(); it.setClass(ActivityManager.peekTopActivity(), BigImageActivity.class); it.putExtra(BigImageActivity.UTI,holder.url.get_data()); ActivityManager.peekTopActivity().startActivity(it); } }); holder.check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ImageView check = (ImageView)v; if(holder.url.isCheck()){ setImage(check,R.drawable.jy_checkbox_off); holder.url.setCheck(false); }else{ setImage(check,R.drawable.jy_checkbox_on); holder.url.setCheck(true); } } }); } public void setImage(ImageView check,int res){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { check.setBackground(ActivityManager.peekTopActivity().getResources().getDrawable(res,ActivityManager.peekTopActivity().getTheme())); }else{ check.setBackground(ActivityManager.peekTopActivity().getResources().getDrawable(res)); } } @Override public int getItemCount() { if(urls==null){ return 0; } return urls.size(); } class ImageViewHoder extends RecyclerView.ViewHolder{ private final SimpleDraweeView imageView; private final ImageView check; private final View item; private ImageModel url; public ImageViewHoder(View itemView) { super(itemView); this.item = itemView; imageView = (SimpleDraweeView) itemView.findViewById(R.id.iamges_im); check = (ImageView) itemView.findViewById(R.id.image_check); } } }
fragment代碼如下,主要是調用數據源數據進行顯示的
public class ImagesFragment extends BaseFragment { private OnFragmentInteractionListener mListener; public ImagesFragment() { } public static ImagesFragment newInstance() { ImagesFragment fragment = new ImagesFragment(); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } RecyclerView recyclerView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_images, container, false); Context context = view.getContext(); recyclerView = getView(view,R.id.images_Recycler); recyclerView.setLayoutManager(new GridLayoutManager(context,3)); Logger.d("進入imagesfragemt"); ImagesRepository.getInstance().getAllImage(new BaseDataSource.BaseCallback>() { @Override public void onSuccess(final List
imageModels) { //Logger.d(imageModels.size()+ HJson.toJson(imageModels)); getActivity().runOnUiThread(new Runnable() { @Override public void run() { recyclerView.setAdapter(new ImagesRecyclerAdapter(imageModels,mListener)); } }); } @Override public void onFailure(DataSourceException e) { } }); return view; } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } }
其實到這裡代碼還是沒寫完呢,只是展示了圖片,代碼後續部分,大家就自行完成好了,如果我有心情的話我會把我仿照微信的整套代碼全部貼出到github上,如有什麼建議或者意見請在下面評論,我會偶爾看的
前些天搞了個系統的下拉刷新跟上拉加載,由於效果一般所以才會有了今天這篇博文對於大多數的碼農來說,能弄出些自己感興趣的好東西還是比較開心的。--package com.ex
一.Universal-Image-Loader概述:Android-Universal-Image-Loader是一個開源的圖片加載框架,這個項目的目的是提供一個可重復
一、操作系統的體系結構計算機是由一堆硬件組成的,操作系統是為了有效的控制這些硬件資源的軟件。操作系統除了有效地控制這些硬件資源的分配,並提供計算機運行所需要的功能之外,為
在android 中,邊緣模糊的效果是通過BlurMaskFilter實現的 , 它定義了一個邊緣模糊半徑和模糊效果 (Blur)。Blur 有四種模糊效果, inner