您的位置:首页 > 移动开发 > Android开发

Android实现动画效果

2017-10-14 08:46 190 查看
利用Handler实现信息的传递

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.hsy.handertest.MainActivity">

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ivImage"/>

</LinearLayout>


MainActivity.java

package com.example.hsy.handertest;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

int [] imageID = new int[]{
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d

};
ImageView ivImage ;
int currentImaged = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivImage = (ImageView) findViewById(R.id.ivImage);
final Handler myHandler = new Handler(){
public void handleMessage(Message msg){
if(msg.what == 0x1233){
ivImage.setImageResource(imageID[(currentImaged++)%imageID.length]);
}
}
};
new Timer().schedule(new TimerTask() {
@Override
public void run() {
myHandler.sendEmptyMessage(0x1233);
}
},0,1200);
}
}




中间的图片就会每隔1.2s换一张
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: