zoukankan      html  css  js  c++  java
  • 获取屏幕宽度,将view移出屏幕再移动回来

    public class MainActivity extends AppCompatActivity {
       private TextView kuandu;
        float curTranslationX;
        float width;
        float height;
        private Button out;
        private Button in;
       private View view;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            kuandu = (TextView) findViewById(R.id.kuandu);
            out = (Button) findViewById(R.id.out);
            in = (Button) findViewById(R.id.in);
            view =  findViewById(R.id.view);
            out.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goOut();
                }
            });
            in.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goIn();
                }
            });
    
            WindowManager manager = this.getWindowManager();
            DisplayMetrics outMetrics = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(outMetrics);
            width = outMetrics.widthPixels;
            height = outMetrics.heightPixels;
            kuandu.setText("宽度是:" +width+ ",高度是" + height);
    
        }
    
        private void goOut() {
            curTranslationX = view.getTranslationX();
            ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", curTranslationX, width);
            animator.setDuration(1000);
            animator.start();
    
        }
    
        private void goIn() {
            curTranslationX = view.getTranslationX();
            ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", curTranslationX, 0);
            animator.setDuration(1000);
            animator.start();
    
        }
    
    
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.laoyimou.imagetest.MainActivity">
    
        <Button
            android:id="@+id/out"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="out" />
    
        <Button
            android:id="@+id/in"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="in" />
    
        <TextView
            android:id="@+id/kuandu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@color/colorPrimary" />
    
    
    </LinearLayout>
  • 相关阅读:
    HTML3 / 4 / 4.1 / 5 版本升级过程中,变化是怎么样的
    HTML head内所有标签,及其作用
    HTML 和 XHTML和区别
    HTML DOCTYPE 都有哪些,它们之前的区别和用途分别是什么?
    eclipse中英文切换--四种方式
    Eclipse Class Decompiler---Java反编译插件
    jdk历史版本下载
    eclipse优化(部分)
    博客迁移到github
    JavaScript:同步、异步和事件循环
  • 原文地址:https://www.cnblogs.com/laoyimou/p/8274916.html
Copyright © 2011-2022 走看看