Java 如何使cardviews的移动更平滑?
我使用帧布局来保存我的每个CardView,并从中启动每个运动动画(从左到右)。但是我注意到这个动作看起来并不是非常平稳 下面是我的代码,您建议怎么做,以提高卡移动的整体平滑度。谢谢 MyFrameLayout:Java 如何使cardviews的移动更平滑?,java,android,xml,android-layout,android-activity,Java,Android,Xml,Android Layout,Android Activity,我使用帧布局来保存我的每个CardView,并从中启动每个运动动画(从左到右)。但是我注意到这个动作看起来并不是非常平稳 下面是我的代码,您建议怎么做,以提高卡移动的整体平滑度。谢谢 MyFrameLayout: public class MyFrameLayout extends FrameLayout { private static int mWidth = 500; MyFrameLayout touchFrameLayout; public MyFrameL
public class MyFrameLayout extends FrameLayout {
private static int mWidth = 500;
MyFrameLayout touchFrameLayout;
public MyFrameLayout(Context context) {
super(context);
initialize(context);
}
public MyFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public MyFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
private void initialize(Context context) {
setOnTouchListener(mTouchListener);
touchFrameLayout = this;
}
private float mDisplacementX;
// private float mLastMoveX;
private float mDisplacementY;
private float mInitialTx;
private boolean mTracking;
private OnTouchListener mTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mWidth = (int) touchFrameLayout.getLayoutParams().width;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mDisplacementX = event.getRawX();
mDisplacementY = event.getRawY();
mInitialTx = getTranslationX();
return true;
case MotionEvent.ACTION_MOVE:
// get the delta distance in X and Y direction
float deltaX = event.getRawX() - mDisplacementX;
float deltaY = event.getRawY() - mDisplacementY;
// updatePressedState(false);
// set the touch and cancel event
if ((Math.abs(deltaX) > ViewConfiguration.get(getContext())
.getScaledTouchSlop() * 2 && Math.abs(deltaY) < Math
.abs(deltaX) / 2)
|| mTracking) {
mTracking = true;
if (getTranslationX() <= mWidth / 2
&& getTranslationX() >= -(mWidth / 2)) {
setTranslationX(mInitialTx + deltaX);
return true;
}
}
break;
case MotionEvent.ACTION_UP:
if (mTracking) {
mTracking = false;
float currentTranslateX = getTranslationX();
if (currentTranslateX > (mWidth/10)) {
rightSwipe();
} else if (currentTranslateX < -(mWidth*10)) {
leftSwipe();
}
// comment this line if you don't want your frame layout to
// take its original position after releasing the touch
setTranslationX(0);
return true;
} else {
// handle click event
setTranslationX(0);
}
break;
}
return false;
}
};
private void deleteCard() {
...
}
private void rightSwipe() {
Toast.makeText(this.getContext(), "Swipe to the right",
Toast.LENGTH_SHORT).show();
DeleteCard();
}
private void leftSwipe() {
Toast.makeText(this.getContext(), "Swipe to the left",
Toast.LENGTH_SHORT).show();
DeleteCard();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.testing.android.layout.MyFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/taskViewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
card_view:cardCornerRadius="8dp">
......
</android.support.v7.widget.CardView>
</com.example.testing.android.layout.MyFrameLayout>
</RelativeLayout>
公共类MyFrameLayout扩展了FrameLayout{
专用静态int mWidth=500;
MyFrameLayout触摸FrameLayout;
公共MyFrameLayout(上下文){
超级(上下文);
初始化(上下文);
}
公共MyFrameLayout(上下文、属性集属性){
超级(上下文,attrs);
初始化(上下文);
}
公共MyFrameLayout(上下文上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
初始化(上下文);
}
私有void初始化(上下文){
setOnTouchListener(mTouchListener);
touchFrameLayout=this;
}
私有浮动mDisplacementX;
//私有浮动mLastMoveX;
私人浮动MDisplacement;
私人浮动迷你型;
私有布尔跟踪;
私有OnTouchListener mTouchListener=新OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
mWidth=(int)touchFrameLayout.getLayoutParams().width;
开关(event.getActionMasked()){
case MotionEvent.ACTION\u DOWN:
mDisplacementX=event.getRawX();
mDisplacementY=event.getRawY();
mInitialTx=getTranslationX();
返回true;
case MotionEvent.ACTION\u移动:
//获取X和Y方向上的增量距离
float deltaX=event.getRawX()-mDisplacementX;
float deltaY=event.getRawY()-mDisplacementY;
//updatePressedState(false);
//设置触摸和取消事件
if((Math.abs(deltaX)>ViewConfiguration.get(getContext())
.getScaledTouchSlop()*2和&Math.abs(deltaY)
Xml:
public class MyFrameLayout extends FrameLayout {
private static int mWidth = 500;
MyFrameLayout touchFrameLayout;
public MyFrameLayout(Context context) {
super(context);
initialize(context);
}
public MyFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public MyFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
private void initialize(Context context) {
setOnTouchListener(mTouchListener);
touchFrameLayout = this;
}
private float mDisplacementX;
// private float mLastMoveX;
private float mDisplacementY;
private float mInitialTx;
private boolean mTracking;
private OnTouchListener mTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mWidth = (int) touchFrameLayout.getLayoutParams().width;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mDisplacementX = event.getRawX();
mDisplacementY = event.getRawY();
mInitialTx = getTranslationX();
return true;
case MotionEvent.ACTION_MOVE:
// get the delta distance in X and Y direction
float deltaX = event.getRawX() - mDisplacementX;
float deltaY = event.getRawY() - mDisplacementY;
// updatePressedState(false);
// set the touch and cancel event
if ((Math.abs(deltaX) > ViewConfiguration.get(getContext())
.getScaledTouchSlop() * 2 && Math.abs(deltaY) < Math
.abs(deltaX) / 2)
|| mTracking) {
mTracking = true;
if (getTranslationX() <= mWidth / 2
&& getTranslationX() >= -(mWidth / 2)) {
setTranslationX(mInitialTx + deltaX);
return true;
}
}
break;
case MotionEvent.ACTION_UP:
if (mTracking) {
mTracking = false;
float currentTranslateX = getTranslationX();
if (currentTranslateX > (mWidth/10)) {
rightSwipe();
} else if (currentTranslateX < -(mWidth*10)) {
leftSwipe();
}
// comment this line if you don't want your frame layout to
// take its original position after releasing the touch
setTranslationX(0);
return true;
} else {
// handle click event
setTranslationX(0);
}
break;
}
return false;
}
};
private void deleteCard() {
...
}
private void rightSwipe() {
Toast.makeText(this.getContext(), "Swipe to the right",
Toast.LENGTH_SHORT).show();
DeleteCard();
}
private void leftSwipe() {
Toast.makeText(this.getContext(), "Swipe to the left",
Toast.LENGTH_SHORT).show();
DeleteCard();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.testing.android.layout.MyFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/taskViewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
card_view:cardCornerRadius="8dp">
......
</android.support.v7.widget.CardView>
</com.example.testing.android.layout.MyFrameLayout>
</RelativeLayout>
......
为了在框架布局中更平滑地移动CardView,您将使用设置视图或其属性的动画。实现这一点的方法有很多,但最简单的方法是选择最简单的方法。它当然适合你的需要,因为你提到你感兴趣的运动是从左到右的运动。您可以设置cardwiew
s的动画,使其更平滑地移动到y轴
仔细阅读文档,以实现您的愿望
编辑:可能对您有用。看看视频,看看是关于什么的。简而言之,在视图组中添加/删除视图时,可以为发生的变换/尺寸比例设置动画