Gallery for manual touch and automatic Infinite Loop

Source: Internet
Author: User

The UI effect should be followed by code:

package xiaogang.zhao;import xiaogang.zhao.R;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.AdapterView.OnItemSelectedListener;import java.util.Timer;import java.util.TimerTask;public class GalleryTest extends Activity {    private SizingGallery mGalleryItem;    private final Handler mHandler = new Handler() {        public void handleMessage(Message message) {            super.handleMessage(message);            switch (message.what) {                case 1:                    mGalleryItem.setSelection(mGalleryItem.getSelectedItemPosition() + 1);                    break;            }        }    };    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        setupViews();        init();    }    private void setupViews() {        mGalleryItem = (SizingGallery) findViewById(R.id.gallery0);        final ImageAdapter adapter = new ImageAdapter(this);        mGalleryItem.setAdapter(adapter);        mGalleryItem.setSelection(Integer.MAX_VALUE / 2);        mGalleryItem.setOnItemSelectedListener(new OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {                android.util.Log.d("AAA", "position: " + position + "    id:" + id);            }            @Override            public void onNothingSelected(AdapterView<?> parent) {                // TODO Auto-generated method stub            }        });    }    private void init() {        final Timer timer = new Timer();        timer.schedule(new TimerTask() {            @Override            public void run() {                mHandler.sendEmptyMessage(1);            }        }, 5000, 5000);    }    private final static class ImageAdapter extends BaseAdapter {        public ImageAdapter(Context c) {            mContext = c;        }        public int getCount() {            return Integer.MAX_VALUE;        }        public Object getItem(int position) {            return position;        }        public long getItemId(int position) {            return position;        }        public View getView(int position, View convertView, ViewGroup parent) {            if (position < 0)                position = position + mThumbIds.length;            ImageView imageView;            if (convertView == null) {                imageView = new ImageView(mContext);                //imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));                imageView.setAdjustViewBounds(false);                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);                imageView.setPadding(18, 18, 18, 18);            } else {                imageView = (ImageView) convertView;            }            imageView.setImageResource(mThumbIds[position % mThumbIds.length]);            return imageView;        }        private Context mContext;    }    private static Integer[] mThumbIds = {            R.drawable.gallery_photo_1, R.drawable.gallery_photo_2, R.drawable.gallery_photo_3,            R.drawable.gallery_photo_4, R.drawable.gallery_photo_5, R.drawable.gallery_photo_6,            R.drawable.gallery_photo_7, R.drawable.gallery_photo_8    };}

 

package xiaogang.zhao;import android.content.Context;import android.graphics.Matrix;import android.util.AttributeSet;import android.view.View;import android.view.animation.Transformation;import android.widget.Gallery;public class SizingGallery extends Gallery {public SizingGallery(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected boolean getChildStaticTransformation(View child, Transformation t) {        t.clear();        View selectedChild = getSelectedView();        if (child == selectedChild) {        Matrix matrix = t.getMatrix();        int w2 = child.getWidth() / 2;        int h2 = child.getHeight() / 2;        matrix.postScale(2f, 1.5f, w2, h2);        }        return true;}}

Note:

1. mgalleryitem is used to set the items of the selected Gallery. setselection (integer. max_value/2) to avoid manual sliding to the left to achieve the infinite loop effect, you can modify the value for some simple tests;

2. Set the value of getcount and imageresource to perform remainder processing;

3. you can use sizinggallery to modify the size of the selected image by default. You can also use the default Gallery, and then use the code commented out above to achieve the following: imageview. setlayoutparams (new gallery. layoutparams (100,100 ));

4. For the entire project code, see:

Http://download.csdn.net/detail/androidzhaoxiaogang/3755223

5 .:

 

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.