使用Animation轮播图片
16lz
2021-01-24
1. 效果:
页面显示几幅图片,图片从右往左进行轮播。
2. 实现方式:
(1)工程目录结构:
(2) ImageActivity.java代码:
package com.app;import com.test.R;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.TranslateAnimation;import android.widget.ImageView;import android.widget.TextView;public class ImageActivity extends Activity {/** Called when the activity is first created. */public ImageView imageView;public ImageView imageView2;/* * View Animation: 视图动画在古老的Android版本系统中就已经提供了,只能被用来设置View的动画。Drawable Animation: 这种动画(也叫Frame动画、帧动画)其实可以划分到视图动画的类别,专门用来一个一个的显示Drawable的resources,就像放幻灯片一样。Property Animation: 属性动画只对Android 3.0(API 11)以上版本的Android系统才有效,这种动画可以设置给任何Object,包括那些还没有渲染到屏幕上的对象。这种动画是可扩展的,可以让你自定义任何类型和属性的动画。 */public Animation animation1;public Animation animation2;public TextView text;public boolean juage = true;public int images[] = new int[] { R.drawable.f5, R.drawable.p1,R.drawable.wz1, R.drawable.f4, R.drawable.hwwq };public int count = 0;public Handler handler = new Handler();// Runnable只是一个接口,它里面只有一个run()方法,没有start()方法// 即使实现了Runnable接口,那也无法启动线程,必须依托其他类。public Runnable runnable = new Runnable() {@Overridepublic void run() {// 1 AnimationSet是Animation的子类// 2 一个AnimationSet包含了一系列的Animation// 3 针对AnimationSet设置一些Animation的常见属性AnimationSet animationSet1 = new AnimationSet(true);AnimationSet animationSet2 = new AnimationSet(true);imageView2.setVisibility(0);// 动画过渡, 一个从左向右的动画TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,-1f, Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 0f);// 2秒ta.setDuration(2000);animationSet1.addAnimation(ta);// 有一个移动的动画紧跟一个淡出的动画,// 如果你不把移动的动画的setFillAfter置为true,// 那么移动动画结束后,View会回到原来的位置淡出,// 如果setFillAfter置为true, 就会在移动动画结束的位置淡出animationSet1.setFillAfter(true);ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF, 0f);// 2秒ta.setDuration(2000);animationSet2.addAnimation(ta);animationSet2.setFillAfter(true);//iamgeView 出去 imageView2 进来imageView.startAnimation(animationSet1);imageView2.startAnimation(animationSet2);imageView.setBackgroundResource(images[count % 5]);count++;imageView2.setBackgroundResource(images[count % 5]);text.setText(String.valueOf(count));// 调用此Runnable对象,以实现每6秒实现一次的定时器操作if (juage)handler.postDelayed(runnable, 6000);Log.i("handler", "handler");}};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.image);imageView = (ImageView) findViewById(R.id.imageView);imageView2 = (ImageView) findViewById(R.id.imageView2);text=(TextView)findViewById(R.id.text);text.setText(String.valueOf(count));//将iamgeView先隐藏,然后显示imageView2.setVisibility(4);// 调用此Runnable对象,以实现每2秒实现一次的定时器操作handler.postDelayed(runnable, 2000);}public void onPause() {juage = false;super.onPause();}}
(2) image.xml布局文件代码:
<?xml version="1.0" encoding="utf-8"?>
(3)left_in.xml动画代码:
(4)left_out.xml动画代码:
(5)往res/drawable文件下,添加轮播图片。
(6)源码下载地址:
https://github.com/diaoguangqiang/android/blob/Image/IamgeTranslat.rar
至此结束。
3. 补充:
(1)轮播坐标系:
(2)若image.xml布局文件有问题,参考: https://blog.csdn.net/yingchunhua365/article/details/89163000 进行解决。
更多相关文章
- Android中Fragment之间replace(替换)的动画效果的实现
- 一、Android(安卓)NDK编程预备之Java jni简介
- Android(安卓)cocos2d 弹弓游戏 Catapult 源代码
- Android(安卓)Notification基础知识
- Android(安卓)自定义Dialog 的使用
- Android一行代码 搞定图片太多,太大OOM(内存溢出)
- setTheme问题2
- android 1.6 launcher研究之launcher的左右滑动
- Android加载网络图片