获取屏幕宽度,将view移出屏幕再移动回来
时间:2022-03-12 14:57
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>
相关推荐
- Android系统编程入门系列之界面Activity交互响应
- 新型横向移动工具原理分析、代码分析、优缺点以及检测方案
- uni-app滚动视图容器(scroll-view)之监听上拉事件
- uniapp h5,app两端复制文本
- Android系统编程入门系列之界面Activity响应丝滑的传统动画
- 【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn
- 诺基亚短信生成!太好玩了
- iOS 跳转App Store进行评分
- 开发一个即时通讯App
- 关闭苹果IOS app自动更新
电脑软件
本类排行
- 1关闭苹果IOS app自动更新
- 2iOS 跳转App Store进行评分
- 3诺基亚短信生成!太好玩了
- 4Android系统编程入门系列之界面Activity响应丝滑的传统动画
- 5uniapp h5,app两端复制文本
- 6uni-app滚动视图容器(scroll-view)之监听上拉事件
- 7新型横向移动工具原理分析、代码分析、优缺点以及检测方案
- 8Android系统编程入门系列之界面Activity交互响应
- 9开发一个即时通讯App
- 10【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn