Java 如何在Android中从左到右平滑滑动ImageView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31562833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to slide an ImageView from left to right smoothly in Android?
提问by LS_
I need to make an ImageView
slide from left to right of the screen with a smooth animation (I want the ImageView
to be visible during the transition) I tried with the following code:
我需要ImageView
用平滑的动画从屏幕的左到右制作一张幻灯片(我希望ImageView
在转换过程中可见)我尝试使用以下代码:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
camion.animate()
.translationX(width)
.setDuration(2000)
.setInterpolator(new LinearInterpolator())
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
//camion.setVisibility(View.GONE);
}
});
The ImageView
moves but the animation is laggy and not smooth like I want to.
What am I doing wrong on the code?
在ImageView
移动,但在动画laggy,而不是光滑如我想。我在代码上做错了什么?
采纳答案by Aritra Roy
Creating this kind of tween animation is simple. Just follow the steps,
创建这种补间动画很简单。跟着步骤,
Step 1
第1步
Create a directory anim
inside the res
directory and put this as slide.xml
创建一个目录anim
里面res
的目录,并把此作为slide.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="800" />
</set>
You can obviously customize the animation by changing the two attributes fromXDelta
and toXDelta
. The %prefers to with respect to the parentwhich simply means that it will move the image 75% with respect to the parent.
您可以通过改变两个属性明显自定义动画fromXDelta
和toXDelta
。的%P是指具有相对于母它只是意味着它将相对于父移动图像的75%。
Step 2
第2步
// Refer the ImageView like this
imageView = (ImageView) findViewById(R.id.img);
// Load the animation like this
animSlide = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide);
// Start the animation like this
imageView.startAnimation(animSlide);
You can also setInterpolator()
and setListeners()
if you want to. I haven't shown them here to keep it simple. If you need, just let me know.
您还可以setInterpolator()
与setListeners()
如果你想。为了简单起见,我没有在这里展示它们。如果您需要,请告诉我。
NOTE
笔记
As you have mentioned repeatedly, that you are experiencing a laggy animation. I have tested this animation on 3 real devices and 2 emulators and the animation was buttery smoothon all of them. Tested on low end devices like Moto E to high end devices like Nexus 5 and Galaxy S6.
正如您反复提到的,您正在体验延迟动画。我已经在3 个真实设备和 2 个模拟器上测试了这个动画,动画在所有设备上都非常流畅。在 Moto E 等低端设备到 Nexus 5 和 Galaxy S6 等高端设备上进行了测试。
If you still have a lag running this code, then the test device must be the reason. The code is perfect.
如果运行此代码仍然有延迟,那么测试设备一定是原因。代码是完美的。
UPDATE
更新
I just checked on my Moto G running on Lollipop too, the animation is running smoothly. This is a very small and light-weight animation, and should never be laggy. If you are still getting a lag, then it must be the device you are testing, or some other piece of code on that Activity making the UI slow or unresponsive.
我也刚刚检查了在 Lollipop 上运行的 Moto G,动画运行流畅。这是一个非常小和轻量级的动画,永远不应该滞后。如果您仍然遇到延迟,那么一定是您正在测试的设备,或者该 Activity 上的其他一些代码导致 UI 变慢或无响应。
Try to check which one is applicable to you,
尝试检查哪个适用于您,
- I have tested on a total of 6 devices now with no lag at all. So, you can be rest assured that your production app will not have any lag, it can be your device which is slow
- If you are doing some heavy operations like accessing the file system, database, or any other heavy operation, it must be slowing down the UI thread and you are loosing frames.Try to use
AsyncTask
for these heavy operations
- 我现在总共测试了 6 台设备,完全没有延迟。因此,您可以放心,您的生产应用程序不会有任何延迟,它可能是您的设备缓慢
- 如果您正在执行一些繁重的操作,例如访问文件系统、数据库或任何其他繁重的操作,那么它一定会减慢 UI 线程的速度并且您正在丢失帧。尝试
AsyncTask
用于这些繁重的操作
回答by Gopal Singh Sirvi
Try this. It will animate the imageview.
尝试这个。它将动画图像视图。
ImageView img_animation = (ImageView) findViewById(R.id.img_animation);
Display display = getWindowManager().getDefaultDisplay();
float width = display.getWidth();
TranslateAnimation animation = new TranslateAnimation(0, width - 50, 0, 0); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(1000); // animation duration
animation.setRepeatCount(5); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to
// left )
// animation.setFillAfter(true);
img_animation.startAnimation(animation); // start animation
回答by Zigri2612
public class MainActivity extends Activity {
int windowwidth;
int windowheight;
private LayoutParams layoutParams;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
final ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
LayoutParams layoutParams = (LayoutParams) img
.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
img.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
}
}
回答by Anshul Khare
Hope this works for u
希望这对你有用
animation.xml
动画.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Animation" >
<ImageView
android:id="@+id/img_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="42dp"
android:src="@drawable/emo_im_laughing" />
</RelativeLayout>
Animation.java
动画.java
import android.os.Bundle;
import android.app.Activity;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class Animation extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.);
ImageView img_animation = (ImageView) findViewById(R.id.img_animation);
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
0.0f, 0.0f);
animation.setDuration(5000);
animation.setRepeatCount(5);
animation.setRepeatMode(2);
animation.setFillAfter(true);
img_animation.startAnimation(animation);
}
}
回答by subrahmanyam boyapati
TranslateAnimation animate = new TranslateAnimation(0, -view.getWidth(), 0, 0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
回答by Andrew Coder
try the code below
试试下面的代码
TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)
animation.setDuration(2000); // animation duration
animation.setRepeatCount(1); // animation repeat count if u repeat only once set to 1 if u don't repeat set to 0
animation.setFillAfter(false);
your_view .startAnimation(animation);//your_view for mine is imageView
回答by Quick learner
To Move imageview
移动 imageview
from Right to LeftUsing @Aritra Roy Answer.
从从右到左使用@Aritra罗伊回答。
Use this code
使用此代码
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="75%p"
android:toXDelta="0%p"
android:duration="100000" />
</set>