纵向跑马灯的实现
来源:互联网 发布:mac os x系统官网下载 编辑:程序博客网 时间:2024/06/05 01:53
//自定义view
public class VerticalMarqueeView extends View {
public static final int DURATION_SCROLL = 2000;public static final int DURATION_ANIMATOR = 1000;
private int color = Color.BLACK;
private int textSize = 30;
private String[] datas = null;
private int width;
private int height;
private int centerX;
private int centerY;
private List<TextBlock> blocks = new ArrayList<TextBlock>(2);
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Handler handler = new Handler();
private boolean isStopScroll = false;
public VerticalMarqueeView(Context context, AttributeSet attrs, int defStyleAttr){
super(context, attrs, defStyleAttr);
}
public VerticalMarqueeView(Context context, AttributeSet attrs){
super(context, attrs);
}
public VerticalMarqueeView(Context context){
super(context);
}
public VerticalMarqueeView color(int color){
this.color = color;
return this;
}
public VerticalMarqueeView textSize(int textSize){
this.textSize = textSize;
return this;
}
public VerticalMarqueeView datas(String[] datas){
this.datas = datas;
return this;
}
public void commit(){
if(this.datas == null || datas.length == 0){
Log.e("VerticalMarqueeView", "the datas's length is illegal");
throw new IllegalStateException("may be not invoke the method named datas(String[])");
}
paint.setColor(color);
paint.setTextSize(textSize);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(this.datas == null || this.datas.length == 0){
Log.e("VerticalMarqueeView", "the datas's length is illegal");
return;
}
width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
centerX = width / 2;
centerY = height / 2;
blocks.clear();
//添加显示区域的文字块
TextBlock block1 = new TextBlock(width, height, paint);
block1.reset(datas[0], centerX, centerY, 0);
blocks.add(block1);
if(datas.length > 1){
TextBlock block2 = new TextBlock(width, height, paint);
block2.reset(datas[1], centerX, centerY + height, 1);
blocks.add(block2);
}
}
@Override
protected void onDraw(Canvas canvas){
for(int i = 0; i < blocks.size(); i++){
blocks.get(i).draw(canvas);
}
}
public void startScroll(){
isStopScroll = false;
if(datas == null || datas.length == 0 || datas.length == 1){
Log.e("VerticalMarqueeView", "the datas's length is illegal");
return;
}
if(!isStopScroll){
handler.postDelayed(new Runnable(){
@Override
public void run(){
scroll();
if(!isStopScroll){
handler.postDelayed(this, DURATION_SCROLL);
}
}
}, DURATION_SCROLL);
}
}
public void stopScroll(){
this.isStopScroll = true;
}
private void scroll(){
ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder(PropertyValuesHolder.ofInt("scrollY", centerY, centerY - height));
animator.setDuration(DURATION_ANIMATOR);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
@Override
public void onAnimationUpdate(ValueAnimator animation){
int scrollY = (int)animation.getAnimatedValue("scrollY");
blocks.get(0).reset(scrollY);
blocks.get(1).reset(scrollY + height);
invalidate();
}
});
animator.addListener(new Animator.AnimatorListener(){
@Override
public void onAnimationStart(Animator animation){
}
@Override
public void onAnimationRepeat(Animator animation){
}
@Override
public void onAnimationEnd(Animator animation){
//移除第一块
int position = blocks.get(1).getPosition();
TextBlock textBlock = blocks.remove(0);
//最后一个
if(position == datas.length - 1){
position = 0;
}else{
position ++;
}
textBlock.reset(datas[position], centerY + height, position);
blocks.add(textBlock);
invalidate();
}
@Override
public void onAnimationCancel(Animator animation){
}
});
animator.start();
}
public int getCurrentPosition(){
if(datas == null || datas.length == 0){
return -1;
}
if(datas.length == 1 && blocks.size() == 1){
return 0;
}
return blocks.get(0).getPosition();
}
public class TextBlock {
private int width;
private int height;
private String text;
private int drawX;
private int drawY;
private Paint paint;
private int position;
public TextBlock(int width, int height, Paint paint){
this.width = width;
this.height = height;
this.paint = paint;
}
public void reset(int centerY){
reset(text, centerX, centerY, position);
}
public void reset(String text, int centerY){
reset(text, centerX, centerY, position);
}
public void reset(String text, int centerY, int position){
reset(text, centerX, centerY, position);
}
public void reset(String text, int centerX, int centerY, int position){
this.text = text;
this.position = position;
int measureWidth = (int)paint.measureText(text);
drawX = (width - measureWidth) / 2;
Paint.FontMetrics metrics = paint.getFontMetrics();
drawY = (int)(centerY + (metrics.bottom - metrics.top) / 2 - metrics.bottom);
}
public int getPosition(){
return position;
}
public void draw(Canvas canvas){
canvas.drawText(text, drawX, drawY, paint);
}
}
}
----------------------activity调用----------------------------
//跑马灯信息显示
vmView = (VerticalMarqueeView)view.findViewById(R.id.vmView);
String[] datas = new String[]{
"玩手机上瘾了?拿它戒了","没想到,女明星比我还节俭啊..","太会玩!美甲居然搞3D","日本女装大佬的“求生之路”","无屏!这才是手机的究极形态"
};
vmView.color(getResources().getColor(android.R.color.black))
.textSize(sp2px(getActivity(), 15))
.datas(datas).commit();
vmView.startScroll();
//点击事件
vmView.setOnClickListener(
new
View.OnClickListener(){
@Override
public
void
onClick(View v){
Toast.makeText(MainActivity.
this
,
"当前的索引为:"
+ vmView.getCurrentPosition(), Toast.LENGTH_SHORT).show();
}
});
private
int
sp2px(Context context,
int
sp){
float
density = context.getResources().getDisplayMetrics().scaledDensity;
return
(
int
) (sp * density +
0
.5f);
}
@Override
protected
void
onDestroy() {
super
.onDestroy();
//必须要调用,否则内存中会一直无限循环
vmView.stopScroll();
}
阅读全文
0 0
- 纵向跑马灯的实现
- Android开发之纵向跑马灯效果实现
- 跑马灯的实现
- 跑马灯的实现
- Android 纵向跑马灯滚动效果
- TextView跑马灯的实现
- Android跑马灯的实现
- TextView跑马灯的实现
- 跑马灯ViewFlipper的实现
- 跑马灯效果的两种实现
- WinForm中实现跑马灯的效果
- textview 长文本 跑马灯的实现
- TextView跑马灯的两种实现
- XML 实现 TextView的跑马灯效果
- Android TextView 跑马灯效果的实现
- TextView实现跑马灯的效果
- Android跑马灯的基本实现
- TextView实现跑马灯的效果
- leetcode 455. Assign Cookies
- Qml字体选择对话框
- 对称加密-利用mcrypt实现加密解密(2)
- 程序猿的24个段子,超级实用和搞笑哦!!!
- HTML学习小记
- 纵向跑马灯的实现
- java中Object类的方法
- SegNet-论文笔记-理解
- mac phpStorm 搭建PHP运行环境
- 常用的Linux命令
- 最小生成树(蒟蒻)
- 文件API(篇二)
- java实现微信h5支付
- 解决javaweb返回json中文为问号的问题