初学Android,图形图像之混合使用逐帧动画和补间动画(三十七)

news/2024/7/8 5:02:12 标签: android, 图形, float, layout, encoding, timer

下面例子混合使用了逐帧动画和补间动画,还有一个缺点,就是画面闪烁,一直没有找到解决办法


上面点击ImageView,上面的人物就开始走路跟移动

定义动画文件

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
  <item android:drawable="@drawable/one" android:duration="10" /> 
  <item android:drawable="@drawable/two" android:duration="10" /> 
  <item android:drawable="@drawable/three" android:duration="10" /> 
  <item android:drawable="@drawable/four" android:duration="10" /> 
  <item android:drawable="@drawable/five" android:duration="10" /> 
  <item android:drawable="@drawable/six" android:duration="10" /> 
  <item android:drawable="@drawable/seven" android:duration="10" /> 
  <item android:drawable="@drawable/eight" android:duration="10" /> 
  <item android:drawable="@drawable/nine" android:duration="10" /> 
  <item android:drawable="@drawable/ten" android:duration="10" /> 
  <item android:drawable="@drawable/eleven" android:duration="10" /> 
  <item android:drawable="@drawable/twelve" android:duration="10" /> 
  <item android:drawable="@drawable/thirteen" android:duration="10" /> 
  <item android:drawable="@drawable/fourteen" android:duration="10" /> 
  <item android:drawable="@drawable/fifteen" android:duration="10" /> 
  <item android:drawable="@drawable/sixteen" android:duration="10" /> 
  <item android:drawable="@drawable/seventeen" android:duration="10" /> 
  <item android:drawable="@drawable/eighteen" android:duration="10" /> 
  <item android:drawable="@drawable/nineteen" android:duration="10" /> 
  <item android:drawable="@drawable/twenty" android:duration="10" /> 
  <item android:drawable="@drawable/twentyone" android:duration="10" /> 
   <item android:drawable="@drawable/twentytwo" android:duration="10" /> 
  <item android:drawable="@drawable/twentythree" android:duration="10" /> 
  <item android:drawable="@drawable/twentyfour" android:duration="10" /> 
  <item android:drawable="@drawable/twentyfive" android:duration="10" /> 
  <item android:drawable="@drawable/twentysix" android:duration="10" /> 
  <item android:drawable="@drawable/twentyseven" android:duration="10" /> 
  <item android:drawable="@drawable/twentyeight" android:duration="10" /> 
  <item android:drawable="@drawable/twentynine" android:duration="10" /> 
  <item android:drawable="@drawable/thirty" android:duration="10" /> 
  <item android:drawable="@drawable/thirtyone" android:duration="10" /> 
  <item android:drawable="@drawable/thirtytwo" android:duration="10" /> 
  <item android:drawable="@drawable/thirtythree" android:duration="10" /> 
  <item android:drawable="@drawable/thirtyfour" android:duration="10" /> 
  <item android:drawable="@drawable/thirtyfive" android:duration="10" /> 
  <item android:drawable="@drawable/thirtysix" android:duration="10" /> 
</animation-list>
主界面引用动画

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="#fff">
    <ImageView android:id="@+id/walk" android:layout_width="150dp" 
        android:layout_height="150dp" 
        android:background ="@anim/walk" /> 
</LinearLayout>

主界面代码

package WangLi.Graphics.Walk;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class Walk extends Activity {
	//记录行走人ImageView当前的位置
	private float curX = 0;
	private float curY = 30;
	//记录行走人ImageView下一个位置的坐标 
	float nextX = 0;
	float nextY = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获取显示行走人的ImageView组件
        final ImageView imageView = (ImageView)findViewById(R.id.walk);
        final Handler handler = new Handler()
        {
        	public void handleMessage(Message msg)
        	{
        		if(msg.what == 0x123)
        		{
        			//横向上一直向右走
        			if(nextX > 320)
        			{
        				curX = nextX = 0;
        			}
        			else
        			{
        				nextX += 8;
        			}
        			
        			//纵向上随机上下走
        			nextY = curY + (float)(Math.random() * 10 - 5);
        			//设置显示行走人的ImageView位置发生位移改变
        			TranslateAnimation anim = new TranslateAnimation(curX,
        					nextX, 
        					curY, 
        					nextY);
        			
        			curX = nextX;
        			curY = nextY;
        			anim.setDuration(200);
        			anim.setFillEnabled(true);
        			//开始位移动画
        			imageView.startAnimation(anim);
        		}
        	}
        };
        final AnimationDrawable walk = (AnimationDrawable)imageView.getBackground();
        imageView.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        		//开始播放人行走的逐帧动画
        		walk.start();
        		//通过定制器控制每0.2秒运行一次TanslateAnimation动画
        		new Timer().schedule(new TimerTask()
        		{
        			public void run()
        			{
        				handler.sendEmptyMessage(0x123);
        			}
        		}, 0, 200);
        	}
        });
    }
}




http://www.niftyadmin.cn/n/1411358.html

相关文章

POJ 2800 Joseph’s Problem 数论找规律

Description 求 Input 两个整数n和k(1<n,k<1e9) Output 输出 Sample Input 5 3 Sample Output 7 暴力超时&#xff0c;这样就打下表找下余数的规律。输入100,27&#xff0c;一下子就可以看出来&#xff0c;倒着的看&#xff0c;是一段一段的等差序列。 例如100 25 除数 …

初学Android,图形图像之自定义补间动画(三十八)

Android提供Animation作为补间动画抽象基类&#xff0c;而且为该抽象基类提供了 AlphaAnimation,RotateAnimation,ScaleAnimation,TranslateAnimation四个类 但是在实际项目中&#xff0c;这些很可能不够用&#xff0c;可能需要一些更复杂的动画&#xff0c;比如说立体空间的旋…

CentOS6.6系统安装

一步步教你安装CentOS 6.6系统说明&#xff1a;截止目前CentOS 6.x最新版本为CentOS 6.6&#xff0c;下面介绍CentOS 6.6的具体安装配置过程服务器相关设置如下&#xff1a;操作系统&#xff1a;CentOS 6.6 64位IP地址&#xff1a;192.168.21.129网关&#xff1a;192.168.21.2D…

react-router-dom示例讲解(七),路径递归

在react-router-dom的官方示例中&#xff0c;路径递归不是最难的&#xff0c;但是绝对是一个比较难理解的示例demo。 本示例的效果图如下&#xff1a; 相关示例核心代码&#xff1a; import React, {Component} from react; import {BrowserRouter as Router,Route,Link,S…

初学Android,图形图像之使用SurfaceView(三十九)

SurfaceView支持双缓冲&#xff0c;在实现游戏绘图上面比View更出色 它有几个重要方法 Canvas lockCanvas(): 锁定整个SurfaceView对象,获取该Surface上的Canvas Canvas lockCanvas(Rect dirty): 锁定SurfaceView上Rect划分的区域&#xff0c;获取该Surface上的Canvas unlock…

面向万物互联的时序数据库HiTSDB

现在填写调查问卷&#xff0c;将优先获得公测资格 当前物联网的浪潮席卷全球&#xff0c;甚至于人们还没有真正意识到物联网的存在&#xff0c;但它已经无处不在 。个人智能手环&#xff0c;家庭里使用的智能空调&#xff0c;空气净化器&#xff0c;电饭煲&#xff0c;到社会化…

初学Android,图形图像之使用SurfaceView(四十)

SurfaceView跟View最大的不同就是&#xff0c;它可以局部更新Canvas&#xff0c;而不用像View一样&#xff0c;重绘整个画面 并且View的绘图必须在当前的UI线程中进行,当需要花较长时间更新绘图时&#xff0c;主UI线程就会阻塞&#xff0c;无法响应用户操作&#xff0c;而Surfa…

react-router-dom示例讲解(8)侧边栏

在react-router-dom的官方示例中&#xff0c;侧边栏是非常容易实现的一个示例&#xff0c;奇实现的核心就是展示两组Routes。 本示例的效果图如下&#xff1a; 相关的核心代码&#xff1a; import React, {Component} from react; import {BrowserRouter as Router,Route,…