您的位置:首页 > 其它

图片的轮转

2015-09-01 14:11 267 查看
图片的选中后 ,底部对选中的图片放大;

首先 先在布局种定义:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Gallery android:layout_height="80dp" android:layout_width="match_parent" android:spacing="2dp" android:gravity="center_vertical" android:background="#55000000" android:id="@+id/gallery"/>
    <ImageSwitcher android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/switcher" android:layout_weight="1"/>

</LinearLayout>

gallery 是用于图片的左右滑动选择的控件(具体的可以百度);

imageswitcher 是图片的轮转的控件(同上)

如果需要实现不同的效果

可以在写一个字定义的(我这写下自定义的);

自定义的布局

<?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">
    <ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:scaleType="fitXY" android:layout_weight="1" android:id="@+id/item_gallery_image"/><TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/item_gallery_text" android:gravity="center"/>

</LinearLayout>


下面是实现的代码:

public class MainActivity extends Activity implements AdapterView.OnItemClickListener,
        ViewSwitcher.ViewFactory {
    //定义ImageSwitcher类对象
    private ImageSwitcher mSwitcher;
    //文本资源
    private String[] titles = {"1","2","3","4"};//你要加的文字
    //大图资源
    private Integer[] mThumbIds = {
            R.drawable.liu,    R.drawable.qie,
            R.drawable.san,    R.drawable.wu,

    };
    //    private Integer[] mThumbIds = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
//            R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
//            R.drawable.sample_7 };
    //大图对应的小图资源
    private Integer[] mImageIds = {
            R.drawable.liu,    R.drawable.qie,
            R.drawable.san,    R.drawable.wu,

    };
    //    private Integer[] mImageIds = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
//            R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,
//            R.drawable.sample_thumb_5, R.drawable.sample_thumb_6, R.drawable.sample_thumb_7 };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置窗体无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        //设置图片的滑动效果
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

        Gallery g = (Gallery) findViewById(R.id.gallery);
        //设置Gallery的适配器
        g.setAdapter(new ImageAdapter(this, mThumbIds.length));
        //Gallery中每个条目的点击事件监听
        g.setOnItemClickListener(this);
        //设置默认其实位置为第二张图片
        g.setSelection(1);
    }

    public void onItemSelected(AdapterView parent, View v, int position, long id) {
        mSwitcher.setImageResource(mThumbIds[position % mImageIds.length]);//更换图片
    }
    public void onNothingSelected(AdapterView parent) {
    }

    @Override
    public View makeView() {
        ImageView i = new ImageView(this);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        return i;
    }
    //Gallery的适配器
    public class ImageAdapter extends BaseAdapter {
        private int mGalleryItemBackground;
        //定义map存储划过的位置
        private HashMap<Integer, View> mViewMaps;
        private int mCount;
        //定义布局加载器
        private LayoutInflater mInflater;

        public ImageAdapter(Context c, int count) {
            this.mCount = count;
            mViewMaps = new HashMap<Integer, View>(count);
            mInflater = LayoutInflater.from(MainActivity.this);
            //定义图片的背景样式
        /*    TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground, 0);*/
            //定义可以重复使用.可回收
         /*   a.recycle();*/
        }

        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) {

            View viewGroup = mViewMaps.get(position%mCount);
            ImageView imageView = null;
            TextView textView = null;
            if(viewGroup==null) {
                viewGroup = mInflater.inflate(R.layout.layout, null);
                imageView = (ImageView) viewGroup.findViewById(R.id.item_gallery_image);
                textView = (TextView) viewGroup.findViewById(R.id.item_gallery_text);
                imageView.setBackgroundResource(mGalleryItemBackground);
                mViewMaps.put(position%mCount, viewGroup);
                imageView.setImageResource(mImageIds[position % mImageIds.length]);
                textView.setText(titles[position % mImageIds.length]);
            }

            return viewGroup;
        }
    }
    //记录当前位置
    private int curruntPos = -1;
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (curruntPos == position) {
            return;
        } else
            curruntPos = position;
        mSwitcher.setImageResource(mThumbIds[position % mThumbIds.length]);
    }
}

如果有侵权,请告知!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: