码迷,mamicode.com
首页 > 移动开发 > 详细

获取屏幕宽度,将view移出屏幕再移动回来

时间:2018-01-12 13:38:40      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:span   宽度   width   tor   contex   nim   err   ppc   protected   

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>

 

获取屏幕宽度,将view移出屏幕再移动回来

标签:span   宽度   width   tor   contex   nim   err   ppc   protected   

原文地址:https://www.cnblogs.com/laoyimou/p/8274916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!