面对一个奇怪的问题,我使用RelativeLayout生成了我的布局文件。 然而,当读取其中一个孩子的顶部时,它被返回为零,这导致我的整个动画逻辑去折腾。
我的想法是将它们从屏幕底部动画到初始位置。 以下是我的代码,大家的任何帮助都将受到高度赞赏。
我的布局文件。
<?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" android:gravity="center_horizontal" android:paddingLeft="35dp" android:paddingRight="35dp" > <LinearLayout android:id="@+id/msgs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="35dp" android:layout_marginTop="25dp" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:gravity="center_horizontal" android:text="@string/share" android:textColor="@android:color/black" android:textSize="26dp" android:typeface="monospace" /> <TextView android:layout_width="match_parent" android:layout_height="48dp" android:layout_gravity="top" android:maxLines="3" android:text="@string/share_message" android:textColor="@android:color/black" android:textSize="14dp" android:typeface="sans" /> </LinearLayout> <ImageView android:id="@+id/view_twitter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_above="@id/msgs" android:layout_marginEnd="25dp" android:scaleType="fitCenter" android:src="@drawable/circle_twitter" /> <ImageView android:id="@+id/view_facebook" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/msgs" android:layout_marginEnd="25dp" android:scaleType="fitCenter" android:layout_toStartOf="@id/view_twitter" android:src="@drawable/facebook_circle" /> <ImageView android:id="@+id/view_google" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/msgs" android:layout_marginEnd="25dp" android:layout_toEndOf="@id/view_twitter" android:scaleType="fitCenter" android:src="@drawable/google_circle" /> </RelativeLayout>我的片段代码,在哪里动画它们
import android.animation.Animator; import android.animation.Animator.AnimatorListener; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.graphics.Point; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.Display; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.AccelerateDecelerateInterpolator; public class IntroFragmentThird extends Fragment{ View google; View twitter; View facebook; View messagePanel; int screenHeight; int screenWidth; int offset; float beginPoint; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Display display = getActivity().getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); screenWidth = size.x; screenHeight = size.y; offset = screenHeight + 200; // start from 200px below screen height } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.intro3_layout, container, false); google = view.findViewById(R.id.view_google); facebook = view.findViewById(R.id.view_facebook); twitter = view.findViewById(R.id.view_twitter); messagePanel = view.findViewById(R.id.msgs); google.setVisibility(View.GONE); // Initial view visibility will be gone facebook.setVisibility(View.GONE); twitter.setVisibility(View.GONE); beginPoint = twitter.getTop(); // this is coming as ZERO return view; } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("#### Top : " + beginPoint); playScreenAnimation(); } public void playScreenAnimation(){ facebook.clearAnimation(); facebook.setVisibility(View.GONE); facebook.clearAnimation(); facebook.setVisibility(View.GONE); facebook.clearAnimation(); facebook.setVisibility(View.GONE); ObjectAnimator anim1 = ObjectAnimator.ofFloat(facebook, "y", offset , beginPoint); anim1.setInterpolator(new AccelerateDecelerateInterpolator()); anim1.setDuration(1000); anim1.setStartDelay(1000); anim1.addListener(new AnimatorListener(){ @Override public void onAnimationStart(Animator animation) { facebook.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationCancel(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animator animation) { // TODO Auto-generated method stub } }); ObjectAnimator anim2 = ObjectAnimator.ofFloat(twitter, "y", offset , beginPoint); anim2.setInterpolator(new AccelerateDecelerateInterpolator()); anim2.setDuration(1000); anim2.addListener(new AnimatorListener(){ @Override public void onAnimationStart(Animator animation) { twitter.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationCancel(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animator animation) { // TODO Auto-generated method stub } }); ObjectAnimator anim3 = ObjectAnimator.ofFloat(google, "y", offset , beginPoint); anim3.setInterpolator(new AccelerateDecelerateInterpolator()); anim3.setDuration(1000); anim3.setStartDelay(1000); anim3.addListener(new AnimatorListener(){ @Override public void onAnimationStart(Animator animation) { google.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationCancel(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animator animation) { // TODO Auto-generated method stub } }); AnimatorSet set = new AnimatorSet(); set.playTogether(anim2,anim1,anim3); set.start(); } }这是截图,初始状态(三个fb,g +,Twitter圈子正在动画)
am facing a strange problem, I have generated my layout file using RelativeLayout. however while reading top of one of the child's present in it is being returned as zero,which is causing my whole animation logic go for toss.
My idea is to animate them from bottom of screen, to there initial position. below is my code, any help from you all will be highly appreciated.
My layout file.
<?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" android:gravity="center_horizontal" android:paddingLeft="35dp" android:paddingRight="35dp" > <LinearLayout android:id="@+id/msgs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="35dp" android:layout_marginTop="25dp" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:gravity="center_horizontal" android:text="@string/share" android:textColor="@android:color/black" android:textSize="26dp" android:typeface="monospace" /> <TextView android:layout_width="match_parent" android:layout_height="48dp" android:layout_gravity="top" android:maxLines="3" android:text="@string/share_message" android:textColor="@android:color/black" android:textSize="14dp" android:typeface="sans" /> </LinearLayout> <ImageView android:id="@+id/view_twitter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_above="@id/msgs" android:layout_marginEnd="25dp" android:scaleType="fitCenter" android:src="@drawable/circle_twitter" /> <ImageView android:id="@+id/view_facebook" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/msgs" android:layout_marginEnd="25dp" android:scaleType="fitCenter" android:layout_toStartOf="@id/view_twitter" android:src="@drawable/facebook_circle" /> <ImageView android:id="@+id/view_google" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/msgs" android:layout_marginEnd="25dp" android:layout_toEndOf="@id/view_twitter" android:scaleType="fitCenter" android:src="@drawable/google_circle" /> </RelativeLayout>My Fragment code, where am animating them
import android.animation.Animator; import android.animation.Animator.AnimatorListener; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.graphics.Point; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.Display; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.AccelerateDecelerateInterpolator; public class IntroFragmentThird extends Fragment{ View google; View twitter; View facebook; View messagePanel; int screenHeight; int screenWidth; int offset; float beginPoint; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Display display = getActivity().getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); screenWidth = size.x; screenHeight = size.y; offset = screenHeight + 200; // start from 200px below screen height } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.intro3_layout, container, false); google = view.findViewById(R.id.view_google); facebook = view.findViewById(R.id.view_facebook); twitter = view.findViewById(R.id.view_twitter); messagePanel = view.findViewById(R.id.msgs); google.setVisibility(View.GONE); // Initial view visibility will be gone facebook.setVisibility(View.GONE); twitter.setVisibility(View.GONE); beginPoint = twitter.getTop(); // this is coming as ZERO return view; } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("#### Top : " + beginPoint); playScreenAnimation(); } public void playScreenAnimation(){ facebook.clearAnimation(); facebook.setVisibility(View.GONE); facebook.clearAnimation(); facebook.setVisibility(View.GONE); facebook.clearAnimation(); facebook.setVisibility(View.GONE); ObjectAnimator anim1 = ObjectAnimator.ofFloat(facebook, "y", offset , beginPoint); anim1.setInterpolator(new AccelerateDecelerateInterpolator()); anim1.setDuration(1000); anim1.setStartDelay(1000); anim1.addListener(new AnimatorListener(){ @Override public void onAnimationStart(Animator animation) { facebook.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationCancel(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animator animation) { // TODO Auto-generated method stub } }); ObjectAnimator anim2 = ObjectAnimator.ofFloat(twitter, "y", offset , beginPoint); anim2.setInterpolator(new AccelerateDecelerateInterpolator()); anim2.setDuration(1000); anim2.addListener(new AnimatorListener(){ @Override public void onAnimationStart(Animator animation) { twitter.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationCancel(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animator animation) { // TODO Auto-generated method stub } }); ObjectAnimator anim3 = ObjectAnimator.ofFloat(google, "y", offset , beginPoint); anim3.setInterpolator(new AccelerateDecelerateInterpolator()); anim3.setDuration(1000); anim3.setStartDelay(1000); anim3.addListener(new AnimatorListener(){ @Override public void onAnimationStart(Animator animation) { google.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationCancel(Animator animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animator animation) { // TODO Auto-generated method stub } }); AnimatorSet set = new AnimatorSet(); set.playTogether(anim2,anim1,anim3); set.start(); } }Here is screenshot of, initial state of views (those three fb, g+, twitter circles are being animated)
最满意答案
好吧,我解决了它,而不是使用对象动画,我最终使用视图动画,并考虑相对位置动画视图。
facebook.clearAnimation(); facebook.setVisibility(View.INVISIBLE); google.clearAnimation(); google.setVisibility(View.INVISIBLE); twitter.clearAnimation(); twitter.setVisibility(View.INVISIBLE); TranslateAnimation anim1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0 ,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0); anim1.setDuration(1000); anim1.setInterpolator(new AccelerateDecelerateInterpolator()); anim1.setAnimationListener(new AnimationListener(){ @Override public void onAnimationStart(Animation animation) { facebook.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } }); TranslateAnimation anim2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0 ,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0); anim2.setDuration(750); //anim2.setStartOffset(500); anim2.setInterpolator(new AccelerateDecelerateInterpolator()); anim2.setAnimationListener(new AnimationListener(){ @Override public void onAnimationStart(Animation animation) { twitter.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } }); TranslateAnimation anim3 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0 ,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0); anim3.setDuration(750); anim3.setStartOffset(500); anim3.setInterpolator(new AccelerateDecelerateInterpolator()); anim3.setAnimationListener(new AnimationListener(){ @Override public void onAnimationStart(Animation animation) { google.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } }); twitter.startAnimation(anim2); facebook.startAnimation(anim1); google.startAnimation(anim3);Alright I solved it, instead of using object animator, i ended up using view animation, and animating views considering there relative positions.
facebook.clearAnimation(); facebook.setVisibility(View.INVISIBLE); google.clearAnimation(); google.setVisibility(View.INVISIBLE); twitter.clearAnimation(); twitter.setVisibility(View.INVISIBLE); TranslateAnimation anim1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0 ,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0); anim1.setDuration(1000); anim1.setInterpolator(new AccelerateDecelerateInterpolator()); anim1.setAnimationListener(new AnimationListener(){ @Override public void onAnimationStart(Animation animation) { facebook.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } }); TranslateAnimation anim2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0 ,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0); anim2.setDuration(750); //anim2.setStartOffset(500); anim2.setInterpolator(new AccelerateDecelerateInterpolator()); anim2.setAnimationListener(new AnimationListener(){ @Override public void onAnimationStart(Animation animation) { twitter.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } }); TranslateAnimation anim3 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0 ,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0); anim3.setDuration(750); anim3.setStartOffset(500); anim3.setInterpolator(new AccelerateDecelerateInterpolator()); anim3.setAnimationListener(new AnimationListener(){ @Override public void onAnimationStart(Animation animation) { google.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } }); twitter.startAnimation(anim2); facebook.startAnimation(anim1); google.startAnimation(anim3);更多推荐
发布评论