动态移动ImageView的位置

Marco_lhc 2014-09-06 02:51:25
我有个一个ImageView不断播放动画,现在能实现用TranslationAnimation实现ImageView的移动效果。但是拖完以后ImageView又回到原来的位置。我希望我鼠标拖动到哪ImageView就在哪个位置。这该如何实现。试了setPadding,setMargins都不奏效。
package com.example.animationdrawable;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.TranslateAnimation;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
private ImageView imgView;
private float curX=0;
private float curY=0;
float nextX=0;
float nextY=0;
int width;
int height;//屏幕的高度
boolean isstart=false;
RelativeLayout.LayoutParams params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width=dm.widthPixels;
height=dm.heightPixels;
imgView=(ImageView)findViewById(R.id.anim);

curX=(width-60)/2;
curY=height-60*2;


final AnimationDrawable animation=(AnimationDrawable)imgView.getBackground();
final Handler handler =new Handler(){
public void handleMessage(Message msg){

if(msg.what==0x123){
float moveX=nextX-curX;
float moveY=nextY-curY;


TranslateAnimation anim=new TranslateAnimation(curX,nextX-imgView.getWidth()/2,curY,nextY-imgView.getHeight()/2);
curX=nextX;
curY=nextY;
anim.setDuration(200);
imgView.startAnimation(anim);

}
if(msg.what==0x111){
params=(RelativeLayout.LayoutParams)imgView.getLayoutParams();
params.setMargins(Math.round(nextX), Math.round(nextY)
, Math.round(width-60-nextX), Math.round(height-60-nextY));
}
}
};

imgView.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(!isstart)
animation.start();
switch(event.getActionMasked()){
case MotionEvent.ACTION_MOVE:
nextX=event.getX();//获得当前的X坐标
nextY=event.getY();//获得当前的Y坐标
handler.sendEmptyMessageDelayed(0x123, 200);
break;
case MotionEvent.ACTION_UP:
handler.sendEmptyMessage(0x111);
break;
}
return true;
}
});

}

}
...全文
366 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
魅离儿 2014-09-09
  • 打赏
  • 举报
回复
然后paint,canvas网上画。我最近也刚学
魅离儿 2014-09-09
  • 打赏
  • 举报
回复
试试帧布局。
Marco_lhc 2014-09-09
  • 打赏
  • 举报
回复
引用 3 楼 u010911576 的回复:
animation.setFillAfter();
试过了,还是不行。不是我要的效果
_deadline 2014-09-08
  • 打赏
  • 举报
回复
animation.setFillAfter();
Marco_lhc 2014-09-07
  • 打赏
  • 举报
回复
哪位大神回答一下
Marco_lhc 2014-09-07
  • 打赏
  • 举报
回复
给分太低了么 T T

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧