[英]Android animation view right to left
我做了一個小應用程序,我在android動畫/過渡中是新的。
我想要的是:如果按下按鈕,背景(視圖)應該滑出而另一個應該進入,但是我不想開始其他活動,我只想更改現有背景。
這正是我想要的: https : //www.polymer-project.org/0.5/components/core-animated-pages/demos/simple.html (您必須更改左上角框中的框才能看到它)
我在其他帖子中讀到了有關它的內容,但通常有啟動另一個活動的解決方案。
如果您理解我的意思,那么給我一個提示或指向教程的鏈接是很高興的。
編輯-解決方案
我讓它對我有用,我建立了一個代碼,該代碼完全符合我的意願,我給出了所有答案1up並標記了最后一個,因為它接近我所做的。 謝謝大家。
此鏈接非常有用: https : //github.com/codepath/android_guides/wiki/Animations (到目前為止,我發現的最佳動畫教程)
從右向左滑動Android動畫 (從右到左,從上到下,…的XML)
左移示例:
碼:
public void transitionleft(final View viewcome, final View viewgo){
animcome = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_right_in);
animgo = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_out);
animgo.setDuration(1000);
animcome.setDuration(1000);
animcome.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
viewcome.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animgo.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
viewgo.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
viewcome.startAnimation(animcome);
viewgo.startAnimation(animgo);
}
res / anim / slide_right_in中的XML過渡
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="5000" android:fromXDelta="100%" android:toXDelta="0%" />
<alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="1.0" />
</set>
RES /動畫/ slide_left_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="-100%"/>
<alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="1.0" />
</set>
工作原理:我有2個視圖,每個視圖都有屏幕大小(match_parent)。 一個可見,另一個不可見。 然后將可見的函數作為viewgo(因為它應該消失),將不可見的函數作為viewcome(因為這應該是新來的人)。在動畫開始時,viewcome將設置為可見,因為它的“滑動”。 動畫完成后,視線將不可見,因為其“滑出”。 這非常容易,而且效果很好。
為了改善動畫效果,您可以使用AnimatorSet來同步兩個動畫(set.playtogether()),但如果沒有這樣做,它對我也很好。
也許您可以嘗試使用valueAnimator:
//animate from your current color to red ValueAnimator
anim = ValueAnimator.ofInt(view.getBackgroundColor(),
Color.parseColor("#FF0000"));
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override public void onAnimationUpdate(ValueAnimator animation) {
view.setBackgroundColor(animation.getAnimatedValue());
}
});
anim.start();
public class MainActivity extends Activity implements OnGestureListener {
private LinearLayout main;
private TextView viewA;
private GestureDetector gestureScanner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this will set default color to black
View view = findViewById(R.id.textView);
view.setBackgroundColor(Color.BLACK);
}
@Override
public boolean onTouchEvent(MotionEvent me) {
return gestureScanner.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent e) {
viewA.setText("-" + "DOWN" + "-");
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
viewA.setText("-" + "FLING" + "-");
//changes color on swipe
view.setBackgroundColor(Color.GREEN);
return true;
}
@Override
public void onLongPress(MotionEvent e) {
viewA.setText("-" + "LONG PRESS" + "-");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
viewA.setText("-" + "SCROLL" + "-");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
viewA.setText("-" + "SHOW PRESS" + "-");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
viewA.setText("-" + "SINGLE TAP UP" + "-");
return true;
}
}
在onCreate中,默認顏色設置為黑色。 現在,無論何時檢測到“事件”,例如觸摸,滑動(滾動),滾動等,您的應用程序都應該更改顏色。 在這里,我更改了onFling的顏色,您可以對其進行修改以獲取隨機顏色,甚至可以在其他事件偵聽器中進行相同操作。
該鏈接應為您指明正確的方向。
希望這可以幫助。
編輯:
對於過渡效果:
animatedBackgroundView.setBackgroundResource(R.anim.background_touch);
animatedBackgroundView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
TransitionDrawable transition = (TransitionDrawable) view.getBackground();
transition.startTransition(500);
transition.reverseTransition(500);
}
});
您的背景資源文件:
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#22ffffff" />
<item android:drawable="#00ffffff" />
</transition>
這是一個完整的解決方案:
在jour xml文件中,將兩個視圖設置在同一位置,並將第二個視圖的可見性設置為invisible
:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/currentView"
android:text="currentView"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="invisible"
android:id="@+id/nextView"
android:text="nextView"/>
</RelativeLayout>
然后在您的活動中:
//First in you onCreate translate the second Layout to the right
comingView.setTranslationX(testbutton.getWidth());
// Here is the method that will do the job
// We slide both views (current one and waiting one) at the same time
// and in the same direction
private void slideExit(View currentView,final View comingView) {
currentView.animate().
translationXBy(-currentView.getWidth()).
setDuration(1000).
setInterpolator(new LinearInterpolator()).
setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
slideFromRightToLeft(comingView);
comingView.setVisibility(View.VISIBLE);
}
});
}
private void slideFromRightToLeft(View v) {
v.animate().
translationX(0).
setDuration(1000).
setInterpolator(new LinearInterpolator());
}
//call the method whenever you want
slideExit(currentView, comingView);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.