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

最新文章

  1. Spring实现AOP的4种方式
  2. diamond专题(一)– 简介和快速使用
  3. Android应用自动更新功能的代码实现
  4. Eclipse无法启动错误之Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini)
  5. sql 查询效率
  6. 到底是 const 还是 static readonly
  7. 转:nginx基础概念(request)
  8. julia下载QQ.jl
  9. Skipped Rebuild All: Project with VS2008
  10. media type 与 media query
  11. jQuery EasyUI 数字框(NumberBox)用法
  12. 《java入门第一季》之面向对象(多态向下转型)
  13. python:利用logbook模块管理日志
  14. Hyperic-Sigar简介——检测与监控
  15. git撤销提交(commit)
  16. 20145220韩旭飞《网络对抗》实验五:MSF基础应用
  17. Css3不透明
  18. H5页面中唤起native app
  19. Oracle 已连接到空闲例程或ORA-01034: ORACLE not available
  20. deploy a ec2 and join into domain with terraform

热门文章

  1. 软件安全测试新武器 ——浅谈基于Dynamic Taint Propagation的测试技术
  2. windows下运行swoole搭建环境
  3. [Selenium] 搭建 Android WebDriver 环境
  4. 字符串转Unicode码
  5. 漫谈WebQQ 协议
  6. Linux下安装oracle数据库提示DISPLAY not set. Please set the DISPLAY and try again。
  7. 架构:template
  8. Java中手动提交事务
  9. curl测试dns解析时间及tcp连接时间
  10. 任务47:Identity MVC:ReturnUrl实现