滑动渐变
来源:互联网 发布:dating付费软件靠谱吗 编辑:程序博客网 时间:2024/04/26 07:54
本文章是我通过参考一篇文章改写的,很久之前了,所以忘记参考谁的了,感谢那位大神。
全部JAVA代码
import android.graphics.Color;import android.os.Bundle;import android.os.Handler;import android.support.v7.app.ActionBarActivity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import com.senzhi.mysupertest.R;/** * Created by zz on 2017/2/1. */public class ActionBarShadeActivity extends ActionBarActivity { private ListView lv;//listview的header private ImageView headerView; private ImageView iv_search; float alpha; int height; View atView;//需要跟随listview滑动渐变的ActionBar private View alphaTitleBar; private TextView tv_title; private TextView tv_alpha; private CommentListAdapter commentAdapter; private Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { tv_alpha.setText(atView.getTop()+" "+height+" "+alpha); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_actionbar_shade); alphaTitleBar = findViewById(R.id.title_bar); tv_title = (TextView) findViewById(R.id.tv_title); tv_alpha = (TextView) findViewById(R.id.tv_alpha); iv_search = (ImageView) findViewById(R.id.iv_search); lv = (ListView) findViewById(R.id.lv);// commentList = new ArrayList<>(); commentAdapter = new CommentListAdapter(); headerView = new ImageView(this); headerView.setBackgroundColor(Color.BLACK); headerView.setLayoutParams(new AbsListView.LayoutParams( AbsListView.LayoutParams.MATCH_PARENT, 600)); lv.addHeaderView(headerView); lv.setAdapter(commentAdapter);//设置listview的监听滑动的事件 lv.setOnScrollListener((AbsListView.OnScrollListener) new AlphaListener()); } private class AlphaListener implements AbsListView.OnScrollListener { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if(view.getChildCount() == 0) return; if(firstVisibleItem == 0){ atView = view.getChildAt(0); height = atView.getHeight(); if(-atView.getTop() < height){ alpha = ( Math.abs(atView.getTop())) / (alphaTitleBar.getHeight() * 4f); alphaTitleBar.setBackgroundColor(Color.rgb(0,0,0));// } if(Math.abs(atView.getTop()) >height/2){ alphaTitleBar.setBackgroundColor(Color.rgb(255,255,255)); alphaTitleBar.setAlpha(Math.abs((float) (alpha-0.4)*2)); tv_title.setTextColor(Color.BLACK); iv_search.setImageResource(R.drawable.searchblack); }else{ alphaTitleBar.setAlpha(Math.abs(1-(float) (alpha-0.1)*2)); tv_title.setTextColor(Color.WHITE);// alphaTitleBar.setBackgroundColor(Color.rgb(0,0,0)); iv_search.setImageResource(R.drawable.searchwhite); } } new Thread(){ @Override public void run() { try{ handler.postDelayed(runnable,20); }catch (Exception e){ } } }.start(); } if(firstVisibleItem == 1) alphaTitleBar.setAlpha(1); } } public class CommentListAdapter extends BaseAdapter { CommentViewHolder holder; @Override public int getCount() { return 20; } @Override public Object getItem(int i) { return 20; } @Override public long getItemId(int i) { return i; } @Override public View getView(int position, View convertView, ViewGroup viewGroup) { if(convertView == null){ holder = new CommentViewHolder(); convertView = LayoutInflater.from(ActionBarShadeActivity.this).inflate(R.layout.person_mycomment_comment_adapter,null); holder.user_head = (ImageView) convertView.findViewById(R.id.user_head); holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name); holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content); holder.tv_source = (TextView) convertView.findViewById(R.id.tv_source); holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time); holder.tv_trumbup = (TextView) convertView.findViewById(R.id.tv_trumbup); holder.tv_comment = (TextView) convertView.findViewById(R.id.tv_comment); convertView.setTag(holder); }else{ holder = (CommentViewHolder) convertView.getTag(); } return convertView; } } public class CommentViewHolder{ ImageView user_head; TextView tv_name; TextView tv_content; TextView tv_source; TextView tv_time; TextView tv_trumbup; TextView tv_comment; }}
页面布局xml代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container_view" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#3000" android:id="@+id/lv"></ListView> <RelativeLayout android:id="@+id/title_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:text="TITLE" android:layout_centerInParent="true" android:textSize="20sp"/> <ImageView android:id="@+id/iv_search" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/searchwhite" android:padding="10dp" android:layout_alignParentRight="true" /> </RelativeLayout> <TextView android:id="@+id/tv_alpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="alpha" android:layout_centerInParent="true" /></RelativeLayout>
listView的适配器布局代码就不贴了,listView可以自定义,随便定义一个简单的就行了,愿谅解。
实现原理:
通过监听listView的滑动事件获取到每次滑动listView时,titlebar所在页面的高度,计算出渐变值并给titlebar设置透明度实现滑动渐变。
很简单的一个东西,发挥自己的想象,可以实现很厉害的效果。
阅读全文
0 0
- 滑动渐变
- 解锁滑动渐变动画
- android 滑动listview ActionBar渐变
- android标题栏滑动渐变效果
- jQuery 实现鼠标滑动表格渐变行
- iOS绘制渐变背景滑动条
- TextView+View渐变,viewpager滑动的时候
- android viewPager 滑动标题颜色渐变效果
- 微信滑动底部菜单渐变效果
- 自定义Android渐变式圆环滑动条
- 标题栏滑动渐变效果的实现
- navigationBar随着tableview滑动透明度渐变
- ScrollView滑动实现标题栏透明度随之渐变
- Android Listview滑动及渐变动画
- ScrollView滑动时隐藏头部颜色渐变
- RecyclerView滑动时使toolsbar渐变
- Android 使用Scroller 实现渐变式滑动
- Android ViewPager 和PageTransformer滑动颜色渐变
- pymongo报错 pymongo.errors.OperationFailure: Authentication failed.
- 如何写好总结助力向上管理
- Cadence 中原理图的绘制流程
- Self referencing loop detected for property
- Java RTTI -- JAVA 进阶
- 滑动渐变
- 父子进程之间的关系
- 前端开发细节-2
- react 真机不出现开发者菜单 不能reload
- js数组冒泡排序
- TCP/IP协议 笔试题和面试题
- C语言(Head First C)-9_1:静态库与动态库:静态库
- Nginx安装教程
- cocos2dx 《忍着飞镖射幽灵》小结