最近QQ更新了qq空间消息列表。天天动听也使用了那效果。而且越来越多的android应用都在使用,咱们不能落后啊!
得紧跟时代的步伐,不然就out了。 看起来不错分享下咯!
首先贴一下效果图,因为gif动态图片不懂怎么搞。下次一定改进。
贴下代码先:
自定义了滑动时背景颜色渐变
- package com.seven.fadingdemo;
- import com.seven.fadingactionbardemo.R;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.graphics.drawable.Drawable;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- /**
- * 定义的控件布局
- *
- * @author seven2729
- */
- public class FadingScrollView extends LinearLayout {
- private LinearLayout mActionBar;
- private Drawable mBgDrawable;
- private ImageView fadingBar;// title渐变的控件
- private int fadingHeight; // 可隐藏的控件高度
- private int oldY;
- private int fadingOffset;
- public static final int ALPHA_START = 20;
- public static final int ALPHA_END = 255;
- public FadingScrollView(Context context) {
- this(context, null);
- }
- public FadingScrollView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
- @SuppressLint("NewApi")
- public FadingScrollView(Context context, AttributeSet attrs,
- int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- setOrientation(VERTICAL);
- }
- @Override
- protected void onFinishInflate() {
- super.onFinishInflate();
- fadingBar = (ImageView) findViewById(R.id.fading_bar);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- fadingHeight = fadingBar.getMeasuredHeight() - fadingOffset;// title渐变的控件距离
- }
- @SuppressLint("ClickableViewAccessibility")
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- switch (ev.getAction()) {
- case MotionEvent.ACTION_DOWN:
- oldY = (int) ev.getY();
- break;
- case MotionEvent.ACTION_MOVE:
- int scrollY = getScrollY();
- int y = (int) ev.getY();
- int deltaY = y - oldY;
- int willScrollY = scrollY - deltaY;
- if (willScrollY > fadingHeight)
- {// 不做操作颜色保持不变
- // willScrollY = fadingHeight;
- }
- else
- {// 颜色渐变
- updateActionBarAlpha(willScrollY * (ALPHA_END - ALPHA_START)
- / fadingHeight + ALPHA_START);
- }
- if (willScrollY < 0) {
- willScrollY = 0;
- }
- scrollTo(0, willScrollY);
- oldY = y;
- break;
- case MotionEvent.ACTION_UP:
- break;
- }
- return true;
- }
- public void bindingActionBar(LinearLayout actionBar) {
- mActionBar = actionBar;
- }
- @SuppressWarnings("deprecation")
- public void setActionBarBgDrawable(Drawable bgDrawable) {
- if (mActionBar == null) {
- try {
- throw new Exception(
- "Please try to binding the actionBar before set it's background.");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- mBgDrawable = bgDrawable;
- mBgDrawable.setAlpha(ALPHA_START);
- mActionBar.setBackgroundDrawable(mBgDrawable);
- }
- @SuppressWarnings("deprecation")
- public void setActionBarAlpha(int alpha) throws Exception {
- if (mActionBar == null || mBgDrawable == null) {
- throw new Exception(
- "acitonBar is not binding or bgDrawable is not set.");
- }
- mBgDrawable.setAlpha(alpha);
- mActionBar.setBackgroundDrawable(mBgDrawable);
- }
- void updateActionBarAlpha(int alpha) {
- try {
- setActionBarAlpha(alpha);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void setFadingOffset(int height) {
- fadingOffset = height;
- }
- }
然后就是代码的使用了,根据自己的需要设置大小或者颜色。
- package com.seven.fadingdemo;
- import com.seven.fadingactionbardemo.R;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.graphics.drawable.ColorDrawable;
- import android.os.Bundle;
- import android.widget.LinearLayout;
- /**
- * 主页展示页面
- *
- * @author seven2729
- */
- public class MainActivity extends Activity {
- private LinearLayout mLayout;
- private FadingScrollView fadingScrollView;// 自定义滑动布局
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- fadingScrollView = (FadingScrollView) findViewById(R.id.root);
- initLinearLayout();
- }
- /**
- * 布局初始化
- */
- @SuppressLint({ "Recycle", "InlinedApi" })
- void initLinearLayout() {
- //初始化fadingScrollView的一些信息
- mLayout = (LinearLayout) findViewById(R.id.lyout_title);
- ColorDrawable bgDrawable = new ColorDrawable(getResources().getColor(
- R.color.color_blue));
- fadingScrollView.bindingActionBar(mLayout);// 设置渐变布局
- fadingScrollView.setActionBarBgDrawable(bgDrawable);// 设置渐变颜色
- }
- }
颜色值的代码:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <color name="white">#FFFFFF</color>
- <color name="black">#000000</color>
- <color name="transparent">#00000000</color>
- <color name="color_blue">#50BDFB</color>
- </resources>
因为最近有在研究,所以找点资料自己修改了。改成自己需要的
如有需要可以 下载源码
如源码有偏差请根据博文修改!
欢迎转载,转载请注明出处:http://blog.csdn.net/seven2729/article/details/48372851