Android之FrameLayout的使用
2015-09-11 14:57
435 查看
今天在学习实现墨迹天气那样的拖动效果时,看到用的是重写FrameLayout。翻了翻书,突然想明白,为什么用FrameLayout.
在FrameLayout中,用我看的书中的话说是,空间永远用不完。
<?xml version="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
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
public class FramLayoutTestActivity extends Activity {
private String TAG = "FramLayoutTestActivity";
private ImageView image1;
private ImageView image2;
private ImageView image3;
private List<ImageView> list;
private int count=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
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=new ArrayList<ImageView>();
list.add(image1);
list.add(image2);
list.add(image3);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
Log.i(TAG,"move---");
showImage();
}
return super.onTouchEvent(event);
}
private void showImage()
{
image1.setVisibility(View.VISIBLE);
count=count%3;
for(ImageView i:list)
{
i.setVisibility(View.INVISIBLE);
}
list.get(count).setVisibility(View.VISIBLE);
count++;
}
}
main.xml
<?xml version="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>
在FrameLayout中,用我看的书中的话说是,空间永远用不完。
<?xml version="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
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
public class FramLayoutTestActivity extends Activity {
private String TAG = "FramLayoutTestActivity";
private ImageView image1;
private ImageView image2;
private ImageView image3;
private List<ImageView> list;
private int count=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
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=new ArrayList<ImageView>();
list.add(image1);
list.add(image2);
list.add(image3);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
Log.i(TAG,"move---");
showImage();
}
return super.onTouchEvent(event);
}
private void showImage()
{
image1.setVisibility(View.VISIBLE);
count=count%3;
for(ImageView i:list)
{
i.setVisibility(View.INVISIBLE);
}
list.get(count).setVisibility(View.VISIBLE);
count++;
}
}
main.xml
<?xml version="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>
相关文章推荐
- Android软键盘用法实例
- Android自定义Spinner
- android animation
- Android:继承ImageView 实现 圆角图片,圆形图片处理。
- android 仿QQ好友动态 title滑动渐变
- Timer和TimerTask在Android中的用法
- android shape的使用
- Android的EditText控件常用属性
- 《ArcGIS Runtime SDK for Android开发笔记》——(5)、基于Android Studio构建ArcGIS Android开发环境(离线部署)
- android开发第一弹--androidStudio常用操作笔记
- 深入android的消息机制(二)
- 问题-ListView 使用的时候,已经遇到的问题;
- Android ListView优化
- Android ViewGroup事件分发机制
- Android View 事件分发机制 源码解析 (上)
- Android 基础进阶 (一)
- Android View的绘制机制流程深入详解(四)
- Android中的长度单位详解(dp、sp、px、in、pt、mm)
- android Button 监听的几种方式
- 反编译android之 .odex .odex.xz .dex