Pretty UI Design For Android -- 滑动背景、透明列表

来源:互联网 发布:软件研发任务分配 编辑:程序博客网 时间:2024/04/28 10:13

本文是从国外一个网上看到的效果,感觉很不错,就简化了一下代码,拿来用了,先看下效果图:


效果还是很不错的,下面让我们看看是如何实现的:

看看文字来源,很简单,是一个数组:

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string-array name="list_content">  
  5.         <item>If I could save time in a bottle </item>  
  6.         <item>the first thing that I\'d like to do </item>  
  7.         <item>is to save every day until eternity passes away </item>  
  8.         <item>just to spend them with you </item>  
  9.         <item>If I could save time in a bottle </item>  
  10.         <item>the first thing that I\'d like to do </item>  
  11.         <item>is to save every day until eternity passes away </item>  
  12.         <item>just to spend them with you </item>  
  13.         <item>If I could make days last forever </item>  
  14.         <item>if words could make wishes come true </item>  
  15.         <item>I\'d save every day like a treasure and then </item>  
  16.         <item>again I would spend them with you  </item>  
  17.         <item>Thank you for comforting me when I\'m sad </item>  
  18.         <item>Loving me when I\'m mad </item>  
  19.         <item>Picking me up when I\'m down </item>  
  20.         <item>Thank you for being my friend and being around </item>  
  21.         <item>Teaching me the meaning of love </item>  
  22.         <item>Encouraging me when I need a shove </item>  
  23.         <item>But most of all thank you for  </item>  
  24.         <item>Loving me for who I am  </item>  
  25.     </string-array>  
  26.   
  27. </resources>  


布局也很简单:

[html] view plain copy
 print?
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <com.example.scrolltest.TopCenterImageView  
  7.         android:id="@+id/bg"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:src="@drawable/image" />  
  11.   
  12.     <ListView  
  13.         android:id="@+id/list"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="match_parent"   
  16.         android:divider="@null"/>  
  17.   
  18. </FrameLayout>  

因为我们是用的一个listview来显示的,所以这样做就是最简单的了。


ok下面我们来看看程序是怎样的:

[java] view plain copy
 print?
  1. package com.example.scrolltest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.AbsListView;  
  7. import android.widget.AbsListView.LayoutParams;  
  8. import android.widget.AbsListView.OnScrollListener;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.ListView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     private TopCenterImageView bg;  
  15.     private ListView list;  
  16.     private View head;  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.   
  23.         bg = (TopCenterImageView) findViewById(R.id.bg);  
  24.         list = (ListView) findViewById(R.id.list);  
  25.         list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,  
  26.                 getResources().getStringArray(R.array.list_content)));  
  27.   
  28.         head = new View(this);  
  29.         head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 700));  
  30.         list.addHeaderView(head);  
  31.   
  32.         list.setOnScrollListener(new OnScrollListener() {  
  33.   
  34.             @Override  
  35.             public void onScrollStateChanged(AbsListView view, int scrollState) {  
  36.             }  
  37.   
  38.             @Override  
  39.             public void onScroll(AbsListView view, int firstVisibleItem,  
  40.                     int visibleItemCount, int totalItemCount) {  
  41.                 int top = head.getTop() / 2;  
  42.                 bg.setTop(top);  
  43.             }  
  44.         });  
  45.     }  
  46. }  

其中有一个TopCenterImageView,相信大家会比较疑惑,让我们来看看他是什么:

[java] view plain copy
 print?
  1. package com.example.scrolltest;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Matrix;  
  5. import android.util.AttributeSet;  
  6. import android.widget.ImageView;  
  7.   
  8. /** 
  9.  * Custom view allowing an image to be displayed with a "top crop" scale type 
  10.  *  
  11.  * @author Nicolas POMEPUY 
  12.  *  
  13.  */  
  14. public class TopCenterImageView extends ImageView {  
  15.   
  16.     public TopCenterImageView(Context context, AttributeSet attrs, int defStyle) {  
  17.         super(context, attrs, defStyle);  
  18.         setScaleType(ScaleType.MATRIX);  
  19.     }  
  20.   
  21.     public TopCenterImageView(Context context, AttributeSet attrs) {  
  22.         super(context, attrs);  
  23.         setScaleType(ScaleType.MATRIX);  
  24.     }  
  25.   
  26.     public TopCenterImageView(Context context) {  
  27.         super(context);  
  28.         setScaleType(ScaleType.MATRIX);  
  29.     }  
  30.   
  31.     /** 
  32.      * Top crop scale type 
  33.      */  
  34.     @Override  
  35.     protected boolean setFrame(int l, int t, int r, int b) {  
  36.         if (getDrawable() == null) {  
  37.             return super.setFrame(l, t, r, b);  
  38.         }  
  39.         Matrix matrix = getImageMatrix();  
  40.         float scaleFactor = getWidth() / (float) getDrawable().getIntrinsicWidth();  
  41.         matrix.setScale(scaleFactor, scaleFactor);  
  42.         setImageMatrix(matrix);  
  43.         return super.setFrame(l, t, r, b);  
  44.     }  
  45.   
  46. }  

这个重写的ImageView是为了能够设置ImageView的大小,让他符合我们的背景,注释写的很清楚:Custom view allowing an image to be displayed with a "top crop" scale type

这时候大家再看代码就很清楚了吧,效果还是很赞的~


以上。

0 0
原创粉丝点击