Android仿QQ主界面-------完善篇

在我前面的博文中,做出了仿QQ主界面的主要工作,博文地址:Android仿QQ主界面

 

但是在那一篇中还有一个不起眼的地方没做,今天就完善它。

 

今天要实现在文字下面来个ImageView,实现动画。先看看效果图。

 

在滑动页面的时候,下面的ImageView会滑动。好了,开始正题。

1.布局文件main.xml

先看代码:

[html]   view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#1685cc"  
  11.         android:orientation="vertical" >  
  12.   
  13.         <LinearLayout  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:background="#1685cc" >  
  17.   
  18.             <TextView  
  19.                 android:id="@+id/textView1"  
  20.                 android:layout_width="wrap_content"  
  21.                 android:layout_height="wrap_content"  
  22.                 android:layout_weight="1"  
  23.                 android:gravity="center"  
  24.                 android:text="试题"  
  25.                 android:textSize="25sp" />  
  26.   
  27.             <TextView  
  28.                 android:id="@+id/textView2"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:layout_weight="1"  
  32.                 android:gravity="center"  
  33.                 android:text="原文"  
  34.                 android:textSize="25sp" />  
  35.   
  36.             <TextView  
  37.                 android:id="@+id/textView3"  
  38.                 android:layout_width="wrap_content"  
  39.                 android:layout_height="wrap_content"  
  40.                 android:layout_weight="1"  
  41.                 android:gravity="center"  
  42.                 android:text="解答"  
  43.                 android:textSize="25sp" />  
  44.         </LinearLayout>  
  45.   
  46.         <ImageView  
  47.             android:id="@+id/imageView1"  
  48.             android:layout_width="50dp"  
  49.             android:layout_height="6dp"  
  50.             android:layout_marginLeft="30dip"  
  51.             android:src="@drawable/meitu" />  
  52.     </LinearLayout>  
  53.   
  54.     <android.support.v4.view.ViewPager  
  55.         android:id="@+id/viewpager"  
  56.         android:layout_width="wrap_content"  
  57.         android:layout_height="wrap_content"  
  58.         android:layout_gravity="center"  
  59.         android:layout_weight="1"  
  60.         android:background="#FF0000" />  
  61.   
  62. </LinearLayout>  


与上篇文章相比,在包含三个TextView的LinearLayout下面加了一个ImageView,用于显示图片。图片的宽和高以及左边距都是固定的。

2.功能实现的Java代码

加了三个成员变量,一个int类型的currentPage用于标示当前正在显示那个页面,一个int类型的disPlayWidth用于保存屏幕的宽度,还有一个int类型的offSet,这个变量标示的是一个宽度,下面作图说明:

 

offSet就是TextView控件的宽度减去图片的宽度除以2,如上图所示。

 

实现动画的原理就是当当前的界面发生改变就实现动画。界面的改变有两个来源

1.用户点击上面的TextView

2.用户滑动界面

我必须对这两种的改变都做出反应。

 

1.对TextView的点击做出反应在TextView的onClick方法中:

[java]   view plain copy
  1.        @Override  
  2. ublic void onClick(View v) {  
  3. int single=b.getWidth()+offSet*2;  
  4.   
  5. if(v.getId()==R.id.textView1)  
  6. {  
  7.       
  8.     vp.setCurrentItem(0);  
  9.     if(currentPage!=0)  
  10.     {  
  11.         TranslateAnimation ta=new TranslateAnimation(currentPage*single,0,0,0);  
  12.         ta.setFillAfter(true);  
  13.         ta.setDuration(200);  
  14.         i.startAnimation(ta);  
  15.     }  
  16.     currentPage=0;  
  17. }  
  18. if(v.getId()==R.id.textView2)  
  19. {  
  20.     vp.setCurrentItem(1);  
  21.     if(currentPage!=1)  
  22.     {  
  23.         TranslateAnimation ta=new TranslateAnimation(currentPage*single,single,0,0);  
  24.         ta.setFillAfter(true);  
  25.         ta.setDuration(200);  
  26.         i.startAnimation(ta);  
  27.     }  
  28.     currentPage=1;  
  29. }  
  30. if(v.getId()==R.id.textView3)  
  31. {  
  32.     vp.setCurrentItem(2);  
  33.     if(currentPage!=2)  
  34.     {  
  35.         TranslateAnimation ta=new TranslateAnimation(currentPage*single,single*2,0,0);  
  36.         ta.setFillAfter(true);  
  37.         ta.setDuration(200);  
  38.         i.startAnimation(ta);  
  39.     }  
  40.     currentPage=2;  
  41. }  


 

在这里,变量single表示移动一次的长度,从图中可以看出是图片的宽度加上offSet*2,这表示这个长度就是一个TextView的宽度。在这里为什么不获取TextView的宽度呢?原因是TextView与TextView之间是有宽度的,直接使用TextView的宽度的话不准。然后判断是哪一个TextView被点击了,再设置当前的界面,接着实现动画。如果点击的是当前的页面则不用实现动画,如果点击的页面不是当前的页面则从当前页面移动到指定的页面。TranslateAnimation动画的前两个参数是X方向的移动。动画完成后ImageView要停在最后的地点,所以要设置setFillAfter为true。

2.用户滑动界面时的反应

用户滑动界面的方法要对ViewPager设置监听,在onPageSelected方法中处理。

[java]   view plain copy
  1. public void onPageSelected(int arg0) {  
  2.         int single=b.getWidth()+offSet*2;  
  3.         TranslateAnimation ta=new TranslateAnimation(currentPage*single,single*arg0,0,0);  
  4.         ta.setFillAfter(true);  
  5.         ta.setDuration(200);  
  6.         i.startAnimation(ta);  
  7.           
  8.           
  9.         currentPage=arg0;  
  10.           
  11.     }  


在这个方法中的arg0是滑动之后的那个界面的标示。这里的代码与上面的差不多,还是实现一个动画。

 

 

 

下面贴出ViewPagerWorkActivity中的全部代码:

[java]   view plain copy
  1. package com.zhycheng.viewpagerwork;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.os.Bundle;  
  9. import android.os.Parcelable;  
  10. import android.support.v4.view.PagerAdapter;  
  11. import android.support.v4.view.ViewPager;  
  12. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  13. import android.view.Display;  
  14. import android.view.LayoutInflater;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.view.animation.TranslateAnimation;  
  18. import android.widget.ImageView;  
  19. import android.widget.TextView;  
  20.   
  21. public class ViewPagerWorkActivity extends Activity implements OnClickListener, OnPageChangeListener {  
  22.     TextView tv1,tv2,tv3;  
  23.     int currentPage=0;  
  24.     ViewPager vp;  
  25.     ArrayList al;  
  26.     Bitmap b;  
  27.     ImageView i;  
  28.     int disPlayWidth,offSet;  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.         i=(ImageView) findViewById(R.id.imageView1);  
  34.         Display dis=this.getWindowManager().getDefaultDisplay();  
  35.         disPlayWidth=dis.getWidth();  
  36.         b=BitmapFactory.decodeResource(this.getResources(),R.drawable.meitu);  
  37.         offSet=(disPlayWidth/3-b.getWidth())/2;  
  38.         tv1=(TextView) findViewById(R.id.textView1);  
  39.         tv2=(TextView) findViewById(R.id.textView2);  
  40.         tv3=(TextView) findViewById(R.id.textView3);  
  41.         vp=(ViewPager) findViewById(R.id.viewpager);  
  42.         tv1.setOnClickListener(this);  
  43.         tv2.setOnClickListener(this);  
  44.         tv3.setOnClickListener(this);  
  45.         al=new ArrayList();  
  46.         LayoutInflater li=LayoutInflater.from(this);  
  47.         al.add(li.inflate(R.layout.zyc1, null));  
  48.         al.add(li.inflate(R.layout.zyc2, null));  
  49.         al.add(li.inflate(R.layout.zyc3, null));  
  50.         PagerAdapter pa=new PagerAdapter(){  
  51.   
  52.             @Override  
  53.             public void destroyItem(View arg0, int arg1, Object arg2) {  
  54.                 // TODO Auto-generated method stub  
  55.                 ((ViewPager)arg0).removeView((View)al.get(arg1));  
  56.                   
  57.             }  
  58.   
  59.             @Override  
  60.             public void finishUpdate(View arg0) {  
  61.                 // TODO Auto-generated method stub  
  62.                   
  63.             }  
  64.   
  65.             @Override  
  66.             public int getCount() {  
  67.                 // TODO Auto-generated method stub  
  68.                 return al.size();  
  69.             }  
  70.   
  71.             @Override  
  72.             public Object instantiateItem(View arg0, int arg1) {  
  73.                 ((ViewPager)arg0).addView((View)al.get(arg1), 0);  
  74.                 return (View)al.get(arg1);  
  75.             }  
  76.   
  77.             @Override  
  78.             public boolean isViewFromObject(View arg0, Object arg1) {  
  79.                 // TODO Auto-generated method stub  
  80.                 return arg0==arg1;  
  81.             }  
  82.   
  83.             @Override  
  84.             public void restoreState(Parcelable arg0, ClassLoader arg1) {  
  85.                 // TODO Auto-generated method stub  
  86.                   
  87.             }  
  88.   
  89.             @Override  
  90.             public Parcelable saveState() {  
  91.                 // TODO Auto-generated method stub  
  92.                 return null;  
  93.             }  
  94.   
  95.             @Override  
  96.             public void startUpdate(View arg0) {  
  97.                 // TODO Auto-generated method stub  
  98.                   
  99.             }};  
  100.         vp.setAdapter(pa);  
  101.         vp.setCurrentItem(0);  
  102.         vp.setOnPageChangeListener(this);  
  103.           
  104.     }  
  105.     @Override  
  106.     public void onClick(View v) {  
  107.         int single=b.getWidth()+offSet*2;  
  108.           
  109.         if(v.getId()==R.id.textView1)  
  110.         {  
  111.               
  112.             vp.setCurrentItem(0);  
  113.             if(currentPage!=0)  
  114.             {  
  115.                 TranslateAnimation ta=new TranslateAnimation(currentPage*single,0,0,0);  
  116.                 ta.setFillAfter(true);  
  117.                 ta.setDuration(200);  
  118.                 i.startAnimation(ta);  
  119.             }  
  120.             currentPage=0;  
  121.         }  
  122.         if(v.getId()==R.id.textView2)  
  123.         {  
  124.             vp.setCurrentItem(1);  
  125.             if(currentPage!=1)  
  126.             {  
  127.                 TranslateAnimation ta=new TranslateAnimation(currentPage*single,single,0,0);  
  128.                 ta.setFillAfter(true);  
  129.                 ta.setDuration(200);  
  130.                 i.startAnimation(ta);  
  131.             }  
  132.             currentPage=1;  
  133.         }  
  134.         if(v.getId()==R.id.textView3)  
  135.         {  
  136.             vp.setCurrentItem(2);  
  137.             if(currentPage!=2)  
  138.             {  
  139.                 TranslateAnimation ta=new TranslateAnimation(currentPage*single,single*2,0,0);  
  140.                 ta.setFillAfter(true);  
  141.                 ta.setDuration(200);  
  142.                 i.startAnimation(ta);  
  143.             }  
  144.             currentPage=2;  
  145.         }  
  146.           
  147.           
  148.     }  
  149.     @Override  
  150.     public void onPageScrollStateChanged(int arg0) {  
  151.           
  152.     }  
  153.     @Override  
  154.     public void onPageScrolled(int arg0, float arg1, int arg2) {  
  155.           
  156.     }  
  157.     @Override  
  158.     public void onPageSelected(int arg0) {  
  159.         int single=b.getWidth()+offSet*2;  
  160.         TranslateAnimation ta=new TranslateAnimation(currentPage*single,single*arg0,0,0);  
  161.         ta.setFillAfter(true);  
  162.         ta.setDuration(200);  
  163.         i.startAnimation(ta);  
  164.           
  165.           
  166.         currentPage=arg0;  
  167.           
  168.     }  
  169. }  


工程代码下载:点击下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值