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动画之通过子线程来实现动画的更多相关文章

  1. android 主线程和子线程之间的消息传递

    从主线程发送消息到子线程(准确地说应该是非UI线程)  package com.zhuozhuo; import android.app.Activity; import android.os.Bun ...

  2. Android -- ViewRoot,关于子线程刷新UI

    Android在4.0之后执行线程更新UI操作会报异常:CalledFromWrongThreadException:Only the original thread that created a v ...

  3. Android利用Looper在子线程中改变UI

    MainActivity如下: package cn.testlooper; import android.app.Activity; import android.os.Bundle; import ...

  4. 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报 ...

  5. Android 主线程和子线程通信问题

        Android 如今不支持View在子线程中创建及调用其方法.假设要实现子线程内容更新之后.将结果及时反馈到主线程中,该怎样出来呢?     能够在主线程中创建Handler来实现. 这样子线 ...

  6. Android开发之在子线程中使用Toast

    在子线程中使用Toast的时候,出现Force close. 错误提示:Can't create handler inside thread that has not called Looper.pr ...

  7. Android中不能在子线程中更新View视图的原因

    这是一条规律,很多coder知道,但原因是什么呢? 如下: When a process is created for your application, its main thread is ded ...

  8. Android子线程更新UI的方法总结

    版权声明:本文为博主原创文章,转载请注明出处:https://i.cnblogs.com/EditPosts.aspx?postid=6121280 消息机制,对于Android开发者来说,应该是非常 ...

  9. Android在子线程中更新UI(二)

    MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...

随机推荐

  1. 启动vmware中的虚拟机的时候,提示Failed to lock the file

    http://www.vixual.net/blog/archives/842 VMware Server 當掉後重新啟動 Guest OS 時,出現 cannot open the disk '*. ...

  2. 通过xmanager连接Linux图形界面

    今天要在linux下安装数据库,用的是xmanager:之前自己也用过该工具在OUI下做过安装,还很顺利. 但是,今天连接后,运行命令,等了有5分钟,没有出现OUI界面. linux版本: [root ...

  3. java中wait和notify

    在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的进程同步是通过synchronized()来实现的,需要说明的是,JAVA的synchronized()方法类似于操作系统概念中的 ...

  4. apache 安装及配置

    近期想用apache运行网站,在网上查询windows 版本的中文说明文档有特别少,所以将学习到的在这里做个笔记,以便日后学习以及大家相互交流. 相关文档:http://httpd.apache.or ...

  5. bzoj3998

    后缀自动机+dp 想了挺长时间 后缀自动机的状态图是一个dag,从root走到一个点的路径数代表了这个状态包含的子串,我们先预处理出来每个节点向后走能够形成多少子串,注意这里不是直接在parent树上 ...

  6. 语言学习系列-Scala连接数据库示例

    Scala语法 预装数据库Mysql,登录用户名密码为:root:root,建立数据库test1,建立数据表emp: package com.ccb.day1   import java.sql.Dr ...

  7. UltraISO制作U盘启动盘-centos7

    1.下载.安装UltraISO软件,(网上找到下载) 2.安装好以后,打开软件,显示如下界面: 3.选择镜像 点击菜单栏的“文件”选项,再点击“打开”按钮,选择要刻录的系统镜像: 4.点击菜单栏的“启 ...

  8. 安装Nginx作为文件服务器

    我是在Windows上安装的,在Linux上也一样 #Windows server2008 R2 #Nginx1.12 Nginx安装包下载地址:http://nginx.org/en/downloa ...

  9. Android控件大全(四)——CoordinatorLayout

    CoordinatorLayout 其实就是个高级的FrameLayout,用于协调子布局要使用该控件,需要再gradle中加入: compile 'com.android.support:desig ...

  10. (链接)Tomcat设置Session的失效方式

    tomcat 设置session过期时间(四种方式):https://blog.csdn.net/liuxiao723846/article/details/50055075 tomcat里设置ses ...