android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本1
来源:互联网 发布:raid2数据图 编辑:程序博客网 时间:2024/06/16 15:33
最终效果图:
当我们在最下边的gallery中切换图片时,上面的大图片会自动切换,切换时有动画效果哦,很简单的一个程序,有待完善更多的功能!
activity代码:
- package cn.com.chenzheng_java;
- import android.app.Activity;
- import android.content.Context;
- 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.AdapterView.OnItemSelectedListener;
- import android.widget.ImageView.ScaleType;
- import android.widget.ViewSwitcher.ViewFactory;
- /**
- * @description 对图片的一个小小展示
- * @author chenzheng_java
- * @since 2011/3/17
- *
- */
- public class GalleryActivityextends Activity implements OnItemSelectedListener {
- int[] imagesId = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3,
- R.drawable.a4
- };
- int index = R.drawable.a1;
- ImageSwitcher imageSwitcher;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /**
- * 设置窗口无标题栏,一定要在setContentView前进行设置哦
- */
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.gallery);
- Gallery gallery = (Gallery) findViewById(R.id.gallery1);
- gallery.setOnItemSelectedListener(this);
- imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
- imageSwitcher.setVisibility(View.VISIBLE);
- /***
- * setInAnimation可以设置淡入动画
- * setOutAnimation可以设置淡出动画
- */
- imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
- getApplicationContext(), android.R.anim.fade_in));
- imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
- getApplicationContext(), android.R.anim.fade_out));
- imageSwitcher.setFactory(new ViewFactory() {
- /**
- * 创建一个新的图片放置到ImageSwitcher上,可以使用其设置背景哦。
- * 一般只会在创建时调用一次。相当于为我们创建一个进行动画效果时的一个背景图。
- * 此方法亦可以通过setView替代
- */
- @Override
- public View makeView() {
- ImageView imageView = new ImageView(GalleryActivity.this);
- // 设置截取模式
- imageView.setScaleType(ScaleType.CENTER_INSIDE);
- imageView.setBackgroundResource(R.drawable.ground);
- Toast.makeText(getApplicationContext(), "执行了一次",
- Toast.LENGTH_SHORT).show();
- return imageView;
- }
- });
- gallery.setVisibility(View.VISIBLE);
- gallery.setAdapter(new ImageAdapter(this));
- }
- class ImageAdapterextends BaseAdapter {
- private Context context2;
- public ImageAdapter(Context context) {
- context2 = context;
- }
- @Override
- public int getCount() {
- return imagesId.length;
- }
- @Override
- public Object getItem(int position) {
- return imagesId[position];
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View image2, ViewGroup parent) {
- ImageView image = new ImageView(context2);
- image.setBackgroundResource(imagesId[position]);
- /**
- * setScaleType()可以设置当图片大小和容器大小不匹配时的剪辑模式.
- * ScaleType.CENTER_INSIDE的意思是,按图片的原比例缩小或者扩大,直到长或者宽中的任何一个顶到容器为止
- */
- image.setScaleType(ScaleType.CENTER_INSIDE);
- return image;
- }
- }
- /**
- * 当gallery的图像发生变化时,同步ImageSwitcher的变化
- */
- @Override
- public void onItemSelected(AdapterView<?> parent, View view,int position,
- long id) {
- imageSwitcher.setImageResource(imagesId[position]);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- }
gallery.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:gravity="center_horizontal"
- >
- <ImageSwitcher
- android:id="@+id/imageSwitcher"
- android:layout_height="350dp"
- android:layout_width="fill_parent"
- ></ImageSwitcher>
- <Galleryandroid:id="@+id/gallery1"
- android:gravity="center"
- android:layout_width="70dp"
- android:layout_height="100dp"></Gallery>
- </LinearLayout>
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本1
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本1
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本2
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本2
- ImageSwitcher配合Gallery的简单使用
- (转)Android 模仿淘宝滑动查看图片的效果 Gallery + ImageSwitcher
- Eg:Gallery与ImageSwitcher结合的图片浏览小程序
- Android复习之切换图片ImageSwitcher&Gallery
- android之ImageSwitcher 图片查看
- ImageSwitcher的使用(一):与Gallery配合使用
- Gallery与ImageSwitcher的图片显示
- Android之Gallery和ImageSwitcher结合的用法
- Android 基础控件之ImageSwitcher、Gallery功能的实现
- ImageSwitcher和Gallery组件的应用
- ImageSwitcher和Gallery组件的应用
- Android中Gallery和ImageSwitcher的使用
- Android中Gallery和ImageSwitcher的使用
- Android 控件之Gallery和ImageSwitcher图片切换器
- 小型 Twitter 的系统 源碼+註釋
- Android-网络请求数据使用apache httpclient v4
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本2
- Android 程序获取、设置铃声、音量、静音、扬声器
- Struts2 获取 Request、Response和Session
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本1
- 【天道酬勤】 腾讯、百度、网易游戏、华为Offer及笔经面经
- Bootstrap 中列表行 <tr> 的显示样式
- test dword ptr [eax],eax ; probe page.--VS报错!
- android之计时器chronometer
- IOS5 2D绘图 -- CGFont
- C# 操作excel
- 我的Microsoft Community介绍主页
- android之检测手机电池