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