High imitation local image selector and high imitation Selector
In projects, you often need to select images in the mobile phone system. However, to directly start the system page, you can only select a single image. In addition, the UI is not controlled by ourselves and is not flexible. Therefore, the general effect of the image is modeled here, create an image Selector
When loading images, use the open-source project Universal-Image-Loader. The sample code is as follows:
MyApplication class:
Package com. home. imgscan; import android. app. application; import android. content. context; import com. nostra13.universalimageloader. core. imageLoader; import com. nostra13.universalimageloader. core. imageLoaderConfiguration; public class MyApplication extends Application {@ Overridepublic void onCreate () {super. onCreate (); initImageLoader (this, false);}/*** initialize ImageLoader (called in onCreate () of Application) ** @ param context * Context object * @ param isDebug * Whether to enable Debug mode */public static void initImageLoader (context, boolean isDebug) {// create the default ImageLoader Configuration Parameter ImageLoaderConfiguration. builder builder = new ImageLoaderConfiguration. builder (context); if (isDebug) {builder. writeDebugLogs ();} ImageLoaderConfiguration configuration = builder. build (); ImageLoader. getInstance (). init (configuration );}}Home page: MainActivity:
Package com. home. imgscan; import java. util. arrayList; import java. util. list; import android. annotation. suppressLint; import android. app. activity; import android. app. progressDialog; import android. content. contentResolver; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. OS. handler; import android. OS. message; import android. provider. me DiaStore; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. widget. gridView; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {// GridView control private GridView; // progress dialog box private ProgressDialog mProgressDialog; // private List of local image paths <String> list = new ArrayLis T <String> (); // The private final static int SCAN_ OK = 1; // adapter private ScanAdapter adapter; // the send button private TextView sendView; @ SuppressLint ("HandlerLeak") private Handler mHandler = new Handler () {@ Overridepublic void handleMessage (Message msg) {super. handleMessage (msg); switch (msg. what) {case SCAN_ OK: mProgressDialog. dismiss (); setAdapter (); break ;}};@ Overrideprotected void onCreate (Bundle savedInsta NceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. main); initView (); getImages ();}/*** initialize the page control */private void initView () {gridView = (GridView) findViewById (R. id. main_grid); // send button sendView = (TextView) findViewById (R. id. main_btn_send); sendView. setOnClickListener (this);}/*** set adapter */private void setAdapter () {if (adapter = null) {ad Apter = new ScanAdapter (MainActivity. this, list); gridView. setAdapter (adapter);} else {adapter. setList (list); adapter. notifyDataSetChanged () ;}}/*** get the image on the mobile phone */private void getImages () {if (! Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {Toast. makeText (this, "SD card does not exist", Toast. LENGTH_SHORT ). show (); return;} mProgressDialog = ProgressDialog. show (this, null, "loading... please wait... "); new Thread (new Runnable () {@ Overridepublic void run () {Uri mImageUri = MediaStore. images. media. EXTERNAL_CONTENT_URI; ContentResolver mContentResolver = MainActivity. this. getContentResolver (); Cursor MCursor = mContentResolver. query (mImageUri, null, MediaStore. Images. Media. MIME_TYPE + "=? Or "+ MediaStore. Images. Media. MIME_TYPE +" =? ", New String [] {" image/jpeg "," image/png "}, MediaStore. images. media. DATE_MODIFIED); while (mCursor. moveToNext () {String path = mCursor. getString (mCursor. getColumnIndex (MediaStore. images. media. DATA); list. add (path);} mCursor. close (); mHandler. sendEmptyMessage (SCAN_ OK );}}). start ();}/*** update send button */public void updateSendBtn () {List <String> hasCheckedList = adapter. getHasCheckList (); if (hasCheckedList = Null | hasCheckedList. size () = 0) {sendView. setText ("send");} else {int size = hasCheckedList. size (); sendView. setText ("Send (" + size + "/9)") ;}@ Overridepublic void onClick (View v) {if (v. getId () = R. id. main_btn_send) {List <String> hasCheckedList = adapter. getHasCheckList (); if (hasCheckedList = null | hasCheckedList. size () = 0) {Toast. makeText (this, "select image", Toast. LENGTH_SHORT ). show (); return ;}/ /Send ...}}}Custom adapter ScanAdapter:
Package com. home. imgscan; import java. util. arrayList; import java. util. list; import android. graphics. bitmap; import android. text. textUtils; import android. util. sparseArray; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. toast; import co M. nostra13.universalimageloader. core. displayImageOptions; import com. nostra13.universalimageloader. core. imageLoader; import com. nostra13.universalimageloader. core. download. imageDownloader. scheme; public class ScanAdapter extends BaseAdapter {private List <String> list; private LayoutInflater inflater; // private List of selected image local paths <String> hasCheckList = new ArrayList <String> (); private MainActivity activi Ty; public ScanAdapter (MainActivity activity, List <String> list) {this. list = list; this. activity = activity; inflater = LayoutInflater. from (activity) ;}@ Overridepublic int getCount () {return list = null? 0: list. size () ;}@ Overridepublic Object getItem (int position) {return list. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup arg2) {if (convertView = null) {convertView = inflater. inflate (R. layout. grid_content, null);} String path = list. get (position); // select the box ImageView box = getAdapterView (convertView, R. id. grid_content_box, path); box. setOnClickListener (new MyOnClickListener (); // image control ImageView iv = getAdapterView (convertView, R. id. grid_content_iv, path); setLocalImg (iv, path); return convertView;} private class MyOnClickListener implements OnClickListener {@ Overridepublic void onClick (View v) {if (v. getId ()! = R. id. grid_content_box) {return;} String path = (String) v. getTag (); if (TextUtils. isEmpty (path) {return;} if (hasCheckList. contains (path) {hasCheckList. remove (path); v. setBackgroundResource (R. drawable. box_unchecked);} else {if (hasCheckList. size ()> = 9) {Toast. makeText (activity, "up to 9 images can be selected", Toast. LENGTH_SHORT ). show (); return;} hasCheckList. add (path); v. setBackgroundResource (R. drawable. box_checked );} Activity. updateSendBtn () ;}@suppresswarnings ("unchecked") public <T extends View> T getAdapterView (View convertView, int id, Object tag) {SparseArray <View> viewHolder = null; try {if (convertView. getTag (R. id. view_holder) instanceof SparseArray <?>) {ViewHolder = (SparseArray <View>) convertView. getTag (R. id. view_holder) ;}} catch (ClassCastException e) {}if (viewHolder = null) {viewHolder = new SparseArray <View> (); convertView. setTag (R. id. view_holder, viewHolder); convertView. setTag (R. id. path, tag);} View childView = viewHolder. get (id); if (childView = null) {childView = convertView. findViewById (id); childView. setTag (tag); viewHolder. put (id, childView);} return (T) childView ;} /*** load local image ** @ param imageView * image ImageView control * @ param imagePath * local image path */public static void setLocalImg (ImageView imageView, String imagePath) {// display image configuration DisplayImageOptions. builder builder = new DisplayImageOptions. builder (); builder. cacheInMemory (true ). cacheOnDisk (true ). bitmapConfig (Bitmap. config. RGB_565); DisplayImageOptions = builder. build (); ImageLoader. getInstance (). displayImage (Scheme. FILE. wrap (imagePath), imageView, options);} public List <String> getList () {return list;} public void setList (List <String> list) {this. list = list;} public List <String> getHasCheckList () {return hasCheckList;} public void setHasCheckList (List <String> hasCheckList) {this. hasCheckList = hasCheckList ;}}
Layout file main. xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "@ android: color/white" android: orientation = "vertical"> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: background = "@ android: color/background_dark"> <ImageView android: id = "@ + id/main_iv_back" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerVertical = "true" android: layout_margin = "10dp" android: background = "@ drawable/back"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: text = "image" android: textColor = "@ android: color/white" android: textSize = "20sp"/> <TextView android: id = "@ + id/main_btn_send" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Rule = "true" android: layout_centerVertical = "true" android: layout_marginTop = "10dp" android: background = "@ android: color/holo_green_light" android: padding = "10dp" android: text = "send" android: textColor = "@ android: color/white "android: textSize =" 18sp "/> </RelativeLayout> <GridView android: id =" @ + id/main_grid "android: layout_width =" match_parent "android: layout_height = "match_parent" android: background = "@ android: color/white" android: cacheColorHint = "@ android: color/transparent" android: gravity = "center" android: horizontalSpacing = "1dp" android: listSelector = "@ android: color/transparent" android: numColumns = "3" android: scrollbars = "none" android: stretchMode = "columnWidth" android: verticalSpacing = "1dp"> </GridView> </LinearLayout>
Layout file grid_content.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/black" > <ImageView android:id="@+id/grid_content_iv" android:layout_width="match_parent" android:layout_height="100dp" android:scaleType="centerCrop" /> <ImageView android:id="@+id/grid_content_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="6dp" android:layout_marginTop="6dp" android:background="@drawable/box_unchecked" /></RelativeLayout>
Sample download link: http://download.csdn.net/detail/u010142437/8854871
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.