首先申明,这个自定义View不是本人写的,大神写的,本人仅限学习一级研究使用

直接上代码吧!后面我再解释一遍

  1 package com.egojit.android.gcoa.views;
  2
  3 import android.animation.Animator;
  4 import android.animation.TypeEvaluator;
  5 import android.animation.ValueAnimator;
  6 import android.content.Context;
  7 import android.graphics.Canvas;
  8 import android.graphics.Color;
  9 import android.graphics.Paint;
 10 import android.graphics.Point;
 11 import android.os.Handler;
 12 import android.os.Message;
 13 import android.support.annotation.Nullable;
 14 import android.util.AttributeSet;
 15 import android.util.DisplayMetrics;
 16 import android.view.View;
 17 import android.view.WindowManager;
 18
 19 import com.asuka.android.asukaandroid.comm.utils.LogUtil;
 20 import com.asuka.android.asukaandroid.view.EmptyView;
 21 import com.egojit.android.gcoa.model.EmptyModel;
 22
 23 import java.util.List;
 24
 25 /**
 26  * Created by Administrator on 2017/4/18 0018.
 27  */
 28
 29 public class CustomScorllView extends View {
 30     private  Context mContext;
 31     private boolean hasStart=false;
 32     private List<EmptyModel> sroucesList;
 33     private double strHeight;
 34     private Point startPoint;
 35     private Point endPoint;
 36     private boolean stop;
 37
 38     public void setSroucesList(List<EmptyModel> sroucesList) {
 39         this.sroucesList = sroucesList;
 40         type=getCurType();
 41         title=getCurTitle();
 42         invalidate();
 43     }
 44
 45     public CustomScorllView(Context context) {
 46         super(context);
 47         mContext=context;
 48     }
 49
 50     public CustomScorllView(Context context, @Nullable AttributeSet attrs) {
 51         super(context, attrs);
 52         mContext=context;
 53     }
 54
 55     public CustomScorllView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
 56         super(context, attrs, defStyleAttr);
 57         mContext=context;
 58     }
 59
 60     public CustomScorllView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
 61         super(context, attrs, defStyleAttr, defStyleRes);
 62         mContext=context;
 63     }
 64
 65     private void startScorll() {
 66         int   tempY=y;
 67         Message message=new Message();
 68         message.obj=tempY;
 69         handler.sendMessage(message);
 70     }
 71     Handler handler=new Handler(){
 72         @Override
 73         public void handleMessage(Message msg) {
 74             super.handleMessage(msg);
 75              int tempY= (int) msg.obj;
 76                 y=tempY;
 77
 78              if (tempY>-50){
 79                  invalidate();
 80                  tempY=tempY-1;
 81                  Message message=new Message();
 82                  message.obj=tempY;
 83                  handler.sendMessage(message);
 84              }
 85         }
 86     };
 87     int start,end;
 88     public void start(){
 89         Point startPoint = new Point(start, 50);
 90         Point endPoint = new Point(end, 50);
 91         final ValueAnimator animator = ValueAnimator
 92                 .ofObject(new PointEvaluator(), startPoint, endPoint);
 93         animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
 94             @Override
 95             public void onAnimationUpdate(ValueAnimator animation) {
 96                 Point mPoint = ((Point) animation.getAnimatedValue());
 97                 y=mPoint.x;
 98                 invalidate();
 99             }
100         });
101         animator.addListener(new Animator.AnimatorListener() {
102             @Override
103             public void onAnimationStart(Animator animation) {
104
105             }
106
107             @Override
108             public void onAnimationEnd(Animator animation) {
109                 //下一个显示
110
111                 if (  start==baseline){
112                     start= (int) (-strHeight/2);
113                     end=baseline;
114                     EmptyModel model=sroucesList.get(0);
115                     sroucesList.remove(0);
116                     sroucesList.add(model);
117                     type=getCurType();
118                     title=getCurTitle();
119                     start();
120                 }else {
121                     start=baseline;
122                     end= (int) (getMeasuredHeight()+strHeight/2);
123                     new Handler().postDelayed(new Runnable() {
124                         @Override
125                         public void run() {
126                             start();
127                         }
128                     },4000);
129                 }
130
131
132             }
133
134             @Override
135             public void onAnimationCancel(Animator animation) {
136
137             }
138
139             @Override
140             public void onAnimationRepeat(Animator animation) {
141
142             }
143         });
144         animator.setDuration(3000);
145         animator.start();
146     }
147     public void startAnimation() {
148        new Handler().postDelayed(new Runnable() {
149            @Override
150            public void run() {
151                hasStart=true;
152                type=getCurType();
153                title=getCurTitle();
154                start=baseline;
155                end= (int) (getMeasuredHeight()+strHeight/2);
156                 startPoint = new Point(start, 50);
157                 endPoint = new Point(end, 50);
158                start();
159            }
160        },4000);
161     }
162
163     private String getCurTitle() {
164         return sroucesList.get(0).getVar1();
165     }
166
167     private String getCurType() {
168         return sroucesList.get(0).getVar();
169     }
170
171     private String getNextTitle() {
172         if (sroucesList.size()==1){
173             return sroucesList.get(0).getVar1();
174         }else {
175             return sroucesList.get(1).getVar1();
176         }
177
178     }
179
180     private String getNextType() {
181         if (sroucesList.size()==1){
182             return sroucesList.get(0).getVar();
183         }else {
184             return sroucesList.get(1).getVar();
185         }
186     }
187
188     int baseline=-999;
189     int y;
190     String type="哈哈哈";
191     String title="个囊女女过过过过";
192     @Override
193     protected void onDraw(Canvas canvas) {
194         super.onDraw(canvas);
195
196         Paint redPaint = new Paint();
197         redPaint.setAntiAlias(true);
198         redPaint.setTextSize(gettextSize(45));
199         redPaint.setColor(0xff50a3ef);
200         redPaint.setTextAlign(Paint.Align.LEFT);
201
202         Paint blckPaint = new Paint();
203         blckPaint.setAntiAlias(true);
204         blckPaint.setTextSize(gettextSize(45));
205         blckPaint.setColor(Color.BLACK);
206         blckPaint.setTextAlign(Paint.Align.LEFT);
207
208         Paint.FontMetricsInt fontMetrics = redPaint.getFontMetricsInt();
209         String typeStr="[" + type + "]";
210         //直接返回参数字符串所占用的宽度
211         float strWidth = redPaint.measureText(typeStr);
212         strHeight=getTxtHeight(redPaint)+15;
213         if (baseline==-999&&sroucesList!=null){
214             baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
215             y=baseline;
216         }
217 //        LogUtil.e("-----------------handler---------"+y);
218         canvas.drawText(typeStr, 30  , y  , redPaint);
219         canvas.drawText(title, strWidth+60  , y  , blckPaint);
220
221     }
222     public double getTxtHeight(Paint mPaint) {
223         Paint.FontMetrics fm = mPaint.getFontMetrics();
224         return Math.ceil(fm.descent - fm.ascent);
225     }
226     public class PointEvaluator implements TypeEvaluator<Point> {
227         @Override
228         public Point evaluate(float fraction, Point startValue, Point endValue) {
229 //            LogUtil.e("-----------------fraction---------"+fraction);
230             int resultX = (int) (fraction * (endValue.x-startValue.x)+startValue.x);
231             int resultY = (int) (fraction * startValue.y);
232             return new Point(resultX, resultY);
233         }
234     }
235
236     @Override
237     public void setOnClickListener(@Nullable OnClickListener l) {
238         super.setOnClickListener(l);
239
240     }
241
242     public List<EmptyModel> getSroucesList() {
243         return sroucesList;
244     }
245
246     public float gettextSize(float f){
247
248
249         int screenWidth =getScreenWidth(mContext);
250
251         float ratioWidth = (float)screenWidth / 1080;
252
253
254         return Math.round(f * ratioWidth);
255     }
256
257     /**
258      * 获取屏幕宽度
259      *
260      * @return
261      */
262     @SuppressWarnings("deprecation")
263     public  int getScreenWidth(Context context) {
264         return ((WindowManager) context
265                 .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
266                 .getWidth();
267     }
268
269 }

主界面布局为

1  <com.wangy.views.CustomScorllView
2                     android:id="@+id/custom_scorllView"
3                     android:layout_width="match_parent"
4                     android:layout_height="match_parent"></com.wany.views.CustomScorllView>

那么如何使用呢!

使用起来就很简单了

 1  List<EmptyModel> alist=new ArrayList<>();
 2         for (int i=0;i<newsList.size();i++){
 3             JSONObject object=newsList.getJSONObject(i);
 4             EmptyModel model=new EmptyModel();
 5             String fileType=object.getString("fileType");
 6             String title=object.getString("title");
 7             String id=object.getString("id");
 8             model.setVar(fileType);
 9             model.setVar1(title);
10             model.setVar2(id);
11             alist.add(model);
12         }
13         if (alist.size()==0)return;
14         custom_scorllView.setSroucesList(alist);
15         custom_scorllView.startAnimation();

其中的newsList为列表数据,你可以从接口中取也可以put值,

EmptyModel为一个空model,他是用来直接设置接口参数形成一个model,这样使用起来比较方便

  1 package com.egojit.android.gcoa.model;
  2
  3
  4 /**
  5  * Created by Administrator on 2016-06-14.
  6  */
  7 public class EmptyModel extends Entity {
  8      String var;
  9     String var1;
 10     String var2;
 11     String var3;
 12     String var4;
 13     String var11;
 14     String var12;
 15     String var13;
 16     String var14;
 17     String var5;
 18     String var15;
 19
 20     public String getVar5() {
 21         return var5;
 22     }
 23
 24     public void setVar5(String var5) {
 25         this.var5 = var5;
 26     }
 27
 28     public String getVar15() {
 29         return var15;
 30     }
 31
 32     public void setVar15(String var15) {
 33         this.var15 = var15;
 34     }
 35
 36     public String getVar11() {
 37         return var11;
 38     }
 39
 40     public void setVar11(String var11) {
 41         this.var11 = var11;
 42     }
 43
 44     public String getVar12() {
 45         return var12;
 46     }
 47
 48     public void setVar12(String var12) {
 49         this.var12 = var12;
 50     }
 51
 52     public String getVar13() {
 53         return var13;
 54     }
 55
 56     public void setVar13(String var13) {
 57         this.var13 = var13;
 58     }
 59
 60     public String getVar14() {
 61         return var14;
 62     }
 63
 64     public void setVar14(String var14) {
 65         this.var14 = var14;
 66     }
 67
 68     public String getVar1() {
 69         return var1;
 70     }
 71
 72     public void setVar1(String var1) {
 73         this.var1 = var1;
 74     }
 75
 76     public String getVar2() {
 77         return var2;
 78     }
 79
 80     public void setVar2(String var2) {
 81         this.var2 = var2;
 82     }
 83
 84     public String getVar3() {
 85         return var3;
 86     }
 87
 88     public void setVar3(String var3) {
 89         this.var3 = var3;
 90     }
 91
 92     public String getVar4() {
 93         return var4;
 94     }
 95
 96     public void setVar4(String var4) {
 97         this.var4 = var4;
 98     }
 99
100     public String getVar() {
101         return var;
102     }
103
104     public void setVar(String var) {
105         this.var = var;
106     }
107 }

转载于:https://www.cnblogs.com/wangying222/p/6946145.html

自定义列表数据自动循环向下滚动view(类似于通知通报消息)相关推荐

  1. matlab 循环写excel文件,matlab数据自动循环写入excel文件

    功能说明: 1.表格第一行(1)要留着添加表头描述,同时也可以随时向表格手工添加数据,因此不能用计数进行循环 第一列(A)为时间戳字符串(如20150413123415).每次运行程序就存储一次数据, ...

  2. jQuery实现列表数据从右至左滚动(类似弹幕)

    平常做的小项目新增的小需求,客户要求页面实现以类似弹幕的形式实现多条数据从右至左以不同的速度逐条循环滚动显示, 用css中的animation来简单实现,如有bug,请自行修改,此博客仅作参考使用. ...

  3. 在javascript 用foreach遍历列表数据进行循环

    最近在工作中开发一个公司流程架构,因为所有数据是后台配置实现,在取值和验证的时候无法用常规的for循环在js里,最后尝试直接用c:foreach在js里进行使用,其用法与常规的for循环一样: 标题 ...

  4. Vue向下滚动加载更多数据-scroll-案例

    vue-infinite-scroll 安装 npm install vue-infinite-scroll --save 尽管官方也推荐了几种载入方式,但"最vue"的方式肯定是 ...

  5. Uni-app实现表格数据自动滚动(无限滚动)

    需求:实现表格数据自动(无限)滚动(排除使用删除数组第一条数据添加到最后面的方法) uniapp自带的table组件和插件市场中的插件都不能使用(因为uniapp无法操作原生DOM元素),因此,实现表 ...

  6. vfp取得excel的句柄_如何使用填充句柄将顺序数据自动填充到Excel中

    vfp取得excel的句柄 The Fill Handle in Excel allows you to automatically fill in a list of data (numbers o ...

  7. VUE学习(七) 自定义列表鼠标移入变色,点击变色(仿el-table实现)

    常规列表数据用elementUI的el-table标签即可实现,但有些需要自定义列表数据样式的,则需要手写,这样如何做出和el-table一样的效果呢?一下代码可以实现 页面渲染 <div cl ...

  8. (仿头条APP项目)6.点击过的新闻列表文字变灰和下拉刷新与滚动加载新闻数据

    文章目录 一.点击过的新闻列表文字变灰 效果图 实现思路 导入ormlite数据库类依赖 利用ormlite创建数据库和表 创建数据库类MyDbHelper 创建数据库中的新闻实体类NewInfo 页 ...

  9. python列表逐行写入excel_python表格数据到excel-想问下python3怎么将列表数据逐行写入excel表格呢?...

    如何用python将数据写入excel表格 简介绍一下这两个库,先说xlrd,这个Excel比较,各种方法使用起来方便: bk = xlrd.open_workbook('your.xls') sh ...

  10. ajax实现向上正在加载,向上滚动或者向下滚动分页异步加载数据(Ajax + lazyload)

    /**** desc : 分页异步获取列表数据,页面向上滚动时候加载前面页码,向下滚动时加载后面页码 ajaxdata_url ajax异步的URL 如data.php page_val_name a ...

最新文章

  1. oauth 使用令牌_使用OAuth2令牌的安全REST服务
  2. lhgdialogV2.37 使用
  3. 新手入门教程-------Spring Boot中集成RabbitMQ
  4. ACM做题过程中的一些小技巧
  5. Razor语法(三)
  6. 掘金小册:Git 原理详解和实用指南
  7. 等值线/面生成一站式封装
  8. 【微信开发相关】Java后端微信授权登录,获取微信openid以及用户信息
  9. 2022年安全员-C证上岗证题目及在线模拟考试
  10. 008 使用MyBatis,easyUI实现CRUD操作样例-CUD(CRUD完毕)
  11. 亲测有效!解决文件名过长无法删除的情况
  12. U盟用新浪微博第三方登录 文件不存在 c8998 解决办法
  13. 饥荒 阿里云服务器搭建记录
  14. debug基本命令及全称
  15. 获取当前日期的年月日
  16. 自动阅读脚本root的好还是免root的好?
  17. Rufus 格式化和创建可引导U盘的工具
  18. Problem B – Buggy ICPC——思维,找规律
  19. サザンカ(sasanqua)平假名
  20. C语言设计模式-模板模式

热门文章

  1. 启动“附近的人”功能,你有兴趣吗?
  2. 外贸网站 | 在NameCheap或NameSilo购买网站域名
  3. html div虚线背景,聊聊css绘制虚线
  4. 10个国外免费下载电子书网站
  5. 第二门课 改善深层神经网络:超参数调试、正则化以及优化(Improving Deep Neural Networks:Hyperparameter tuning…)
  6. 计算机无法进入操作系统怎么办,老司机教你电脑开机无法进入系统怎么办
  7. 观3b1b线性代数本质系列视频的思考感悟
  8. python编程a的x次方_python平方-Python,平方
  9. CentOS 编译安装 Nebula Graph 3.10
  10. 数据分析各省高考难度,河南两广山西 最难