[置顶] Android Gallery橱窗效果

美女图片查看器——Gallery橱窗效果的实现

先看一下效果图
[置顶] Android Gallery橱窗效果_第1张图片

主界面的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.jeffery.actionbar.gallerydemo.MainActivity" tools:showIn="@layout/app_bar_main">


    <!--背景大图-->
    <ImageView  android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/bg_1" />

    <!--窗口-->
    <Gallery  android:id="@+id/gallery" android:layout_alignParentBottom="true" android:spacing="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" >

    </Gallery>
</RelativeLayout>

下面是MainActivity 的部分代码,要注意无线循环

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private Gallery gallery;
    private ImageView iv;

    //图片集
    private Integer[] image = {
            R.drawable.bg_1,
            R.drawable.bg_2,
            R.drawable.bg_3,
            R.drawable.bg_4,
            R.drawable.bg_5,
            R.drawable.bg_6,
            R.drawable.bg_7,
            R.drawable.bg_8,
            R.drawable.bg_9,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        initView();
        initData();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    private void initView() {
        gallery = (Gallery)findViewById(R.id.gallery);
        iv = (ImageView)findViewById(R.id.iv);
    }

    private void initData() {

        ImageAdapter adapter =  new ImageAdapter(this,image);

        gallery.setAdapter(adapter);
        //初始化的时候默认选中初始的位置(就和初始位置为0时一样)
        int indext = (Integer.MAX_VALUE / 2)-(Integer.MAX_VALUE / 2)%image.length;
        gallery.setSelection(indext);
        //设置gallery的点击事件
        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,position+"",Toast.LENGTH_SHORT).show();
                //设置背景图
                iv.setImageResource(image[position%image.length]);
            }
        });

    }

ImageAdapter代码块,要注意一下TypedArray的使用,主要是为了获得attr中GalleryDemo的属性

package com.jeffery.actionbar.gallerydemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.jeffery.actionbar.gallerydemp.R;

/** * Created by JefferyAnd on 2015/11/26. */
public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private Integer[] image;
    private int galleryBackground;//背景

    public ImageAdapter(Context mContext, Integer[] image) {
        this.mContext = mContext;
        this.image = image;
        TypedArray a = mContext.obtainStyledAttributes(R.styleable.GalleryDemo);//为了获得attr中GalleryDemo的属性
        galleryBackground = a.getResourceId(R.styleable.GalleryDemo_android_galleryItemBackground, 0);

        a.recycle();//否则会对下次造成影响
    }

    @Override
    public int getCount() {
// return image.length;
        return Integer.MAX_VALUE;
    }

    @Override
    public Object getItem(int position) {
        return image[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView view = new ImageView(mContext);
// view.setImageResource(image[position]);
        view.setImageResource(image[position % image.length]);
        view.setLayoutParams(new Gallery.LayoutParams(120, 160));
        view.setScaleType(ImageView.ScaleType.FIT_XY);
        view.setBackgroundResource(galleryBackground);

        return view;
    }
}

attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="GalleryDemo">
        <attr name="android:galleryItemBackground"></attr>
    </declare-styleable>
</resources>

demo下载地址:
http://pan.baidu.com/s/1o6s3Gtc

你可能感兴趣的:(android,图片,gallery,美女图片查看器)