进阶三之Android UI介面之(QQ介面滑动)

学的到东西的事情是锻炼,学不到的是磨练。


本讲内容:QQ介面滑动


下面是效果面

      


下面是res/layout/activity_main.xml 布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.qq.MainActivity$PlaceholderFragment" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/default_bg"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="6.0"
            android:orientation="horizontal" >

            <RelativeLayout
                android:id="@+id/layout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <ImageView
                    android:id="@+id/tab1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/tab1" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <ImageView
                    android:id="@+id/tab2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/tab2" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layout3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <ImageView
                    android:id="@+id/tab3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/tab3" />
            </RelativeLayout>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/bodylayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="#fff"
            android:gravity="center"
            android:orientation="horizontal" >
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>
在3个relativelayout外面加个LinearLayout,然后3个relativelayout属性中设置android:layout_weight="1", 使之平均


下面是res/drawabel/tab1.xml 

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/tab1_over" android:state_pressed="true"/>
    <item android:drawable="@drawable/tab1_normal" android:state_pressed="false"/>
    <!-- 用来区分点击前后,可以是俩张图片 -->

</selector>


下面是res/drawabel/tab2.xml 

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/tab2_over" />
    <item android:state_pressed="false" android:drawable="@drawable/tab2_normal" />
</selector>


下面是res/drawabel/tab3.xml 

<?xml version="1.0" encoding="UTF-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/tab3_over" />
    <item android:state_pressed="false" android:drawable="@drawable/tab3_normal" />
</selector>

下面是res/layout/tab1.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash" />

</LinearLayout>


下面是res/layout/tab2.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageView 
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash2"/>

</LinearLayout>


下面是res/layout/tab3.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageView 
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash3"/>

</LinearLayout>

下面是MainActivity.java主界面文件:

public class MainActivity extends ActivityGroup {
	private RelativeLayout layout;
	
	private RelativeLayout layout1;
	private RelativeLayout layout2;
	private RelativeLayout layout3;
	
	private RelativeLayout bodylayout;
	
	private ImageView tab1;
	private ImageView tab2;
	private ImageView tab3;

	private ImageView first;
	private int current=1;//默认选中第一个,可以动态的改变此参数值
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initUI();
	}
	
	private void initUI() {
		layout=(RelativeLayout) findViewById(R.id.root);
		
		layout1=(RelativeLayout) findViewById(R.id.layout1);
		layout2=(RelativeLayout) findViewById(R.id.layout2);
		layout3=(RelativeLayout) findViewById(R.id.layout3);
		bodylayout=(RelativeLayout) findViewById(R.id.bodylayout);
		
		tab1=(ImageView) findViewById(R.id.tab1);
		tab2=(ImageView) findViewById(R.id.tab2);
		tab3=(ImageView) findViewById(R.id.tab3);
		tab1.setOnClickListener(onClickListener);
		tab2.setOnClickListener(onClickListener);
		tab3.setOnClickListener(onClickListener);
		
		RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		p.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		first=new ImageView(this);
		first.setTag("first");
		first.setImageResource(R.drawable.topbar_select);
		
		// 默认选中项
		switch (current) {
		case 1:
			layout1.addView(first, p);
			current=R.id.tab1;
			break;
		case 2:
			layout2.addView(first, p);
			current = R.id.tab2;
			break;
		case 3:
			layout3.addView(first, p);
			current = R.id.tab3;
			break;
		default:
			break;
		}
		
		View view = getLocalActivityManager().startActivity("index",
				new Intent(MainActivity.this, Tab1.class))
				.getDecorView();
		bodylayout.addView(view);
		
	}

	private boolean isAdd=false; // 是否添加过 top_select
	private int select_width;    // top_select_width
	private int select_height;   // top_select_height
	private int firstLeft;       // 第一次添加后的左边距*****
	private int startLeft;       // 起始左边距

	// 添加一个view,移除一个view
	private void replace() {
		switch (current) {
		case R.id.tab1:
			changeTop(layout1);
			break;
		case R.id.tab2:
			changeTop(layout2);
			break;
		case R.id.tab3:
			changeTop(layout3);
			break;
		default:
			break;
		}
	}
	
	private void changeTop(RelativeLayout relativeLayout) {
		ImageView old=(ImageView) relativeLayout.findViewWithTag("first");
		select_width=old.getWidth();
		select_height=old.getHeight();
		
		RelativeLayout.LayoutParams p=new RelativeLayout.LayoutParams(select_width, select_height);
		
		p.leftMargin = old.getLeft() + ((RelativeLayout)old.getParent()).getLeft();
		p.topMargin = old.getTop() + ((RelativeLayout)old.getParent()).getTop();
		
		// 获取起始位置
		firstLeft = old.getLeft() + ((RelativeLayout)old.getParent()).getLeft();
		
		ImageView iv = new ImageView(this);
		iv.setTag("move");
		iv.setImageResource(R.drawable.topbar_select);
		
		layout.addView(iv , p);
		relativeLayout.removeView(old);
	}
	
	private OnClickListener onClickListener = new OnClickListener(){
		public void onClick(View v) {
			if(!isAdd){
				replace(); // 初次使用移除old 添加新的top_select为RelativeLayout所使用
				isAdd = true;
			}
			
			ImageView top_select = (ImageView) layout.findViewWithTag("move");
			int tabLeft;
			int endLeft = 0;
			
			boolean run = false;

			switch (v.getId()) {
			case R.id.tab1:
				if (current != R.id.tab1) {
					// 中心位置
					tabLeft = ((RelativeLayout) tab1.getParent()).getLeft() + tab1.getLeft() + tab1.getWidth() / 2;
					// 最终位置
					endLeft = tabLeft - select_width / 2;
					current = R.id.tab1;
					run = true;
					bodylayout.removeAllViews();
					View view = getLocalActivityManager().startActivity("index",
							new Intent(MainActivity.this, Tab1.class))
							.getDecorView();
					bodylayout.addView(view);
				}
				break;
			case R.id.tab2:
				if (current != R.id.tab2) {
					tabLeft = ((RelativeLayout) tab2.getParent()).getLeft() + tab2.getLeft() + tab2.getWidth() / 2;
					endLeft = tabLeft - select_width / 2;
					current = R.id.tab2;
					run = true;
					bodylayout.removeAllViews();
					View view = getLocalActivityManager().startActivity("index",
							new Intent(MainActivity.this, Tab2.class))
							.getDecorView();
					bodylayout.addView(view);
				}
				break;
			case R.id.tab3:
				if (current != R.id.tab3) {
					tabLeft = ((RelativeLayout) tab3.getParent()).getLeft() + tab3.getLeft() + tab3.getWidth() / 2;
					endLeft = tabLeft - select_width/2;
					current = R.id.tab3;
					run = true;
					bodylayout.removeAllViews();
					View view = getLocalActivityManager().startActivity("index",
							new Intent(MainActivity.this, Tab3.class))
							.getDecorView();
					bodylayout.addView(view);
				}
				break;
			default:
				break;
			}
			
			if(run){
				TranslateAnimation animation = new TranslateAnimation(startLeft, endLeft - firstLeft, 0, 0);
				startLeft = endLeft - firstLeft; // 重新设定起始位置
				animation.setDuration(400);
				animation.setFillAfter(true);
				top_select.bringToFront();
				top_select.startAnimation(animation);
			}
			
		}

	};

}

下面是Tab1.java界面文件:

public class Tab1 extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab1);
	}
}

下面是Tab2.java界面文件:

public class Tab2 extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab2);
	}
}

下面是Tab3.java界面文件:

public class Tab3 extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab3);
	}
}

本讲就到这里,Take your time and enjoy it






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值