Android 仿QQ主页面的实

  这一节讲一下QQ主页面的实现,先看一下官方效果图:


其中的好友,群组等既可以点击切换也卡,也可以滑动切换。所以,在实现的时候要同时使用两个手段。“会话”,“好友”等可以用Button来写,也可以是RadioButton,还可以是TextView,方法很多,在这里我选择了用TextView来做。而且这里的TextView要支持颜色的切换,默认一个暗白色,页卡停留在那是白色。总体来说还是比较简单的,下面看一下xml布局文件:

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

    <RelativeLayout
        android:id="@+id/headlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/chat_bottom_send_pressed" >

        <ImageView
            android:id="@+id/stateimage"
            android:layout_width="13dp"
            android:layout_height="13dp"
            android:layout_toRightOf="@+id/nametext"
            android:layout_marginLeft="10dp"
            android:layout_centerInParent="true"
            android:contentDescription="wq"
            android:src="@drawable/onlinestatus_online" />

        <ImageView
            android:id="@+id/faceimage"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="6dp"
            android:layout_marginTop="10dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/qq" />

        <TextView
            android:id="@+id/nametext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="14dp"
            android:layout_toRightOf="@+id/faceimage"
            android:text="JY"
            android:textColor="@color/color_bai"
            android:textSize="18dp" />
    </RelativeLayout>//这个Layout是最上面的头像,名称以及状态的布局。当然,也可以用LinearLayout来做。

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/login_moremenu_back"
        android:orientation="vertical"
        >
    <LinearLayout//这个LinearLayout用来显示四个标题
        android:id="@+id/bodylayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/login_moremenu_back"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/speaktext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="会话"
            android:textColor="@drawable/text_color"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/fridenttext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="好友"
            android:textColor="@drawable/text_color"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/grouptext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="群组"
            android:textColor="@drawable/text_color"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/changetext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="动态"
            android:textColor="@drawable/text_color"
            android:textSize="18dp" />
    </LinearLayout>
    
    <ImageView//这个就是下面的横杠图片
        android:id="@+id/cursor"
        android:layout_width="50dp"
        android:layout_height="6dp"
        android:layout_marginLeft="15dp"
        android:src="@drawable/meitu" />
    
    </LinearLayout>

    

    <android.support.v4.view.ViewPager//不解释,用来滑动页卡
        android:id="@+id/vPager"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:background="#000000"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />

</LinearLayout>

 有了上一篇《Android ViewPager使用详解 》的基础 ,Activity的代码就很容易理解了,我就不注释了。看一下Activity代码:

package com.example.imitateqq;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class MainQQActivity extends Activity {

	private ViewPager viewPager;
	private ImageView imageView;
	private TextView textView1, textView2, textView3, textView4;
	private View view1, view2, view3, view4;
	private List<View> views;
	private int offSet = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.mainqq);
		initView();
		initListener();
	}

	private void initView() {
		viewPager = (ViewPager) findViewById(R.id.vPager);
		imageView = (ImageView) findViewById(R.id.cursor);

		textView1 = (TextView) findViewById(R.id.speaktext);
		textView2 = (TextView) findViewById(R.id.fridenttext);
		textView3 = (TextView) findViewById(R.id.grouptext);
		textView4 = (TextView) findViewById(R.id.changetext);

		LayoutInflater layoutInflater = getLayoutInflater();
		view1 = layoutInflater.inflate(R.layout.qqtab_1, null);
		view2 = layoutInflater.inflate(R.layout.qqtab_2, null);
		view3 = layoutInflater.inflate(R.layout.qqtab_3, null);
		view4 = layoutInflater.inflate(R.layout.qqtab_4, null);

		views = new ArrayList<View>();
		views.add(view1);
		views.add(view2);
		views.add(view3);
		views.add(view4);

		bmpW=BitmapFactory.decodeResource(getResources(), R.drawable.a).getWidth();
		DisplayMetrics displayMetrics=new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		int screenW=displayMetrics.widthPixels;
		offSet=(screenW / 4 - bmpW) / 2;
		Matrix matrix=new Matrix();
		matrix.postTranslate(screenW, 0);
		imageView.setImageMatrix(matrix);
	}

	private void initListener() {
		textView1.setOnClickListener(new MyOnClickListener(0));
		textView2.setOnClickListener(new MyOnClickListener(1));
		textView3.setOnClickListener(new MyOnClickListener(2));
		textView4.setOnClickListener(new MyOnClickListener(3));

		viewPager.setAdapter(new MyPagerAdapter(views));
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
		viewPager.setCurrentItem(0);
		textView1.setTextColor(getResources().getColor(R.color.color_bai));
	}

	private class MyPagerAdapter extends PagerAdapter {
		private List<View> mListViews;

		public MyPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(mListViews.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(mListViews.get(position));
			return mListViews.get(position);
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

	}

	private class MyOnPageChangeListener implements OnPageChangeListener {
		int one = offSet * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
		
		public void onPageScrollStateChanged(int arg0) {
			

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
			

		}

		public void onPageSelected(int arg0) {
			Animation animation = new TranslateAnimation(one*currIndex, one*arg0, 0, 0);
			currIndex = arg0;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(300);
			imageView.startAnimation(animation);
			switch(currIndex){
			case 0:
				textView1.setTextColor(getResources().getColor(R.color.color_bai));
				textView2.setTextColor(getResources().getColor(R.color.title_bg));
				textView3.setTextColor(getResources().getColor(R.color.title_bg));
				textView4.setTextColor(getResources().getColor(R.color.title_bg));
				break;
			case 1:
				textView2.setTextColor(getResources().getColor(R.color.color_bai));
				textView1.setTextColor(getResources().getColor(R.color.title_bg));
				textView3.setTextColor(getResources().getColor(R.color.title_bg));
				textView4.setTextColor(getResources().getColor(R.color.title_bg));
				break;
			case 2:
				textView3.setTextColor(getResources().getColor(R.color.color_bai));
				textView2.setTextColor(getResources().getColor(R.color.title_bg));
				textView1.setTextColor(getResources().getColor(R.color.title_bg));
				textView4.setTextColor(getResources().getColor(R.color.title_bg));
				break;
			case 3:
				textView4.setTextColor(getResources().getColor(R.color.color_bai));
				textView2.setTextColor(getResources().getColor(R.color.title_bg));
				textView3.setTextColor(getResources().getColor(R.color.title_bg));
				textView1.setTextColor(getResources().getColor(R.color.title_bg));
				default :
					break;
			}
		}

	}

	private class MyOnClickListener implements OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		public void onClick(View v) {
			viewPager.setCurrentItem(index);
			
			switch(index){
			case 0:
				textView1.setTextColor(getResources().getColor(R.color.color_bai));
				textView2.setTextColor(getResources().getColor(R.color.title_bg));
				textView3.setTextColor(getResources().getColor(R.color.title_bg));
				textView4.setTextColor(getResources().getColor(R.color.title_bg));
				break;
			case 1:
				textView2.setTextColor(getResources().getColor(R.color.color_bai));
				textView1.setTextColor(getResources().getColor(R.color.title_bg));
				textView3.setTextColor(getResources().getColor(R.color.title_bg));
				textView4.setTextColor(getResources().getColor(R.color.title_bg));
				break;
			case 2:
				textView3.setTextColor(getResources().getColor(R.color.color_bai));
				textView2.setTextColor(getResources().getColor(R.color.title_bg));
				textView1.setTextColor(getResources().getColor(R.color.title_bg));
				textView4.setTextColor(getResources().getColor(R.color.title_bg));
				break;
			case 3:
				textView4.setTextColor(getResources().getColor(R.color.color_bai));
				textView2.setTextColor(getResources().getColor(R.color.title_bg));
				textView3.setTextColor(getResources().getColor(R.color.title_bg));
				textView1.setTextColor(getResources().getColor(R.color.title_bg));
				default :
					break;
			}
			
		}

	}
}


 最后看一下效果图:

  关于QQ的UI部分基本上就是这样,之后将引入服务器,实现通信的功能。当然,还有很多UI要写,比如聊天页面,设置页面等等,在用到的时候再去写。谢谢大家关注!

  下载地址

  • 41
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 171
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值