Pretty UI Design For Android -- 滑动背景、透明列表
2016-11-10 17:49
417 查看
本文是从国外一个网上看到的效果,感觉很不错,就简化了一下代码,拿来用了,先看下效果图:
效果还是很不错的,下面让我们看看是如何实现的:
看看文字来源,很简单,是一个数组:
[html] view
plain copy
print?
<?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>
布局也很简单:
[html] view
plain copy
print?
<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下面我们来看看程序是怎样的:
[java] view
plain copy
print?
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,相信大家会比较疑惑,让我们来看看他是什么:
[java] view
plain copy
print?
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
这时候大家再看代码就很清楚了吧,效果还是很赞的~
以上。
效果还是很不错的,下面让我们看看是如何实现的:
看看文字来源,很简单,是一个数组:
[html] view
plain copy
print?
<?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>
布局也很简单:
[html] view
plain copy
print?
<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下面我们来看看程序是怎样的:
[java] view
plain copy
print?
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,相信大家会比较疑惑,让我们来看看他是什么:
[java] view
plain copy
print?
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 -- 滑动背景、透明列表
- Pretty UI Design For Android -- 滑动背景、透明列表
- 转niutao:android 4.1.2 launcher2实现应用列表背景透明
- Photoshop制作Android UI: 如何将图片背景变为透明
- android Launcher3应用列表修改透明背景
- Android开发实现Launcher3应用列表修改透明背景的方法
- Android launcher2 应用列表背景透明/透明壁纸
- android 4.1.2 launcher2实现应用列表背景透明
- android在滑动控件过程中动态设置view背景透明的问题
- Photoshop制作Android UI: 怎样将图片背景变为透明
- Android Design ItemTouchHelper实现酷炫列表的移行和滑动删除效果
- android 4.1.2 launcher2实现像4.4launcher3应用列表背景透明的效果
- android 4.1.2 launcher2实现应用列表背景透明
- android优化--滑动ListView列表时背景变黑的解决方法
- First class: UI design for Android
- Android ListView 滑动背景为黑色的解决办法
- android 设置Button或者ImageButton的背景透明 半透明
- Android UI Design Tips(Google官方出品:Android UI 设计指导)——第一部分
- Android UI Design Tips(Google官方出品:Android UI 设计指导)——第一部分
- android 自定义Dialog背景透明及显示位置设置