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

效果还是非常不错的,以下让我们看看是如何实现的:

看看文字来源,非常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 -- 滑动背景、透明列表的更多相关文章

  1. Android Webview 背景透明

    两个关键点: 1         fBarParams.format = PixelFormat.RGBA_8888; 2 mWebView.setBackgroundColor(Color.TRAN ...

  2. Android webview背景设置为透明无效 拖动时背景闪烁黑色

    Adndroid 2.X的设置 webview是一个使用方便.功能强大的控件,但由于webview的背景颜色默认是白色,在一些场合下会显得很突兀(比如背景是黑色). 此时就想到了要把webview的背 ...

  3. android 自定义Dialog背景透明及显示位置设置

    先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{ public SelectDialog ...

  4. android 设置Button或者ImageButton的背景透明 半透明 透明

    Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... />  透明 ...

  5. Android 设置按钮背景透明与半透明_图片背景透明

    Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... />  透明 ...

  6. android 设置背景为空(透明)

    在给控件设置背景时像ps那样的背景透明 在3.0以下可以使用 imageView.setBackgroundResource(android.R.id.empty); 但是这个方法在3.0以上会出现 ...

  7. Android设置窗体Activity背景透明

    背景透明 style.xml <item name="android:windowBackground">@color/transparent</item> ...

  8. Android UI Design

    Ref:直接拿来用!10款实用Android UI工具 Ref:Android UI设计资源 Ref:Android酷炫实用的开源框架(UI框架) Ref:Android UI 组件 Ref:Andr ...

  9. android 自己定义dialog并实现失去焦点(背景透明)的功能

    前言:因为在项目中须要用到更新显示动画的需求,所以想到了dialog,自己定义dialog不难.网上教程非常多,可是在实现dialog背景透明的需求时,遇到了一点问题.网上的一些方法在我的机器上并没有 ...

随机推荐

  1. vue实现tab栏切换

    html <ul class="tab"> <li v-for="(item,index) in tabs" @click="tab ...

  2. 操作Map

    ///操作Map Map<String,Object> userInfo = new HashMap(); userInfo.put("uid", adUserEnti ...

  3. redis.conf配置文件配置项解析

    知识来源于 : https://blog.csdn.net/bsfz_2018/article/details/79061413[Redis在linux下的安装] daemonize:如需要在后台运行 ...

  4. 【转载】spring boot 链接 虚拟机(Linux) redis

    原文:https://www.imooc.com/article/43279?block_id=tuijian_wz 前提是你已经安装redis且支持远程连接,redis的安装这里不再赘述,有需要的可 ...

  5. php7 memcache和memcached.so扩展

    php7安装memcache和memcached扩展 https://github.com/websupport-sk/pecl-memcache https://github.com/php-mem ...

  6. python学习笔记:第四天

    day04: 一.计算求值 num += 1 等价于 num = num + 1num -= 1 等价于 num = num - 1num *= 2 等价于 num = num * 2num /= 2 ...

  7. 洛谷 P4147 玉蟾宫 (最大子矩形问题)

    这道题用到了悬线法,非常牛逼,可以看这个论文. https://blog.csdn.net/twtsa/article/details/8120269 #include<cstdio> # ...

  8. 紫书 例题11-4 UVa247 (Floyd判断联通)

    Floyd联通, 然后为了输出联通分量而新建一个图, 让互相可以打电话的建立一条边, 然后dfs输出联通分量就ok了. #include<cstdio> #include<iostr ...

  9. WPF 封装 dotnet remoting 调用其他进程

    原文:WPF 封装 dotnet remoting 调用其他进程 本文告诉大家一个封装好的库,使用这个库可以快速搭建多进程相互使用. 目录 创建端口 调用软件 运行的类 运行C++程序 通道 使用 在 ...

  10. Memcached存储溢出

    Memcached存储溢出 测试数据生成程序: package com.stoon.test; public class TestFor { public static void main(Strin ...