Android用属性动画拖动view到任意位置
2016-08-23 11:00
393 查看
package me.waye;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MoveImgActivity extends Activity {
ImageView iv;
private int containerWidth;
private int containerHeight;
float lastX, lastY;
RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_move_img);
rl = (RelativeLayout) findViewById(R.id.rl);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
lastX = event.getRawX();
lastY = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
// 不要直接用getX和getY,这两个获取的数据已经是经过处理的,容易出现图片抖动的情况
float distanceX = lastX - event.getRawX();
float distanceY = lastY - event.getRawY();
float nextY = iv.getY() - distanceY;
float nextX = iv.getX() - distanceX;
// 不能移出屏幕
if (nextY < 0) {
nextY = 0;
} else if (nextY > containerHeight - iv.getHeight()) {
nextY = containerHeight - iv.getHeight();
}
if (nextX < 0)
nextX = 0;
else if (nextX > containerWidth - iv.getWidth())
nextX = containerWidth - iv.getWidth();
// 属性动画移动
ObjectAnimator y = ObjectAnimator.ofFloat(iv, "y", iv.getY(), nextY);
ObjectAnimator x = ObjectAnimator.ofFloat(iv, "x", iv.getX(), nextX);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(x, y);
animatorSet.setDuration(0);
animatorSet.start();
lastX = event.getRawX();
lastY = event.getRawY();
}
return false;
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// 这里来获取容器的宽和高
if (hasFocus) {
containerHeight = rl.getHeight();
containerWidth = rl.getWidth();
}
}
}
activity_move_img.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/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.interactivechart.MoveImgActivity">
<ImageView
android:id="@+id/iv"
android:src="@drawable/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
相关文章推荐
- Android下拖动任意View代码(使用属性动画,眼前一亮)
- android任意view移动缩放至消失到任何位置的通用动画
- Android View拖动至任意位置(ImageView,VideoView等)
- Android移动view动画问题 关于view的位置移动了,但view里面绑定的监听器还在原位
- Android使用属性动画ValueAnimator动态改变SurfaceView的背景颜色
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- Android动画精讲一:从setTranslationX谈属性动画和view动画的区别
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- 在Android中动画移动一个View的位置,采用Scroller类实现
- Android为ViewPager增加切换动画——使用属性动画
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- Android 用属性动画自定义view的渐变背景
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- 采用Spanned在android View任意位置加图片效果
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- Android Listview切换动画,扩展到任意view切换之间动画实现
- Android 获取View的位置属性
- Android设置SurfaceView任意大小和任意位置
- Android下图片或按钮等可拖动到任意位置的效果实现源码
- Android至ViewPager添加切换动画——使用属性动画