画廊视图Gallery

简介: 幻灯片图片浏览器 1.布局 1 7 8 12 13 21 22 2.逻辑控制 1 package com.example.androidgallery; 2 3 import android.

幻灯片图片浏览器

1.布局

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".AndroidGalleryActivity" >
 7 
 8     <ImageSwitcher
 9         android:id="@+id/imgswi"
10         android:layout_width="320dp"
11         android:layout_height="320dp" />
12 
13     <Gallery
14         android:id="@+id/gallery"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:layout_marginTop="25dp"
18         android:unselectedAlpha="0.6"
19         android:spacing="3pt"
20          />
21 
22 </LinearLayout>

2.逻辑控制

  1 package com.example.androidgallery;
  2 
  3 import android.os.Bundle;
  4 import android.app.Activity;
  5 import android.content.res.TypedArray;
  6 import android.support.v4.view.ViewPager.LayoutParams;
  7 import android.view.Menu;
  8 import android.view.View;
  9 import android.view.ViewGroup;
 10 import android.view.animation.AnimationUtils;
 11 import android.widget.AdapterView;
 12 import android.widget.AdapterView.OnItemSelectedListener;
 13 import android.widget.BaseAdapter;
 14 import android.widget.Gallery;
 15 import android.widget.ImageSwitcher;
 16 import android.widget.ImageView;
 17 import android.widget.ViewSwitcher.ViewFactory;
 18 
 19 public class AndroidGalleryActivity extends Activity {
 20 
 21     int[] imageIds=new int[]{
 22     R.drawable.mm,
 23     R.drawable .mm2,
 24     R.drawable.mm3,
 25     R.drawable.mm4
 26     } ;
 27     
 28     @Override
 29     protected void onCreate(Bundle savedInstanceState) {
 30         super.onCreate(savedInstanceState);
 31         setContentView(R.layout.activity_android_gallery);
 32         final Gallery gallery=(Gallery)this.findViewById(R.id.gallery);
 33         final ImageSwitcher imgswi=(ImageSwitcher)this.findViewById(R.id.imgswi);
 34         //设置ViewFactory对象
 35         imgswi.setFactory(new ViewFactory() {
 36             
 37             @Override
 38             public View makeView() {
 39                 ImageView imageView=new ImageView(AndroidGalleryActivity.this);
 40                 imageView.setBackgroundColor(0xff0000);
 41                 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
 42                 imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
 43                 return imageView;
 44             }
 45         });
 46         //设置图片更换动画效果
 47         imgswi.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
 48         imgswi.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
 49         //创建一个BaseAdapter对象,负责提供Gallery显示每张图片
 50         BaseAdapter adapter=new BaseAdapter() {
 51             
 52             @Override
 53             public View getView(int position, View convertview, ViewGroup parent) {
 54                 //创建一个ImageView
 55                 ImageView imageView=new ImageView(AndroidGalleryActivity.this);
 56                 imageView.setImageResource(imageIds[position%imageIds.length]);
 57                 //设置ImageView缩放类型
 58                 imageView.setScaleType(ImageView.ScaleType.FIT_XY);
 59                 imageView.setLayoutParams(new Gallery.LayoutParams(75,100));
 60                 /*TypedArray typeArray=obtainStyledAttributes(R.styleable.Gallery);
 61                 imageView.setBackgroundResource(TypedArray)*/
 62                 return imageView;
 63             }
 64             
 65             @Override
 66             public long getItemId(int arg0) {
 67                 // TODO Auto-generated method stub
 68                 return arg0;
 69             }
 70             
 71             @Override
 72             public Object getItem(int arg0) {
 73                 // TODO Auto-generated method stub
 74                 return arg0;
 75             }
 76             
 77             @Override
 78             public int getCount() {
 79                 // TODO Auto-generated method stub
 80                 return imageIds.length;
 81             }
 82         };
 83         
 84         gallery.setAdapter(adapter);
 85         gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
 86 
 87             @Override
 88             public void onItemSelected(AdapterView<?> parent, View view,
 89                     int position, long id) {
 90                 // TODO Auto-generated method stub
 91                 imgswi.setImageResource(imageIds[position%imageIds.length]);
 92             }
 93 
 94             @Override
 95             public void onNothingSelected(AdapterView<?> parent) {
 96                 // TODO Auto-generated method stub
 97                 
 98             }
 99             
100         });
101     }
102 
103     @Override
104     public boolean onCreateOptionsMenu(Menu menu) {
105         // Inflate the menu; this adds items to the action bar if it is present.
106         getMenuInflater().inflate(R.menu.activity_android_gallery, menu);
107         return true;
108     }
109 
110 }

目录
相关文章
|
3月前
|
前端开发 JavaScript 算法
【web前端技术】响应式画廊Gallery插件-Justified-Gallery
【web前端技术】响应式画廊Gallery插件-Justified-Gallery
52 0
|
C#
WPF ListView展示层叠信息
原文:WPF ListView展示层叠信息 通常我们在ListView中展示一列同类数据,例如城市名称。不过可以对ListView的DataTemplate稍作修改,让其显示层叠信息。例如:需要在ListView中显示省份和省份对应的城市名称。
1082 0
|
Android开发 iOS开发 索引
Xamarin自定义布局系列——支持无限滚动的自动轮播视图CarouselView
原文:Xamarin自定义布局系列——支持无限滚动的自动轮播视图CarouselView 背景简述 自动轮播视图(CarouselView)在现在App中的地位不言而喻,绝大多数的App中都有类似的视图,无论是WebApp还是Native App。
1828 0
|
Web App开发 C# Windows
WPF图片浏览器(显示大图、小图等)
原文:WPF图片浏览器(显示大图、小图等) 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangshubo1989/article/details/46784601 1.概述                最近利用WPF做了一个图片浏览器,能够将文件夹中的所有图片以小图的形式显示,并将选中的图片以512*512大小显示。
2494 0
Winform中Picture控件图片的拖拽显示
原文:Winform中Picture控件图片的拖拽显示 注解:最近做了一个小工具,在Winform中对Picture控件有一个需求,可以通过鼠标从外部拖拽图片到控件的上,释放鼠标,显示图片! 首先你需要对你的整个Fom窗口的AllowDrop设置Ture                   //函数从动态链接库中倒入(模拟鼠标事件) [System.
1113 0
|
Web App开发
ImageList+Listview设计图片浏览器
需要在窗体增加imageList和listview控件,并把ListView控件的LargeImageList设置为imageList1 ListView控件显示图片的大小可以在imageList1控件中调整ImageSize属性,如果图片失真,可以设置imageList1控件的ColorDepth值为Depth32Bit.
740 0