基于AnDroidFrameLayout的使用详解
使用 详解 基于
2023-06-13 09:14:54 时间
今天在学习实现墨迹天气那样的拖动效果时,看到用的是重写FrameLayout。翻了翻书,突然想明白,为什么用FrameLayout.
在FrameLayout中,用我看的书中的话说是,空间永远用不完。
复制代码代码如下:
<?xmlversion="1.0"encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#897753"
>
<ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:src="@drawable/sky"/>
<ImageView
android:id="@+id/image2"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/cloud"/>
<ImageView
android:id="@+id/image3"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/sun"/>
</FrameLayout>
其中,image1、image2、image3都是在同一块空间的。可以说它们是重叠着的,界面显示的是最近用的那一个。
在我的代码中把它们都先不可见。
在整体代码中实现的是点一下屏幕就换一张图片。
另外,我个人感觉,实现拖动效果的关键原理就是framelayout使得几部分空间的重叠。设置只有一部分可见。当拖动时,设置其他部分移动。
发现下载附近要扣e币,我把代码也贴上,图片可以换成自己喜欢的~
FramLayoutTestActivity.java
importjava.util.ArrayList;
importjava.util.List;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.animation.Animation;
importandroid.widget.ImageView;
publicclassFramLayoutTestActivityextendsActivity{
private StringTAG="FramLayoutTestActivity";
privateImageViewimage1;
privateImageViewimage2;
privateImageViewimage3;
privateList<ImageView>list;
privateintcount=0;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1=(ImageView)findViewById(R.id.image1);
image2=(ImageView)findViewById(R.id.image2);
image3=(ImageView)findViewById(R.id.image3);
list=newArrayList<ImageView>();
list.add(image1);
list.add(image2);
list.add(image3);
}
@Override
publicbooleanonTouchEvent(MotionEventevent){
//TODOAuto-generatedmethodstub
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
Log.i(TAG,"move---");
showImage();
}
returnsuper.onTouchEvent(event);
}
privatevoidshowImage()
{
image1.setVisibility(View.VISIBLE);
count=count%3;
for(ImageViewi:list)
{
i.setVisibility(View.INVISIBLE);
}
list.get(count).setVisibility(View.VISIBLE);
count++;
}
}
main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#897753"
>
<ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:src="@drawable/sky"/>
<ImageView
android:id="@+id/image2"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/cloud"/>
<ImageView
android:id="@+id/image3"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/sun"/>
</FrameLayout>
相关文章
- Spring-Security 简介、入门案例详解、安全框架、权限验证 SSM项目 使用 JavaConfig配置
- Java直接(堆外)内存使用详解编程语言
- [PHP] apache在worker模式配置fastcgi使用php-fpm详解编程语言
- ThreadLocal使用详解编程语言
- 使用pyqt写了一个检查大数据环境的gui详解编程语言
- 探究MySQLres的功能与使用方法MySQL资源结果集库详解(mysql_res)
- MySQL笔记之数据备份与还原的使用详解
- 基于Sizeof与Strlen的区别以及联系的使用详解
- 基于JavaScript实现继承机制之构造函数+原型链混合方式的使用详解
- c语言中基于随机函数的使用详解
- 基于c++中的默认拷贝函数的使用详解
- 基于C#MBG扩展方法类库的使用详解
- 基于动态修改App.Config与web.Config的使用详解
- 基于Java回顾之JDBC的使用详解
- 基于C++字符串替换函数的使用详解
- 基于.Net中的数字与日期格式化规则助记词的使用详解
- 基于php使用memcache存储session的详解
- MySQL触发器使用详解