使用FrameLayout(帧布局) 和Timer实现动态图片

 1.实现图片的轮换显示,定义帧布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TableLayout android:layout_width="fill_parent"
			 android:layout_height="wrap_content"
			 android:stretchColumns="0,1,2,3">
	<TableRow>
		<TextView android:text="水果"></TextView>
		<TextView android:text="西瓜 "></TextView>
		<TextView android:text="香蕉"></TextView>
		<TextView android:text="芒果"></TextView>
	</TableRow>
	<TableRow>
		<TextView android:text="language"></TextView>
		<TextView android:text="中文 "></TextView>
		<TextView android:text="英文"></TextView>
		<TextView android:text="日语"></TextView>
	</TableRow>
	<TableRow>
		<TextView android:text="编程语言"></TextView>
		<TextView android:text="C "></TextView>
		<TextView android:text="C++"></TextView>
		<TextView android:text="Java"></TextView>
	</TableRow>
</TableLayout>
<!--上面的表格布局可以忽略 -->
<FrameLayout android:id="@+id/frame"
			 android:layout_width="wrap_content"
			 android:layout_height="wrap_content"
			 android:layout_gravity="center"/>
</LinearLayout>

 2.java 代码:

public class MainActivity extends Activity {
	private FrameLayout frameLayout=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获取布局对象
        frameLayout=(FrameLayout) findViewById(R.id.frame);
        //使用计时器
        Timer timer=new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
			int i=0;
			@Override
			public void run() {
				i++;
				System.out.println(i);
				Message msg=new Message();
				msg.what=i;
				i%=3;
				//发送消息
				handler.sendMessage(msg);
			}
		}, 1000, 1000);
    }
    //使用Handler实现UI线程与Timer线程之间的通信
    Handler handler=new Handler(){
    	//处理消息更新UI线程
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			
			case 1:frameLayout.setForeground(getResources().getDrawable(R.drawable.f));;break;
			case 2:frameLayout.setForeground(getResources().getDrawable(R.drawable.s));break;
			case 3:frameLayout.setForeground(getResources().getDrawable(R.drawable.t));break;
			default:System.out.println("哈哈,没有图片");break;
			}
		}
    	
    };
}


3.在drawable中放了3张图片 f.png,s.png,t.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值