Android — How to position View off-screen?

后端 未结 6 2046
鱼传尺愫
鱼传尺愫 2020-11-28 01:08

I\'m trying to animate a simple ImageView in my application and I want it to slide in from the bottom of the screen and come to a resting position where the top 50px of the

6条回答
  •  醉梦人生
    2020-11-28 01:56

    I figured out a solution to this that should be easy to implement. It involves modifying the layout and the Activity inflating the layout... see below:

    Activity (QuickPlay.java):

    public class QuickPlay extends Activity implements AnimationListener
    {
        private ImageView myImageView;
        private LinearLayout LL;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            this.setContentView(R.layout.quick_play_screen);
    
            myImageView = (ImageView) this.findViewById(R.id.Clip);
            LL = (LinearLayout) this.findViewById(R.id.QuickPlayClipLayout);
    
            //finally
            Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide_in_quickplay);
            anim.setAnimationListener(this);
            LL.startAnimation(anim);
        }
        @Override
        public void onAnimationEnd(Animation animation){}
    
        @Override
        public void onAnimationRepeat(Animation animation){}
    
        @Override
        public void onAnimationStart(Animation animation)
        {
            // This is the key...
            //set the coordinates for the bounds (left, top, right, bottom) based on the offset value (50px) in a resource XML
            LL.layout(0, -(int)this.getResources().getDimension(R.dimen.quickplay_offset), 
                    LL.getWidth(), LL.getHeight() + (int)this.getResources().getDimension(R.dimen.quickplay_offset));
        }
    }
    

    New LinearLayout (CustomLinearLayout.java):

    public class CustomLinearLayout extends LinearLayout
    {
        private Context myContext;
    
        public CustomLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            myContext = context;
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec+((int)myContext.getResources().getDimension(R.dimen.quickplay_offset)));
        }
    }
    

    Layout (/res/layout/quick_play_screen.xml):

    
       
          
          
       
    

    Resource (/res/values/constants.xml):

    
    
        50dp
    
    

    Animation (/res/anim/slide_in_quickplay.xml):

    
    
       
       
    
    

    The program now does exactly what I need it to do. The entire layout starts off screen at the bottom, slides in in 1 sec and comes to a rest where the top of the layout is actually 50px off the top of the screen (i.e. LL.getTop() = -50) and the bottom of the layout is resting at the bottom of the screen (i.e. LL.getBottom() = 530 = 480 + 50).

提交回复
热议问题