zoukankan      html  css  js  c++  java
  • UI组件之AdapterView及其子类(四)Gallery画廊控件使用

    听说 Gallery如今已经不使用了,API使用ViewPaper取代了,以后再学专研ViewPaper吧如今说说Gallery画廊,就是不停显示图片的意思

    Gallery是用来水平滚动的显示一系列项目。Gallery组件能够横向显示一个图像列表,当单击当前图像的后一个图像时,这个图像列表会向左移动一格,当单击当前图像的前一个图像时,这个图像列表会向右移动一样

    也能够通过拖动的方式来向左和向右移动图像列表在使用Gallery的时候,我们应指定他的背景。不然它的项目会紧凑的贴在一起。不会产生画廊的效果了。可是。你也能够通过指定Gallery的属性来设置距离。高度等參数来产生画廊的效果。

    Gallery的xml属性:


    能够看到仅仅有4个属性:

    animationDuration:当布局已经改变,设置一个过渡动画应该执行多长时间(以毫秒为单位)。
    gravity:项目的位置,对齐方式
    spacing:设置项目之间间距,图片之间的间距
    unselectedAlpha:设置没有选择时的Alpha,没有选中图片的透明度


    一个简单的样例,使用一个Gallery,一个ImageView显示选中的图片。

    方法是:Gallery既然AdapterView的子类当然是能够使用Adapter来提供列表项的。当点击列表项时。ImageView显示图片

    main.xml

    <?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"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="320dp"
            android:layout_height="320dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/baiyang" />
    <!-- android:unselectedAlpha设置没选中图片的透明度 android:spacing设置图片之间间隔  -->
        <Gallery
            android:id="@+id/gallery1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:spacing="4dp"
            android:unselectedAlpha="0.6" />
    </LinearLayout>
    
    MainActivity.java
    public class MainActivity extends Activity {
          int[] imagesids=new int[]{
        	R.drawable.p1,	R.drawable.p2,R.drawable.p3,R.drawable.p4,
        	R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8
          };
          Gallery g;
          ImageView image;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            g=(Gallery) findViewById(R.id.gallery1);
            image=(ImageView) findViewById(R.id.imageView1);
            
            BaseAdapter ba=new BaseAdapter(){
    
    			@Override
    			public int getCount() {
    				// TODO Auto-generated method stub
    				return imagesids.length;
    			}
    
    			@Override
    			public Object getItem(int position) {
    				// TODO Auto-generated method stub
    				return position;
    			}
    
    			@Override
    			public long getItemId(int position) {
    				// TODO Auto-generated method stub
    				return position;
    			}
                //设置子选项ImageView的外观
    			@Override
    			public View getView(int position, View convertView, ViewGroup parent) {
    				// TODO Auto-generated method stub
    				 ImageView imageview= new ImageView(MainActivity.this);
    				 imageview.setImageResource(imagesids[position]);
    				 
    				 imageview.setScaleType(ImageView.ScaleType.FIT_XY);
    				 imageview.setLayoutParams(new Gallery.LayoutParams(160,200));
    								
    				return imageview;
    			}
            	
            };
            //设置adapter
            g.setAdapter(ba);
            //设置选项被选监听器
            g.setOnItemSelectedListener(new OnItemSelectedListener(){
    
    			@Override
    			public void onItemSelected(AdapterView<?> parent, View view,
    					int position, long id) {
    				// TODO Auto-generated method stub
    				
    				image.setImageResource(imagesids[position]);
    				
    			}
    
    			@Override
    			public void onNothingSelected(AdapterView<?> parent) {
    				// TODO Auto-generated method stub
    				
    			}
            	
            });
        }
    


    看了几篇好的博客,能够实现循环Gallery,3DGallery的效果,先放一下。如今时间紧没有时间专研。如今留着以后学习

    http://blog.csdn.net/loongggdroid/article/details/7581236

    http://blog.csdn.net/herryz/article/details/6141957

    http://blog.csdn.net/easyer2012/article/details/8244483

  • 相关阅读:
    UML类图和用例图
    设计模式基本原则
    c# 协变和逆变
    git本地忽略
    计算器科学概论-数据操控
    计算机科学概论-数据存储
    docker部署gitlab-ce
    sqlserver2008R2 本地不能用localhost连接
    Redis常用命令
    C# 值类型和引用类型的区别
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7027755.html
Copyright © 2011-2022 走看看