天天看點

android動畫來回運動,ImageView自動來回循環移動和随手移動的結合,用到Animation...

随着學習,經驗是增加了,但是還是很缺少經驗,我分享這個代碼隻是更大家分享小小的成果,順便永久性儲存這個代碼,以後用到的時候可以拿來就用,需要改正也是很友善的,畢竟自己動腦子寫的,每一步的功能都了解的很清楚。

package com.example.coin;

import android.os.Bundle;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.util.DisplayMetrics;

import android.view.Menu;

import android.view.MotionEvent;

import android.view.animation.AnimationSet;

import android.view.animation.TranslateAnimation;

import android.widget.AbsoluteLayout;

import android.widget.ImageView;

@SuppressLint("NewApi") public class MainActivity extends Activity {

private ImageView imgv_Touch; //手動

private ImageView imgv_AutoMove; //自動

private int ScreenX;

private int ScreenY;

private int imgWidth;

private int imgHeight;

private float m;

private float d;

private float x;

private float y;

private float mX;

private float mY;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imgv_Touch=(ImageView) findViewById(R.id.imageView1);

imgv_AutoMove=(ImageView) findViewById(R.id.imageView2);

DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

ScreenX = dm.widthPixels;

ScreenY = dm.heightPixels;

imgWidth = 100;

imgHeight =100;

Animation();

}

@SuppressWarnings("deprecation")

@Override

public boolean onTouchEvent(MotionEvent event) {

// TODO Auto-generated method stub

m=event.getX();

d=event.getY();

x = event.getX()-45;

y = event.getY()-160;

mX=x-(imgWidth/2);

mY=y-(imgHeight/2);

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN :

imgv_Touch.setLayoutParams(new

AbsoluteLayout.LayoutParams

(imgWidth, imgHeight, (int)mX,(int)mY));

break;

case MotionEvent.ACTION_MOVE :

imgv_Touch.setLayoutParams(new

AbsoluteLayout.LayoutParams

(imgWidth, imgHeight, (int)mX,(int)mY));

break;

case MotionEvent.ACTION_UP :

imgv_Touch.setLayoutParams(new

AbsoluteLayout.LayoutParams

(imgWidth, imgHeight, (int)mX,(int)mY));

break;

default:

break;

}

return true;

}

private void Animation() {

// TODO Auto-generated method stub

// TODO Auto-generated method stub

AnimationSet animationSet = new AnimationSet(true);

TranslateAnimation translateAnimation = new TranslateAnimation(

// X軸的開始位置

android.view.animation.Animation.RELATIVE_TO_SELF, -1f,

// X軸的結束位置

android.view.animation.Animation.RELATIVE_TO_SELF, ScreenX/80,

// Y軸的開始位置

android.view.animation.Animation.RELATIVE_TO_SELF, 0f,

// Y軸的結束位置

android.view.animation.Animation.RELATIVE_TO_SELF, 0f);

translateAnimation.setDuration(1000);

translateAnimation.setRepeatCount(-1); // 設定動畫重複次數 -1表示死循環

translateAnimation.setRepeatMode(android.view.animation.Animation.REVERSE);

//translateAnimation.setRepeatMode(Animation.RESTART); //重新從頭執行

//translateAnimation.setRepeatMode(Animation.REVERSE); //反方向執行

animationSet.addAnimation(translateAnimation);

imgv_AutoMove.setAnimation(animationSet);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}