实现一个能动的人是我对模仿《奇怪的大冒险》第一阶段的最后一步,这一段时间学会了许多的东西,见到了很多大坑,也顺利脱险了。而本文所说的|能动的人是基于ImageView打造的。在ImageView中我又给它添加了更多的属性来实现我想要的功能。
1>在实现过程中小人走动的实现是依靠AnimationDramble加载两张运动的图片来实现的。
2>实现过程中我写了一些私有变量,并且暴露部分接口来给外部控制。
3>其中的向左向右运动(X坐标移动)是以定时器Timer重新定义和清除实现的,由于Timer没有暂停,所以在每一次点击后我就重新定义一次全局变量Timer和Hander,从而改变了主线程UI的变化。
4>左右按键并没有监听它们的OnClickListener而是监听了它们的OnTouchListener。如果是按下就开启自定义view的Timer和运动动画,否则就关闭Timer和动画。
5>针对每次的运动距离我提供了一个ValueAnimator来实现平滑过渡(实际上效果并不好)
总而言之基本实现了它的一些要求。
具体代码如下:
1、游戏组主界面的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_game_0_0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhiteBackground"
tools:context="com.example.app_damaoxian.Activity_game_0_0">
<ImageView
android:id="@+id/game0_0back"
android:scaleType="centerInside"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="true"
android:background="@drawable/background_back"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<com.example.app_damaoxian.heroview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/heroview" />
<ImageView
android:clickable="true"
android:id="@+id/herogoleft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_goleft"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<ImageView
android:clickable="true"
android:id="@+id/iv_jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_jump"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
<ImageView
android:clickable="true"
android:id="@+id/herogoright"
android:background="@drawable/background_goright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/herogoleft" />
</RelativeLayout>
2、游戏主界面Activity的java代码:
package com.example.app_damaoxian;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class Activity_game_0_0 extends AppCompatActivity {
private RelativeLayout relativeLayout;
private ImageView herogoleft,herogoright,game0_0back,iv_jump;
private boolean goRight;
private heroview heroView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_0_0);
relativeLayout=(RelativeLayout)findViewById(R.id.activity_game_0_0);
relativeLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
initView();
}
private void initView(){
herogoleft=(ImageView)findViewById(R.id.herogoleft);
herogoright=(ImageView)findViewById(R.id.herogoright);
game0_0back=(ImageView)findViewById(R.id.game0_0back);
heroView=(heroview)findViewById(R.id.heroview);
iv_jump=(ImageView)findViewById(R.id.iv_jump);
game0_0back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
overridePendingTransition(R.animator.fragment_in,R.animator.fragment_out);
}
});
herogoright.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN){
herogoright.setImageDrawable(getResources().getDrawable(R.drawable.thumb_dpad_right_pressed));
heroView.startGoRight();
goRight=true;
}else if (event.getAction()==MotionEvent.ACTION_UP){
herogoright.setImageDrawable(getResources().getDrawable(R.drawable.thumb_dpad_right));
heroView.stopGoRight();
}
return true;
}
});
herogoleft.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN){
herogoleft.setImageDrawable(getResources().getDrawable(R.drawable.thumb_dpad_left_pressed));
heroView.startGoLeft();
goRight=false;
}else if (event.getAction()==MotionEvent.ACTION_UP){
herogoleft.setImageDrawable(getResources().getDrawable(R.drawable.thumb_dpad_left));
heroView.stopGoLeft();
}
return true;
}
});
iv_jump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (goRight){
heroView.startRight_updown();
}else {
heroView.startLeft_updown();
}
}
});
}
@Override
protected void onDestroy() {
heroView.clearAndcancelTimer();
super.onDestroy();
}
}
3、自定义view的Java代码,extends ImagView:
package com.example.app_damaoxian;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by 尽途 on 2017/4/22.
*/
public class heroview extends ImageView {
private AnimationDrawable herogorightList,herogoleftList;
private float heroWidth,heroHeight,heroX,heroY,currentX;
private Timer goRightTimer,goLeftTimer;
private TimerTask goRightTimerTask,goLeftTimerTask;
private Handler handler;
public heroview(Context context){
super(context);
initData();
addDrawable();
}
public heroview(Context context, AttributeSet attributeSet){
super(context,attributeSet);
initData();
addDrawable();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//setMeasuredDimension();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private void addDrawable(){
setImageDrawable(getResources().getDrawable(R.drawable.stop));
herogorightList=new AnimationDrawable();
herogoleftList=new AnimationDrawable();
herogorightList.addFrame(getResources().getDrawable(R.drawable.go_right),200);
herogorightList.addFrame(getResources().getDrawable(R.drawable.go_right1),200);
herogoleftList.addFrame(getResources().getDrawable(R.drawable.go_left),200);
herogoleftList.addFrame(getResources().getDrawable(R.drawable.go_left1),200);
herogorightList.setOneShot(false);
herogoleftList.setOneShot(false);
}
private void initData(){
heroWidth=getWidth();
heroHeight=getHeight();
heroX=getLeft();
heroY=getTop();
}
private void newgoRightTimer(){
goRightTimer=new Timer();
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 0:
heroX=heroX+50f;
break;
case 1:
heroX=heroX-50f;
break;
default:
break;
}
ValueAnimator progressAnimatior=ValueAnimator.ofFloat(heroX-50f,heroX);
setX(currentX);
progressAnimatior.setDuration(200);
progressAnimatior.setTarget(currentX);
progressAnimatior.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
currentX=(float)animation.getAnimatedValue();
invalidate();
}
});
progressAnimatior.start();
setX(heroX);
super.handleMessage(msg);
}
};
goRightTimerTask=new TimerTask() {
@Override
public void run() {
Message message=new Message();
message.what=0;
handler.sendMessage(message);
}
};
}
private void newgoLeftTimer(){
goLeftTimer=new Timer();
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 0:
heroX=heroX+50f;
break;
case 1:
heroX=heroX-50f;
break;
default:
break;
}
ValueAnimator progressAnimatior=ValueAnimator.ofFloat(heroX+50f,heroX);
setX(currentX);
progressAnimatior.setDuration(200);
progressAnimatior.setTarget(currentX);
progressAnimatior.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
currentX=(float)animation.getAnimatedValue();
invalidate();
}
});
progressAnimatior.start();
setX(heroX);
super.handleMessage(msg);
}
};
goLeftTimerTask=new TimerTask() {
@Override
public void run() {
Message message=new Message();
message.what=1;
handler.sendMessage(message);
}
};
}
/**
* 为外部提供了控制hero动作形态的内部接口
*/
public void startGoRight(){
setImageDrawable(herogorightList);
herogorightList.start();
newgoRightTimer();
goRightTimer.schedule(goRightTimerTask,0,200);
}
public void stopGoRight(){
herogorightList.stop();
clearAnimation();
setImageDrawable(getResources().getDrawable(R.drawable.stop));
goRightTimer.cancel();
}
public void startGoLeft(){
setImageDrawable(herogoleftList);
herogoleftList.start();
newgoLeftTimer();
goLeftTimer.schedule(goLeftTimerTask,0,200);
}
public void stopGoLeft(){
herogoleftList.stop();
clearAnimation();
setImageDrawable(getResources().getDrawable(R.drawable.stop));
goLeftTimer.cancel();
}
public void heroCry(){
setImageDrawable(getResources().getDrawable(R.drawable.cry));
}
public void startLeft_updown(){
if (herogoleftList.isRunning()){
herogoleftList.stop();
}
setImageDrawable(getResources().getDrawable(R.drawable.left_updown));
}
public void startRight_updown(){
if (herogorightList.isRunning()){
herogorightList.stop();
}
setImageDrawable(getResources().getDrawable(R.drawable.right_updown));
}
public void clearAndcancelTimer(){
if (goLeftTimer!=null){
goLeftTimer.cancel();
goLeftTimer=null;
}
if (goRightTimer!=null){
goRightTimer.cancel();
goRightTimer=null;
}
}
}
本例已上传至Github 欢迎Star https://github.com/zqljintu/qiguaidedamaoxian 点击打开链接