Pretty UI Design For Android -- 滑动背景、透明列表
本文是从国外一个网上看到的效果。感觉非常不错。就简化了一下代码。拿来用了,先看下效果图:
效果还是非常不错的,以下让我们看看是如何实现的:
看看文字来源,非常easy,是一个数组:
<?xml version="1.0" encoding="utf-8"? >
<resources> <string-array name="list_content">
<item>If I could save time in a bottle </item>
<item>the first thing that I\'d like to do </item>
<item>is to save every day until eternity passes away </item>
<item>just to spend them with you </item>
<item>If I could save time in a bottle </item>
<item>the first thing that I\'d like to do </item>
<item>is to save every day until eternity passes away </item>
<item>just to spend them with you </item>
<item>If I could make days last forever </item>
<item>if words could make wishes come true </item>
<item>I\'d save every day like a treasure and then </item>
<item>again I would spend them with you </item>
<item>Thank you for comforting me when I\'m sad </item>
<item>Loving me when I\'m mad </item>
<item>Picking me up when I\'m down </item>
<item>Thank you for being my friend and being around </item>
<item>Teaching me the meaning of love </item>
<item>Encouraging me when I need a shove </item>
<item>But most of all thank you for </item>
<item>Loving me for who I am </item>
</string-array> </resources>
布局也非常easy:
<FrameLayout 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"> <com.example.scrolltest.TopCenterImageView
android:id="@+id/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image" /> <ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"/> </FrameLayout>
由于我们是用的一个listview来显示的。所以这样做就是最简单的了。
ok以下我们来看看程序是如何的:
package com.example.scrolltest; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity { private TopCenterImageView bg;
private ListView list;
private View head; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); bg = (TopCenterImageView) findViewById(R.id.bg);
list = (ListView) findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
getResources().getStringArray(R.array.list_content))); head = new View(this);
head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 700));
list.addHeaderView(head); list.setOnScrollListener(new OnScrollListener() { @Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
} @Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int top = head.getTop() / 2;
bg.setTop(top);
}
});
}
}
当中有一个TopCenterImageView。相信大家会比較疑惑,让我们来看看他是什么:
package com.example.scrolltest; import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.widget.ImageView; /**
* Custom view allowing an image to be displayed with a "top crop" scale type
*
* @author Nicolas POMEPUY
*
*/
public class TopCenterImageView extends ImageView { public TopCenterImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setScaleType(ScaleType.MATRIX);
} public TopCenterImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setScaleType(ScaleType.MATRIX);
} public TopCenterImageView(Context context) {
super(context);
setScaleType(ScaleType.MATRIX);
} /**
* Top crop scale type
*/
@Override
protected boolean setFrame(int l, int t, int r, int b) {
if (getDrawable() == null) {
return super.setFrame(l, t, r, b);
}
Matrix matrix = getImageMatrix();
float scaleFactor = getWidth() / (float) getDrawable().getIntrinsicWidth();
matrix.setScale(scaleFactor, scaleFactor);
setImageMatrix(matrix);
return super.setFrame(l, t, r, b);
} }
这个重写的ImageView是为了可以设置ImageView的大小。让他符合我们的背景。凝视写的非常清楚:Custom view allowing an image to be displayed with a "top crop" scale type
这时候大家再看代码就非常清楚了吧,效果还是非常赞的~
以上。
Pretty UI Design For Android -- 滑动背景、透明列表的更多相关文章
- Android Webview 背景透明
两个关键点: 1 fBarParams.format = PixelFormat.RGBA_8888; 2 mWebView.setBackgroundColor(Color.TRAN ...
- Android webview背景设置为透明无效 拖动时背景闪烁黑色
Adndroid 2.X的设置 webview是一个使用方便.功能强大的控件,但由于webview的背景颜色默认是白色,在一些场合下会显得很突兀(比如背景是黑色). 此时就想到了要把webview的背 ...
- android 自定义Dialog背景透明及显示位置设置
先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{ public SelectDialog ...
- android 设置Button或者ImageButton的背景透明 半透明 透明
Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... /> 透明 ...
- Android 设置按钮背景透明与半透明_图片背景透明
Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... /> 透明 ...
- android 设置背景为空(透明)
在给控件设置背景时像ps那样的背景透明 在3.0以下可以使用 imageView.setBackgroundResource(android.R.id.empty); 但是这个方法在3.0以上会出现 ...
- Android设置窗体Activity背景透明
背景透明 style.xml <item name="android:windowBackground">@color/transparent</item> ...
- Android UI Design
Ref:直接拿来用!10款实用Android UI工具 Ref:Android UI设计资源 Ref:Android酷炫实用的开源框架(UI框架) Ref:Android UI 组件 Ref:Andr ...
- android 自己定义dialog并实现失去焦点(背景透明)的功能
前言:因为在项目中须要用到更新显示动画的需求,所以想到了dialog,自己定义dialog不难.网上教程非常多,可是在实现dialog背景透明的需求时,遇到了一点问题.网上的一些方法在我的机器上并没有 ...
随机推荐
- 服务器搭建域控与SQL Server的AlwaysOn环境过程(一) 搭建域控服务器
0 准备阶段 1. Windows Server 服务器 3台(其中域控服务器配置可降低一个水准,目前博主试用的是:域控服务器--2核4G 数据库服务器(节点)--4核8G ) 2. SQL Serv ...
- NodeJS学习笔记 (9)网络服务-https(ok)
模块概览 这个模块的重要性,基本不用强调了.在网络安全问题日益严峻的今天,网站采用HTTPS是个必然的趋势. 在nodejs中,提供了 https 这个模块来完成 HTTPS 相关功能.从官方文档来看 ...
- [SHOI2012]魔法树
题目:洛谷P3833. 题目大意:给你一棵树,有两种操作:1.给两个点和它们之间的最短路上的所有点加上一个值:2.询问以某个点为根的子树的子树和.你需要实现这个功能. 解题思路:如果只有最后才询问的话 ...
- maven 安装jar包
1 下载maven: 下载路径: http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-b ...
- c 和 指针读书笔记(1)
1.字符串常量:出现在表达式中,其值是一个指针 "abc" + 1; //b 2.回掉函数:就是把函数的作为参数传入父函数,函数本身就是一个地址,传址肯定没问题.至于父函数是想用函 ...
- WPF 获取应用的所有窗口
原文:WPF 获取应用的所有窗口 本文告诉大家如何获取应用内的所有窗口,无论这些窗口有没显示 在 WPF 可以通过 Application.Current.Windows 列举应用的所有窗口 fore ...
- Object-C,数组NSArray
晚上回来,写了2个iOS应用程序. 就是在界面中,展示标签.一种是手动构造界面,然后绑定事件.另外一种是,使用自带的界面作为容器,但是手动向里面放其它界面元素. 书中的观点是,使用图形化界面,构造界面 ...
- 【Henu ACM Round#24 A】k-String
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果是一个k-string的话. 考虑最后的串假设形式为sss..ss(k个s) 则s中出现的字母,整个串中最后出现的次数肯定为k的 ...
- BeautifulSoup的高级应用 之.parent .parents .next_sibling.previous_sibling.next_siblings.previous_siblings
继上一篇BeautifulSoup的高级应用,主要解说的是contents children descendants string strings stripped_strings.本篇主要解说.pa ...
- JBOSS部署项目之后,无法通过IP地址訪问,仅仅能通过localhost或者127.0.0.1訪问
这几天入职到了一家新的公司,然后第一天就開始搭建各种环境.由于原先一直用的是Tomcat容器,然后也是第一次接触JBOSS容器,搭建完之后,在MyEclipse中启动了JBOSS容器,然后想在浏览器中 ...