Imageswitcher and gesturedetector

Source: Internet
Author: User

To make an Image Browsing function, you need to browse a large number of images. There is an animated effect between image switching. At the beginning, viewpager is used ,, if there are too many images, the memory will be insufficient (and a solution was found on the official website), so I tried to switch the screen and prepare the view and release the bitmap, finally, I found that the screen was a little choppy during switching, so I thought of viewswitcher. However, this control does not have its own screen effect, so I need to add it myself.

Imageswitche inherits from viewswitcherr:

Layout file:

  <ImageSwitcher       android:layout_height="fill_parent"      android:layout_width="fill_parent"      android:id="@+id/is">  </ImageSwitcher>

Java object:

ImageSwitcher is = (ImageSwitcher)findViewById(R.id.is);

Call the is. setfactory (viewswitcher. viewfactory factory) method. In viewswitcher. viewfactory, there is a method makeview, And the return value is the view object.

@Overridepublic View makeView() {ImageView image =new ImageView(this);image.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));image.setScaleType(ImageView.ScaleType.FIT_XY);return image;}

To switch an image, call the setimageuri (URI), setimageresource (INT resid), or setimagedrawable (drawable) method.

You can add an animation effect when switching an image by using the setinanimation method and setoutanimation method.

Viewswitcher and imageswitcher are basically the same, but one is to show only imageview, and the other is not just to show imageview.

The official document said: this control has two views and can only have two views. In addition to being created through viewfactory, you can also use addview (view child, int index, viewgroup. layoutparams Params) manually add the method.

Gesturedetector:

This class is mainly used to process the motionevent object, so that after the touch screen, you can simply determine what operations the segment has done.

This class has five constructor methods. I used gesturedetector (context, gesturedetector. ongesturelistener listener.

The second parameter is the monitoring interface for the touch screen. It is generally used to implement the class simpleongesturelistener object. The user inherits simpleongesturelistener and overwrites anything he wants to implement.

Here I have rewritten the onfling method, which mainly judges the screen.


Some simple code may be out of weeks, not tested, and not animated. The default picture contains data/<package name>/files/directory:

import java.io.File;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.net.Uri;import android.os.Bundle;import android.view.GestureDetector;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.MotionEvent;import android.view.View;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.Gallery.LayoutParams;import android.widget.ViewSwitcher.ViewFactory;public class SeePhoto extends Activity implements ViewFactory{private ImageSwitcher is;private int position;private List<File>files;private GestureDetector gd;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.seephoto);files = getFiles();is = (ImageSwitcher)findViewById(R.id.is);is.setFactory(this);gd=new GestureDetector(this,new MyGuestListner());if(files.size()!=0)is.setImageURI(Uri.fromFile(files.get(position)));position=position+1;}private List<File> getFiles() {// TODO Auto-generated method stubList<File>list = new ArrayList<File>();String names[] = fileList();for(int i=0;i<names.length;i++){list.add(getFileStreamPath(names[i]));}return list;}@Overridepublic View makeView() {ImageView image =new ImageView(this);image.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));image.setScaleType(ImageView.ScaleType.FIT_XY);return image;}@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubreturn gd.onTouchEvent(event);}class MyGuestListner extends SimpleOnGestureListener{@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {if(files.size()!=0)if(velocityX>0&&position>0){is.setImageURI(Uri.fromFile(files.get(position)));position =position-1;}else if(velocityX<0&&position!=files.size()-1){is.setImageURI(Uri.fromFile(files.get(position)));position=position+1;}return super.onFling(e1, e2, velocityX, velocityY);}}}

Related Article

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.