切换图片(ImageSwitcher)

java.lang.Object;

android.view.View;

android.widget.ViewGroup;

android.widget.FrameLayout;

android.widget.ViewAnimator;

android.widget.ImageSwitcher;

ImageSwitcher 提供了显示图片以及图片切换的动画,这一块能够应用于制作动感影集。

ImageSwitcher 类方法

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

ImageSwitcher 和 Gallery 演示样例

完整project:http://download.csdn.net/detail/sweetloveft/9428069

ViewFactory下述project中主要演示了 ImageSwitcher 和 Gallery 的使用方法,须要注意的是:ViewFactory.makeView 的实现中,ImageView 所要设置的布局类型是 FrameLayout.LayoutParams,除此之外,其它地方出现要设置的參数均为 Gallery.LayoutParams。否则会导致崩溃!

此外还能够学习下,Window 相关的属性

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

1.MainActivity.java

package com.sweetlover.activity;

import com.sweetlover.imageswitcher.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher; @SuppressWarnings("deprecation")
public class MainActivity extends Activity implements ViewFactory,
OnItemSelectedListener { public class SelfAdapter extends BaseAdapter { private Context context = null; public SelfAdapter(Context context) {
super();
this.context = context;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return pic.length;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return pic[position];
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(context);
LayoutParams param = new LayoutParams(WIDTH, HEIGHT);
imageView.setImageResource(pic[position]);
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(param);
return imageView;
}
} private Gallery gallery = null;
private SelfAdapter selfAdapter = null;
private ImageSwitcher imageSwitcher = null;
private FrameLayout.LayoutParams param = null; private static final int WIDTH = 480;
private static final int HEIGHT = 640;
private static final int BACKGND_COLOR = Color.BLACK;
private static final int IN_ANIM = android.R.anim.fade_in;
private static final int OUT_ANIM = android.R.anim.fade_out;
private static final Integer[] pic = { R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3 }; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); gallery = (Gallery) findViewById(R.id.gallery);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
selfAdapter = new SelfAdapter(this);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils
.loadAnimation(this, IN_ANIM));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
OUT_ANIM));
gallery.setAdapter(selfAdapter);
gallery.setOnItemSelectedListener(this);
} @Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
imageSwitcher.setImageResource(pic[position]);
} @Override
public void onNothingSelected(AdapterView<? > parent) {
// TODO Auto-generated method stub } @Override
public View makeView() {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(this);
param = new FrameLayout.LayoutParams(WIDTH, HEIGHT, Gravity.CENTER);
imageView.setBackgroundColor(BACKGND_COLOR);
imageView.setScaleType(ScaleType.CENTER);
imageView.setLayoutParams(param);
return imageView;
}
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" /> <Gallery
android:id="@+id/gallery"
android:background="#660000FF"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="15dp" /> </RelativeLayout>

3.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sweetlover.imageswitcher"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.sweetlover.activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application> </manifest>

阅读《Android 从入门到精通》(24)——切换图片的更多相关文章

  1. Android从入门到精通pdf+书源代码

    不须要积分,免费放送 Android从入门到精通的pdf,入门的好书籍,因为csdn文件大小的限制所以分成了两部分. part1地址:http://download.csdn.net/detail/a ...

  2. Android Volley入门到精通:使用Volley加载网络图片

    在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...

  3. 阅读《Android 从入门到精通》(33)——Intent 分类

    Intent 分类 显式 Intent:Intent("android.intent.action.CALL", Uri.parse("tel:" + stri ...

  4. 阅读《Android 从入门到精通》(12)——自己主动完毕文本框

    自己主动完毕文本框(AutoCompleteTextView) java.lang.Object; android.view.View; android.view.TextView; android. ...

  5. 阅读《Android 从入门到精通》(9)——多项选择

    多项选择(CheckBox) CheckBox 类是 Button 的子类,层次关系例如以下: android.widget.Button android.widget.CompoundButton ...

  6. 阅读《Android 从入门到精通》(17)——进度条

    进度条(ProgressBar) java.lang.Object; android.view.View; android.widget.ProgressBar; ProgressBar 类方法 Pr ...

  7. 阅读《Android 从入门到精通》(15)——数字时钟

    数字时钟(DigitalClock) java.lang.Object; android.view.View; android.widget.TextView; android.widget.Digi ...

  8. 阅读《Android 从入门到精通》(10)——单项选择

    单项选择(RadioGroup) RadioGroup 是 LinearLayout 的子类,继承关系例如以下: android.view.ViewGroup android.widget.Linea ...

  9. 阅读《Android 从入门到精通》(29)——四大布局

    LinearLayout 类方法 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQ ...

随机推荐

  1. TCP/IP——链路层简记

    在TCP/IP协议族中链路层的主要目的有三个: 1,为IP模块发送和接受IP数据报. 2,为ARP模块发送ARP请求和接受ARP应答. 3,为RARP模块发送RARP请求和接受RARP应答. 链路层包 ...

  2. Git in Powershell saying 'Could not find ssh-agent'

    加入系统环境变量D:\Program Files\Git\usr\bin

  3. React Native 系列(九)

    前言 本系列是基于React Native版本号0.44.3写的.很多的App都使用了Tab标签组件,例如QQ,微信等等,就是切换不同的选项,显示不同的内容.那么这篇文章将介绍RN中的Tab标签组件. ...

  4. 【BZOJ 4180】 4180: 字符串计数 (SAM+二分+矩阵乘法)

    4180: 字符串计数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 164  Solved: 75 Description SD有一名神犇叫做Oxe ...

  5. 「清华集训2015」V

    「清华集训2015」V 题目大意: 你有一个序列,你需要支持区间加一个数并对 \(0\) 取 \(\max\),区间赋值,查询单点的值以及单点历史最大值. 解题思路: 观察发现,每一种修改操作都可以用 ...

  6. [SDOI2014]数数 --- AC自动机 + 数位DP

    [SDOI2014]数数 题目描述: 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串. 例如当S=(22,333,0233)时,233是幸运数,2333 ...

  7. dcoker常用命令

    记录一下常用的命令 docker run -t -i  xxxx /bin/bash 运行容器的交互会话shell docker start xxxx 启动容器 docker stop xxxx 停止 ...

  8. 中国剩余定理 hdu 3579

    HDU 3579 Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. js异步处理工作机制(setTimeout, setInterval)

    经常谈到异步,但是发现自己一直没深入理解setTimeout, setInterval,逛论坛的时候发现了这篇好文章,分享一下. ————————————————————以下为原文—————————— ...

  10. CDOJ 1307 ABCDE 前缀和优化dp

    ABCDE 题目连接: http://acm.uestc.edu.cn/#/problem/show/1307 Description Binary-coded decimal (BCD) is a ...