android动画之通过子线程来实现动画
android动画之通过子线程来实现动画
使用android动画机制,往往是相对于原始位置来进行参照。
这里通过子线程修改物体位置实现动画。
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <TextView
android:id="@+id/show"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/button1"
android:onClick="MyCLick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="行动" /> <Button
android:id="@+id/button2"
android:onClick="MyCLick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="28dp"
android:layout_toLeftOf="@+id/button1"
android:text="获取位置" /> </RelativeLayout>
动画代码:
public class MainActivity extends Activity {
	TextView textView;
	MyRuns myRuns;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView = (TextView) findViewById(R.id.show);
		myRuns=new MyRuns(new MyHead(textView, 4, 8), true);//位移动画
	}
	static class MyHead extends Handler {// 坐标动画
		View view;// 操作元素
		float cx;
		float cy;
		public MyHead(View view, float cx, float cy) {
			super();
			this.view = view;
			this.cx = cx;
			this.cy = cy;
		}
		@Override
		public void handleMessage(Message msg) {
			// 更新ui
			view.setX(view.getX() + cx);
			view.setY(view.getY() + cy);
			super.handleMessage(msg);
		}
	}
	// 子线程更新位置
	class MyRuns implements Runnable {//更新UI界面
		MyHead head;
		boolean isFire = false;
		public MyRuns(MyHead head, boolean isFire) {
			super();
			this.head = head;
			this.isFire = isFire;
		}
		public boolean isFire() {
			return isFire;
		}
		public void setFire(boolean isFire) {
			this.isFire = isFire;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				while (true) {
					if (!isFire) {
						break;//停止动画
					}
					Thread.sleep(80);
					Message message = new Message();
					message.what = 3;
					message.obj = "";
					head.sendMessage(message);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
	//开始运动
	void StartThreed(MyRuns myRuns){
		myRuns.setFire(true);//开启
		new Thread(myRuns).start();
	}
	public void MyCLick(View view) {
		if (view.getId() == R.id.button1) {
			StartThreed(myRuns);
		} else if (view.getId() == R.id.button2) {
			myRuns.setFire(false);//结束子线程
			Toast.makeText(getApplicationContext(),
					"坐标" + textView.getX() + "||" + textView.getY(),
					Toast.LENGTH_SHORT).show();
		}
	}
}
原文地址:http://sijienet.com/bbs/?leibie=showinfo&id=57
android动画之通过子线程来实现动画的更多相关文章
- android 主线程和子线程之间的消息传递
		
从主线程发送消息到子线程(准确地说应该是非UI线程) package com.zhuozhuo; import android.app.Activity; import android.os.Bun ...
 - Android -- ViewRoot,关于子线程刷新UI
		
Android在4.0之后执行线程更新UI操作会报异常:CalledFromWrongThreadException:Only the original thread that created a v ...
 - Android利用Looper在子线程中改变UI
		
MainActivity如下: package cn.testlooper; import android.app.Activity; import android.os.Bundle; import ...
 - Android进阶(十六)子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误
		
原子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误 今天用子线程调Toast报 ...
 - Android 主线程和子线程通信问题
		
Android 如今不支持View在子线程中创建及调用其方法.假设要实现子线程内容更新之后.将结果及时反馈到主线程中,该怎样出来呢? 能够在主线程中创建Handler来实现. 这样子线 ...
 - Android开发之在子线程中使用Toast
		
在子线程中使用Toast的时候,出现Force close. 错误提示:Can't create handler inside thread that has not called Looper.pr ...
 - Android中不能在子线程中更新View视图的原因
		
这是一条规律,很多coder知道,但原因是什么呢? 如下: When a process is created for your application, its main thread is ded ...
 - Android子线程更新UI的方法总结
		
版权声明:本文为博主原创文章,转载请注明出处:https://i.cnblogs.com/EditPosts.aspx?postid=6121280 消息机制,对于Android开发者来说,应该是非常 ...
 - Android在子线程中更新UI(二)
		
MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...
 
随机推荐
- POJ2135 Farm Tour —— 最小费用最大流
			
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
 - linux初级学习笔记一:linux操作系统及常用命令,及如何获取命令的使用帮助!(视频序号:02_1,2)
			
本节学习的命令:ls,cd,type,pwd, printenv, hash, date, clock, man, hwclock, info, cal, echo, printf, file! 本节 ...
 - html5--6-52 动画效果-过渡
			
html5--6-52 动画效果-过渡 实例 @charset="UTF-8"; div{ width: 300px; height: 150px; margin: 30px; f ...
 - 转:Apache-Tomcat各发布包说明
			
下载地址:http://tomcat.apache.org/发布包说明: apache-tomcat-[version].zip---------------------------基本发布包.这些发 ...
 - jstl标签: c:Foreach详解
			
为循环控制,它可以将集合(Collection)中的成员循序浏览一遍.运作方式为当条件符合时,就会持续重复执行的本体内容. 为循环控制,它可以将集合(Collection)中的成员循序浏览一遍.运作方 ...
 - skynet源码阅读<7>--死循环检测
			
在使用skynet开发时,你也许会碰到类似这样的警告:A message from [ :0100000f ] to [ :0100000a ] maybe in an endless loop (v ...
 - LA-4726 (斜率优化+单调队列)
			
题意: 给定一个01序列,选一个长度至少为L 的连续子序列使其平均值最大;输出这个子序列的起点和终点;如果有多个答案,输出长度最小的,还有多个就输出第一个编号最小的; 思路: 用sum[i]表示[1, ...
 - BZOJ1453: [WC2005]Dface双面棋盘
			
离线LCT维护MST,和3082的方法一样.然而比较码农,适合颓废的时候写. PS:线段树分治要好写得多,LCT比较自娱自乐. #include<bits/stdc++.h> using ...
 - CreateRemoteThread注入DLL
			
DLL注入的常用方式之一远程线程注入,实现代码如下 // CreateRemoteThread.cpp : Defines the entry point for the application.// ...
 - 多线程-threading模块3
			
超级播放器 #coding:utf-8 import threading from time import sleep,ctime #超级播放器 def super_player(file,time) ...