androidswitcher的使用
来源:互联网 发布:网络金融最初发展阶段 编辑:程序博客网 时间:2024/06/08 07:48
<?xml version="1.0" encoding="UTF-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageSwitcher android:id="@+id/imageSwitcher1" android:layout_width="fill_parent" android:layout_height="fill_parent"> </ImageSwitcher> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:id="@+id/viewGroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp" android:gravity="center_horizontal" android:orientation="horizontal" > </LinearLayout> </RelativeLayout></FrameLayout>
package com.example.photoalbum;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.view.ViewGroup;import android.view.animation.AnimationUtils;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout.LayoutParams;import android.widget.Toast;import android.widget.ViewSwitcher.ViewFactory;public class ShowPhotoActivity extends Activity implements ViewFactory, OnTouchListener{/*** ImagaSwitcher 的引用*/private ImageSwitcher mImageSwitcher;/*** 图片id数组*/private int[] imgIds;/*** 当前选中的图片id序号*/private int currentPosition;/*** 按下点的X坐标*/private float downX;/*** 装载点点的容器*/private LinearLayout linearLayout;/*** 点点数组*/private ImageView[] tips; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.show_photo);imgIds = new int[]{R.drawable.item01,R.drawable.item02,R.drawable.item03,R.drawable.item04,R.drawable.item05, R.drawable.item06, R.drawable.item07, R.drawable.item08,R.drawable.item09,R.drawable.item10, R.drawable.item11, R.drawable.item12};//实例化ImageSwitchermImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);//设置FactorymImageSwitcher.setFactory(this);//设置OnTouchListener,我们通过Touch事件来切换图片mImageSwitcher.setOnTouchListener(this);linearLayout = (LinearLayout) findViewById(R.id.viewGroup);tips = new ImageView[imgIds.length];for(int i=0; i<imgIds.length; i++){ImageView mImageView = new ImageView(this);tips[i] = mImageView;LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); layoutParams.rightMargin = 3;layoutParams.leftMargin = 3;mImageView.setBackgroundResource(R.drawable.page_indicator_unfocused);linearLayout.addView(mImageView, layoutParams);}//这个我是从上一个界面传过来的,上一个界面是一个GridViewcurrentPosition = getIntent().getIntExtra("position", 0);mImageSwitcher.setImageResource(imgIds[currentPosition]);setImageBackground(currentPosition);}/** * 设置选中的tip的背景 * @param selectItems */ private void setImageBackground(int selectItems){ for(int i=0; i<tips.length; i++){ if(i == selectItems){ tips[i].setBackgroundResource(R.drawable.page_indicator_focused); }else{ tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused); } } } @Overridepublic View makeView() {final ImageView i = new ImageView(this);i.setBackgroundColor(0xff000000);i.setScaleType(ImageView.ScaleType.CENTER_CROP);i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));return i ;}@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:{//手指按下的X坐标downX = event.getX();break;}case MotionEvent.ACTION_UP:{float lastX = event.getX();//抬起的时候的X坐标大于按下的时候就显示上一张图片if(lastX > downX){if(currentPosition > 0){//设置动画,这里的动画比较简单,不明白的去网上看看相关内容mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_in));mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_out));currentPosition --;mImageSwitcher.setImageResource(imgIds[currentPosition % imgIds.length]);setImageBackground(currentPosition);}else{Toast.makeText(getApplication(), "已经是第一张", Toast.LENGTH_SHORT).show();}} if(lastX < downX){if(currentPosition < imgIds.length - 1){mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_in));mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.lift_out));currentPosition ++ ;mImageSwitcher.setImageResource(imgIds[currentPosition]);setImageBackground(currentPosition);}else{Toast.makeText(getApplication(), "到了最后一张", Toast.LENGTH_SHORT).show();}}}break;}return true;}}
http://blog.csdn.net/xiaanming/article/details/8997260 制作动画的网站
上面切换图片主要用到的就是动画了,用的是translate移动动画,不了解动画的同学到这里看看http://blog.csdn.net/xiaanming/article/details/8997260,我就不介绍了,接下来我吧动画代码贴出来,在res新建一个anim的目录,如下图
左边进入的动画,left_in.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="-100%p"
- android:toXDelta="0"
- android:duration="500"/>
- </set>
- <?xml version="1.0" encoding="UTF-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="0"
- android:toXDelta="-100%p"
- android:duration="500"/>
- </set>
- <?xml version="1.0" encoding="UTF-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="100%p"
- android:toXDelta="0"
- android:duration="500"/>
- </set>
- <?xml version="1.0" encoding="UTF-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="0"
- android:toXDelta="100%p"
- android:duration="500"/>
- </set>
0 0
- androidswitcher的使用
- GUID使用的使用
- ${}的使用
- 的使用
- @[],@()的使用
- ->的使用
- +、-、*、/、%的使用
- &&的使用
- %:的使用
- Makefile中使用$$的使用
- Makefile中使用$$的使用
- Red5的使用--使用日志
- prefernce的使用具体使用
- 【工具使用】git 的使用
- 关于使用AsyncTaskLoader的使用
- 使用CABasicAnimation的基本使用
- 使用RocketMQ的客户端使用
- gulp的使用基本使用
- win7安装Android环境
- 磊科Q3刷236W 免交换机双拨
- java nio 基础(一)
- Merge Two Sorted Lists
- 网站中防盗链技术
- androidswitcher的使用
- Cocos2d-x VLC Player
- 关于vc6.0下配置GDI+的问题
- 机器学习基石——第3-4讲.Types of Learning
- 大数据,怎么搞?
- C++ 服务端开发之Socket 入门
- 机器学习基石——第5-6讲.Training versus Testing
- 【JSP开发】利用cookie返回上次登陆时间
- maven install时报错Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile