• Gallery和自定义Adapter配合使用,实现图片预览


    Gallery是一个可以拖动的列表,正中对应的是选中的东西.他和spinner有共同的父类:AbsSpinner

    属性:

    android:animationDuration="1000"   图片切换动画持续时间
    android:spacing="8dp"            设置图片之间的间距
    android:unselectedAlpha="0.6"      设置没有选择的图片的透明度

    <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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.kale.gallery.MainActivity" 
        android:orientation="vertical">
        
        <!-- 
            android:animationDuration="1000"    图片切换动画持续时间
            android:spacing="8dp"                 设置图片之间的间距
            android:unselectedAlpha="0.6"        设置没有选择的图片的透明度
         -->
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用ImageView作预览图"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    
        <ImageView
            android:id="@+id/imageView_id"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="60dp"
            android:text="下方是gallery"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    
        <Gallery
            android:id="@+id/gallery_id"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"        
            android:layout_height="80dp"
            android:animationDuration="1000"
            android:spacing="8dp" 
            android:unselectedAlpha="0.3"/>
    
    </LinearLayout>

    MainActivity.java

    package com.kale.gallery;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.Gallery.LayoutParams;
    import android.widget.ImageView;
    
    @SuppressWarnings("deprecation")
    public class MainActivity extends Activity {
    
        ImageView iV;
        Gallery gallery;
        
        int[] imageIds = new int [] {
                R.drawable.appstore,
                R.drawable.cydia,
                R.drawable.itunes,
                R.drawable.mail,
                R.drawable.safair
        };
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            viewInit();
            gallery.setAdapter(new ImageAdapter(this));
            gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
    
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO 自动生成的方法存根
                    iV.setImageResource(imageIds[arg2]);
                    
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO 自动生成的方法存根
                    
                }
            });
        }
        
        
        public class ImageAdapter extends BaseAdapter {
            private Context mContext;
                public ImageAdapter(Context context) {
                mContext = context;
            }
    
            public int getCount() { 
                return imageIds.length;
            }
    
            public Object getItem(int position) {
                return position;
            }
    
            public long getItemId(int position) {
                return position;
            }
    
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView image = new ImageView(mContext);
                image.setImageResource(imageIds[position]);
                //设置缩放类型
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                image.setAdjustViewBounds(true);
                //设置布局参数
                image.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                return image;
            }
        }
    
        private void viewInit() {
            // TODO 自动生成的方法存根
            gallery = (Gallery) findViewById(R.id.gallery_id);
            iV = (ImageView)findViewById(R.id.imageView_id);
        }
    
    
    }
  • 相关阅读:
    主成分分析法
    K-means算法
    高斯混合模型
    data structure test
    八皇后问题求解
    商品管理系统课程设计
    哈佛商学院20部必看电影
    usaco1.1
    Visual Studio 2012 出现关于ActivityLog.xml错误的解决方案
    2012蓝桥杯决赛题
  • 原文地址:https://www.cnblogs.com/tianzhijiexian/p/3853631.html
Copyright © 2020-2023  润新知