随着学习,经验是增加了,但是还是很缺少经验,我分享这个代码只是更大家分享小小的成果,顺便永久性保存这个代码,以后用到的时候可以拿来就用,需要改正也是很方便的,毕竟自己动脑子写的,每一步的功能都理解的很清楚。
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;
}
}
点击查看更多内容
8人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦