Android uses ImageSwitcher + Gallery to implement image Switching

Source: Internet
Author: User

Android uses ImageSwitcher + Gallery to implement image Switching

In Android application development, it is essential to deal with image data, such as portraits or photo albums. In some cases, when there are many images, we need to view the images through switching. There are many implementation methods, and we need to switch through ViewPager. This is also widely used at present, you can also use viewflipper or customize the tool class to implement ....., today, we use two native controls of Android to implement image switching. I wanted to map it, but I found it really inconvenient to publish a CSDN blog, so I was too lazy ~~

------------------------------------ UI layout file: activity_image_viewer.xml -------------------------------

Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">

Android: id = "@ + id/imageSwitcher"
Android: layout_width = "match_parent"
Android: layout_height = "0dp"
Android: layout_weight = "4">


Android: id = "@ + id/gallery"
Android: layout_width = "match_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1"
Android: spacing = "10sp"/>

 

--------------------------------------- Image item layout image_viewer_item.xml -------------------------------------

Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical">

Android: id = "@ + id/image"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>

 

----------------------------------------------------------------------- ImageViewerActivity code -----------------------------------------------------------

Public class ImageViewerActivity extends Activity implements OnItemSelectedListener, ViewFactory {
/** Gallery control */
Private Gallery mGallery;
/** ImageSwitcher control */
Private ImageSwitcher mImageSwitcher;
/** Index of the selected image */
Private int selectedIndex = 0;
/** Coordinate when the finger is released */
Private int upX;
/** Coordinates when the finger is pressed */
Private int downX;


@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_image_viewer );
FindViews ();
Init ();
}


/** Initialize the UI */
Private void findViews (){
MGallery = (Gallery) findViewById (R. id. gallery );
MImageSwitcher = (ImageSwitcher) findViewById (R. id. imageSwitcher );
MGallery. setOnItemSelectedListener (this );
MImageSwitcher. setOnTouchListener (onTouchListener );
}


/** Set parameters */
Private void init (){
/** Image adapter */
ImageViewerAdapter imageAdapter = new ImageViewerAdapter (this );
MGallery. setAdapter (imageAdapter );
/** Switch between fingers when sliding */
MImageSwitcher. setFactory (this );
}


@ Override
Public void onItemSelected (AdapterView Parent, View view, int position, long id ){
MImageSwitcher. setImageResource (ImageViewerAdapter. images [position]);
SelectedIndex = position;
}


/** ImageSwitcher mobile phone slide monitoring event */
Private OnTouchListener onTouchListener = new OnTouchListener (){


@ Override
Public boolean onTouch (View v, MotionEvent event ){
If (event. getAction () = MotionEvent. ACTION_DOWN ){
DownX = (int) event. getX ();
Return true;
} Else if (event. getAction () = MotionEvent. ACTION_UP ){
UpX = (int) event. getX ();
}
If (upX-downX> 100 ){
/** Drag the image from the left to the right, that is, you can see the previous image. If it is the first image, it will go to the end */
If (mGallery. getSelectedItemPosition () = 0)
SelectedIndex = mGallery. getCount ()-1;
Else selectedIndex = mGallery. getSelectedItemPosition ()-1;
}
If (downX-upX & gt; 100 ){
/** Drag from right to left, that is, the last one. If it is the last one, go to the first */
If (mGallery. getSelectedItemPosition () = (mGallery. getCount ()-1 ))
SelectedIndex = 0;
Else selectedIndex = mGallery. getSelectedItemPosition () + 1;
}
MGallery. setSelection (selectedIndex, true );
Return true;
}


};


@ Override
Public void onNothingSelected (AdapterView Parent ){}


@ Override
Public View makeView (){
ImageView imageView = new ImageView (this );
/** Set the scaling ratio */
ImageView. setScaleType (ImageView. ScaleType. FIT_CENTER );
ImageView. setLayoutParams (new ImageSwitcher. LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT ));
Return imageView;
}

}

----------------------------------------------------------------------- Data adapter ImageViewerAdapter code -------------------------------------------------------

Public class ImageViewerAdapter extends BaseAdapter {
/** Context */
Private Context mContext;
/** Layout manager */
Private LayoutInflater layoutInfalter;
/** Image (6 custom images )*/
Public static Integer [] images = {R. drawable. image01, R. drawable. image02, R. drawable. image03, R. drawable. image04, R. drawable. image5, R. drawable. image06 };


Public ImageViewerAdapter (Context context ){
Super ();
This. mContext = context;
LayoutInfalter = LayoutInflater. from (mContext );
}

@ Override
Public int getCount (){
Return images. length;
}


@ Override
Public Object getItem (int position ){
Return position;
}


@ Override
Public long getItemId (int position ){
Return position;
}


@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
ViewHolder viewHolder = null;
If (null = convertView ){
ViewHolder = new ViewHolder ();
ConvertView = (ImageView) layoutInfalter. inflate (R. layout. image_viewer_item, null );
ViewHolder. imageView = (ImageView) convertView. findViewById (R. id. image );
ConvertView. setTag (viewHolder );
} Else {
ViewHolder = (ViewHolder) convertView. getTag ();
}
ViewHolder. imageView. setImageResource (images [position]);
ViewHolder. imageView. setLayoutParams (new Gallery. LayoutParams (100, LayoutParams. FILL_PARENT ));
Return convertView;
}


Private static class ViewHolder {
Private ImageView imageView;
}
}

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.