Sample code for browsing image effects in the WeChat-like sending circle of friends developed by Android

Source: Internet
Author: User
This article mainly introduces information about the effect of pictures in the Android text box. For more information, see this article, for more information, see

Let's take a look at the results first:

Public class MyActivity extends Activity {/* Image Display List */private ListView listView;/* Image URL array */private List
 
  
ContentBeans;/* talk about the adapter */private MyAdapter adapter;/*** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); initData (); initViews ();}/*** initialize data */private void initData () {contentBeans = new ArrayList
  
   
(); ArrayList
   
    
ImgUrls1 = new ArrayList
    
     
(); ImgUrls1.add ("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg? Attname = & e = 1448288962 & token = KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ _: AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ContentBean cb1 = new ContentBean (1," java "," Sun Microsystems ", imgUrls1); contentBeans. add (cb1); ArrayList
     
      
ImgUrls2 = new ArrayList
      
        (); ImgUrls2.add ("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg? Attname = & e = 1448288962 & token = KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ _: AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); imgUrls2.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg? Attname = & e = 1448288962 & token = KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ _: AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); imgUrls2.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg? Attname = & e = 1448288962 & token = KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ _: AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ContentBean cb2 = new ContentBean (2," OC "," Stepstone ", imgUrls2); contentBeans. add (cb2); ArrayList
       
         ImgUrls3 = new ArrayList
        
          (); ImgUrls3.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ImgUrls3.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ImgUrls3.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ImgUrls3.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ImgUrls3.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ImgUrls3.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ImgUrls3.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ImgUrls3.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ImgUrls3.add (" http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8 "); ContentBean cb3 = new ContentBean (3," python "," Guido van Rosum ", imgUrls3); contentBeans. add (cb3);} private void initViews () {listView = (ListView) findViewById (R. id. lv_my); adapter = new MyAdapter (MyActivity. this, contentBeans); listView. setAdapter (adapter );}}
        
       
      
     
    
   
  
 

The image above is the network image I uploaded to Qiniu, and the image is loaded with ImageLoader. The following figure also shows the specific ImageLoader configuration.
2. check the adapter content.
Set the image adapter in the list adapter. the GridView of the image overwrites a GridView that cannot be moved, and overwrites OnMeasure ();


Public class MyAdapter extends BaseAdapter {private Context context; private List
 
  
Data; public MyAdapter (Context context, List
  
   
Data) {this. context = context; this. data = data ;}@ Override public int getCount () {return data. size () ;}@ Override public Object getItem (int I) {return data. get (I) ;}@ Override public long getItemId (int I) {return I ;}@ Override public View getView (int I, View view, ViewGroup viewGroup) {ViewHolder holder; if (view = null) {holder = new ViewHolder (); view = View. inflate (context, R. layout. it Em, null); holder. gridView = (NoScrollGridView) view. findViewById (R. id. gridview); holder. tvName = (TextView) view. findViewById (R. id. TV _name); holder. tvTitle = (TextView) view. findViewById (R. id. TV _title); view. setTag (holder);} else {holder = (ViewHolder) view. getTag ();} final ContentBean bean = data. get (I); holder. tvName. setText (bean. getName (); holder. tvTitle. setText (bean. getTitle (); if (data! = Null & data. size ()> 0) {holder. gridView. setAdapter (new ImageGridAdapter (context, bean. getImgUrls ();}/*** click event in the image list */holder. gridView. setOnItemClickListener (new AdapterView. onItemClickListener () {@ Override public void onItemClick (AdapterView
   AdapterView, View view, int I, long l) {Intent intent = new Intent (context, ImagePagerActivity. class); intent. putExtra (ImagePagerActivity. EXTRA_IMAGE_URLS, (Serializable) bean. getImgUrls (); intent. putExtra (ImagePagerActivity. EXTRA_IMAGE_INDEX, I); context. startActivity (intent) ;}}); return view;} class ViewHolder {TextView tvName, tvTitle; NoScrollGridView gridView ;}}
  
 

3. then there is image browsing. There are also a lot of demos on the Internet, as well as detailed explanations. you can just drag them and use them. the number of pictures below is to listen to setOnPageChangeListener () to change the index value of the image below.


/*** Image Viewer */public class ImagePagerActivity extends FragmentActivity {private static final String STATE_POSITION = "STATE_POSITION"; public static final String EXTRA_IMAGE_INDEX = "image_index "; public static final String EXTRA_IMAGE_URLS = "image_urls"; private HackyViewPager mPager; private int pagerPosition; private TextView indicator; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. image_detail_pager); pagerPosition = getIntent (). getIntExtra (EXTRA_IMAGE_INDEX, 0); ArrayList
 
  
Urls = getIntent (). getStringArrayListExtra (EXTRA_IMAGE_URLS); mPager = (HackyViewPager) findViewById (R. id. pager); ImagePagerAdapter mAdapter = new ImagePagerAdapter (getSupportFragmentManager (), urls); mPager. setAdapter (mAdapter); indicator = (TextView) findViewById (R. id. indicator); CharSequence text = getString (R. string. viewpager_indicator, 1, mPager. getAdapter (). getCount (); indicator. setText (text ); // Update the subscript mPager. setOnPageChangeListener (new OnPageChangeListener () {@ Override public void onPageScrollStateChanged (int arg0) {}@ Override public void onPageScrolled (int arg0, float arg1, int arg2) {}@ Override public void onPageSelected (int arg0) {CharSequence text = getString (R. string. viewpager_indicator, arg0 + 1, mPager. getAdapter (). getCount (); indicator. setText (text) ;}}); if (savedIns TanceState! = Null) {pagerPosition = savedInstanceState. getInt (STATE_POSITION);} mPager. setCurrentItem (pagerPosition) ;}@ Override public void onSaveInstanceState (Bundle outState) {outState. putInt (STATE_POSITION, mPager. getCurrentItem ();} private class ImagePagerAdapter extends FragmentStatePagerAdapter {public ArrayList
  
   
FileList; public ImagePagerAdapter (FragmentManager fm, ArrayList
   
    
FileList) {super (fm); this. fileList = fileList;} @ Override public int getCount () {return fileList = null? 0: fileList. size () ;}@ Override public Fragment getItem (int position) {String url = fileList. get (position); return ImageDetailFragment. newInstance (url );}}}
   
  
 

Detailed Image Fragment interface, which contains a long click event and the image loading status


Package com. hankkin. weiXinLookImgsDemo. activty; import android. graphics. bitmap; import android. OS. bundle; import android. support. v4.app. fragment; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. imageView; import android. widget. progressBar; import android. widget. toast; import com. hankkin. weiXinLookImgsDemo. r; import com. nostra13.universalimagel Oader. core. imageLoader; import com. nostra13.universalimageloader. core. assist. failReason; import com. nostra13.universalimageloader. core. listener. simpleImageLoadingListener; import com. others. photoViewAttacher;/*** Fragment */public class ImageDetailFragment extends Fragment {private String mImageUrl; private ImageView mImageView; private ProgressBar progressBar; private PhotoViewAttacher mAtta Cher; public static ImageDetailFragment newInstance (String imageUrl) {final ImageDetailFragment f = new ImageDetailFragment (); final Bundle args = new Bundle (); args. putString ("url", imageUrl); f. setArguments (args); return f ;}@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); mImageUrl = getArguments ()! = Null? GetArguments (). getString ("url"): null;} @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {final View v = inflater. inflate (R. layout. image_detail_fragment, container, false); mImageView = (ImageView) v. findViewById (R. id. image); mAttacher = new PhotoViewAttacher (mImageView); mAttacher. setOnPhotoTapListener (new PhotoViewAttacher. onPhotoTapListener () {@ Override public void onPhotoTap (View arg0, float arg1, float arg2) {getActivity (). finish () ;}}); mAttacher. setOnLongClickListener (new View. onLongClickListener () {@ Override public boolean onLongClick (View view) {Toast. makeText (getActivity (). getApplicationContext (), "save", Toast. LENGTH_SHORT ). show (); return false ;}}); progressBar = (ProgressBar) v. findViewById (R. id. loading); return v ;}@ Override public void onActivityCreated (Bundle savedInstanceState) {super. onActivityCreated (savedInstanceState); ImageLoader. getInstance (). displayImage (mImageUrl, mImageView, new SimpleImageLoadingListener () {@ Override public void onLoadingStarted (String imageUri, View view) {progressBar. setVisibility (View. VISIBLE) ;}@ Override public void onLoadingFailed (String imageUri, View view, FailReason failReason) {String message = null; switch (failReason. getType () {case IO_ERROR: message = "Download error"; break; case DECODING_ERROR: message = "image cannot be displayed"; break; case NETWORK_DENIED: message = "network problems, cannot download "; break; case OUT_OF_MEMORY: message =" The image is too large to be displayed "; break; case UNKNOWN: message =" UNKNOWN error "; break;} Toast. makeText (getActivity (), message, Toast. LENGTH_SHORT ). show (); progressBar. setVisibility (View. GONE) ;}@ Override public void onLoadingComplete (String imageUri, View, Bitmap loadedImage) {progressBar. setVisibility (View. GONE); mAttacher. update ();}});}}

I forgot to initialize ImageLoader. I wrote it to Application.


Private MyApplication context; @ Override public void onCreate () {super. onCreate (); context = this; initImageLoader (context);}/*** initialize Imageloader * by Hankkin: 2015-11-22 23:20:29 * @ param context */public static void initImageLoader (Context context) {DisplayImageOptions options = new DisplayImageOptions. builder (). showImageOnLoading (R. drawable. ic_launcher ). showImageOnFail (R. drawable. ic_launcher ). resetViewBeforeLoading (false) // default. delayBeforeLoading (0 ). cacheInMemory (true) // default. cacheOnDisk (true) // default. considerExifParams (true) // default. imageScaleType (ImageScaleType. IN_SAMPLE_POWER_OF_2) // default. bitmapConfig (Bitmap. config. ARGB_8888) // default. displayer (new SimpleBitmapDisplayer () // default. handler (new Handler () // default. build (); File picPath = new File (Environment. getExternalStorageDirectory (). getPath () + File. separator + "weixinlookimgdemo" + File. separator + "files"); ImageLoaderConfiguration config = new ImageLoaderConfiguration. builder (context ). memoryCacheExtraOptions (480,800) // default = device screen dimensions. diskCacheExtraOptions (480,800, null ). threadPoolSize (3) // default. threadPriority (Thread. NORM_PRIORITY-1) // default. tasksProcessingOrder (QueueProcessingType. FIFO) // default. denyCacheImageMultipleSizesInMemory (). memoryCache (new LruMemoryCache (2*1024*1024 )). memoryCacheSize (2*1024*1024 ). memoryCacheSizePercentage (13) // default. diskCache (new UnlimitedDiscCache (picPath) // default. diskCacheSize (50*1024*1024 ). diskCacheFileCount (1000 ). diskCacheFileNameGenerator (new HashCodeFileNameGenerator () // default. imageDownloader (new BaseImageDownloader (context) // default. imageDecoder (new BaseImageDecoder (true) // default. defaultDisplayImageOptions (options) // default. writeDebugLogs (). build (); // Initialize ImageLoader with configuration. imageLoader. getInstance (). init (config );}

The above is all about the effect of browsing images in the Android text box, hoping to help you learn Android software programming.

The above is the detailed content of the sample code for the Android development of the imitation sending circle of friends to browse image effects. For more information, see other related articles on php Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.