Gallery 和ImageSwitcher

package com.example.administrator.mytestapp.AlbumDemo;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.example.administrator.mytestapp.R;

public class Album extends AppCompatActivity implements ViewSwitcher.ViewFactory {
private Integer[] mThumbIds={
        R.drawable.ic_user_background,R.drawable.author,R.drawable.ic_user_background,
        R.drawable.author,R.drawable.ic_user_background,R.drawable.author,
        R.drawable.ic_user_background,R.drawable.author};
    private Integer[] mImgIds={
        R.drawable.ic_user_background, R.drawable.author, R.drawable.ic_user_background, R.drawable.author,
            R.drawable.ic_user_background, R.drawable.author, R.drawable.ic_user_background, R.drawable.author
    };
    private ImageSwitcher mImgSwitch;
    private Gallery mGallery;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_album);
        mImgSwitch= (ImageSwitcher) findViewById(R.id.ImgSwitch);
        mImgSwitch.setFactory(this);
        mImgSwitch.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        mImgSwitch.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
        mGallery= (Gallery) findViewById(R.id.ImgGallery);
        mGallery.setAdapter(new ImageAdapter(this));
        mGallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                mImgSwitch.setImageResource(mImgIds[position]);
                Toast.makeText(Album.this, "你选择了"+position+"号图片", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }

    @Override
    public View makeView() {
        ImageView img=new ImageView(this);
        img.setBackgroundColor(0xFF00000);
        img.setScaleType(ImageView.ScaleType.FIT_CENTER);
        img.setLayoutParams(new ImageSwitcher.LayoutParams(Gallery.LayoutParams.MATCH_PARENT,Gallery.LayoutParams.MATCH_PARENT));
        return img;
    }
    public class ImageAdapter extends BaseAdapter
    {
        private Context mContext;
        private ImageAdapter(Context c)
        {
            mContext=c;
        }

        @Override
        public int getCount() {
            return mThumbIds.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) {
            ImageView mImgView=new ImageView(mContext);
            mImgView.setImageResource(mThumbIds[position]);
            //设置图片视图边界保持他的长度比例
            mImgView.setAdjustViewBounds(true);
            mImgView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.WRAP_CONTENT, Gallery.LayoutParams.WRAP_CONTENT));
            return mImgView;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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="com.example.administrator.mytestapp.AlbumDemo.Album">
    <ImageSwitcher
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:id="@+id/ImgSwitch">
    </ImageSwitcher>
    <Gallery
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/ImgGallery"
        android:background="#550000"
        android:spacing="16dp"
        android:layout_alignParentBottom="true"></Gallery>

</RelativeLayout>

转载于:https://www.cnblogs.com/jiang2538406936/p/5842381.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值