public class ADTextViewextends AppCompatTextView{
private int mSpeed;
private int mInterval;
private int mFrontColor;
private int mContentColor;
private int mFrontTextSize;
private int mContentTextSize;
private ListmTexts;
private int mY;
private int mIndex;
private PaintmPaintContent;
private PaintmPaintFront;
private boolean isMove;
private StringTAG;
private boolean hasInit;
private boolean isPaused;
private com.brioal.adtextviewlib.view.ADTextView.OnItemClickListenerOnItemClickListener;
public void setOnItemClickListener(com.brioal.adtextviewlib.view.ADTextView.OnItemClickListeneronItemClickListener){
this.OnItemClickListener =onItemClickListener;
}
public ADTextView(Contextcontext){
this(context, (AttributeSet)null);
}
public ADTextView(Contextcontext, AttributeSetattrs){
super(context, attrs);
this.mY =0;
this.mIndex =0;
this.isMove =true;
this.TAG ="ADTextView";
this.hasInit =false;
this.isPaused =false;
this.obtainStyledAttrs(attrs);
this.init(context);
}
private void obtainStyledAttrs(AttributeSetattrs){
TypedArray array =this.getContext().obtainStyledAttributes(attrs, styleable.ADTextView);
this.mSpeed = array.getInt(styleable.ADTextView_ad_text_view_speed, 1);
this.mInterval = array.getInt(styleable.ADTextView_ad_text_view_interval, 2000);
this.mFrontColor = array.getColor(styleable.ADTextView_ad_text_front_color, -65536);
this.mContentColor = array.getColor(styleable.ADTextView_ad_text_content_color, -16777216);
this.mFrontTextSize = (int)array.getDimension(styleable.ADTextView_ad_text_front_size, (float)SizeUtil.Sp2Px(this.getContext(), 15));
this.mContentTextSize = (int)array.getDimension(styleable.ADTextView_ad_text_content_size, (float)SizeUtil.Sp2Px(this.getContext(), 15));
array.recycle();
}
public boolean onTouchEvent(MotionEventevent){
int action =event.getAction();
switch(action){
case 0:
if(this.OnItemClickListener !=null){
this.OnItemClickListener.onClick(((AdEntity)this.mTexts.get(this.mIndex)).getmUrl());
}
default:
return true;
}
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width =this.measureWidth(widthMeasureSpec);
int height =this.measureHeight(heightMeasureSpec);
this.setMeasuredDimension(width, height);
}
private int measureHeight(int heightMeasureSpec){
int mode = MeasureSpec.getMode(heightMeasureSpec);
int size = MeasureSpec.getSize(heightMeasureSpec);
int result;
if(mode ==1073741824){
result = size;
} else {
int mfronTextHeight = (int)(this.mPaintFront.descent() -this.mPaintFront.ascent());
int mContentTextHeight = (int)(this.mPaintContent.descent() -this.mPaintContent.ascent());
result = Math.max(mfronTextHeight, mContentTextHeight) *2;
if(mode == -2147483648){
result = Math.min(result, size);
}
}
return result;
}
private int measureWidth(int widthMeasureSpec){
int mode = MeasureSpec.getMode(widthMeasureSpec);
int size = MeasureSpec.getSize(widthMeasureSpec);
int result;
if(mode ==1073741824){
result = size;
} else {
String text ="十个字十个字十个字字";
Rect rect =new Rect();
this.mPaintContent.getTextBounds(text, 0, text.length(), rect);
result = rect.right - rect.left;
if(mode == -2147483648){
result = Math.min(result, size);
}
}
return result;
}
public void setTexts(ListmTexts){
this.mTexts =mTexts;
}
public void setInterval(int mInterval){
this.mInterval =mInterval;
}
public void setSpeed(int speed){
this.mSpeed =speed;
}
public void setFrontColor(int mFrontColor){
this.mPaintFront.setColor(mFrontColor);
}
public void setBackColor(int mBackColor){
this.mPaintContent.setColor(mBackColor);
}
public void setFrontTextSize(int frontTextSize){
this.mFrontTextSize =frontTextSize;
}
public void setContentTextSize(int contentTextSize){
this.mContentTextSize =contentTextSize;
}
private void init(Contextcontext){
this.mIndex =0;
this.mPaintFront =new Paint();
this.mPaintFront.setAntiAlias(true);
this.mPaintFront.setDither(true);
this.mPaintFront.setTextSize((float)this.mFrontTextSize);
this.mPaintFront.setColor(this.mFrontColor);
this.mPaintFront.setTextAlign(Paint.Align.RIGHT);
this.mPaintContent =new Paint();
this.mPaintContent.setAntiAlias(true);
this.mPaintContent.setDither(true);
this.mPaintContent.setTextSize((float)this.mContentTextSize);
this.mPaintContent.setColor(this.mContentColor);
this.mPaintContent.setTextAlign(Paint.Align.RIGHT);
}
protected void onDraw(Canvascanvas){
super.onDraw(canvas);
if(this.mTexts !=null){
AdEntity model = (AdEntity)this.mTexts.get(this.mIndex);
String font = model.getmFront();
String back = model.getmBack();
Rect indexBound =new Rect();
this.mPaintFront.getTextBounds(font, 0, font.length(), indexBound);
Rect contentBound =new Rect();
this.mPaintContent.getTextBounds(back, 0, back.length(), contentBound);
if(this.mY ==0 && !this.hasInit){
this.mY =this.getMeasuredHeight() - indexBound.top;
this.hasInit =true;
}
if(this.mY <=0 - indexBound.bottom){
this.mY =this.getMeasuredHeight() - indexBound.top;
++this.mIndex;
this.isPaused =false;
}
canvas.drawText(back, 0, back.length(), (float)(getWidth() - (indexBound.right - indexBound.left +20 +this.getTotalPaddingLeft())), (float)this.mY, this.mPaintContent);
canvas.drawText(font, 0, font.length(), (float)(getWidth() - (10 +this.getTotalPaddingLeft())), (float)this.mY, this.mPaintFront);
if(!this.isPaused &&this.mY <=this.getMeasuredHeight() /2 - (indexBound.top + indexBound.bottom) /2){
this.isMove =false;
this.isPaused =true;
Timer timer =new Timer();
timer.schedule(new TimerTask(){
public void run(){
com.etsdk.app.huov7.view.ADTextView.this.postInvalidate();
com.etsdk.app.huov7.view.ADTextView.this.isMove =true;
}
}, (long)this.mInterval);
}
this.mY -=this.mSpeed;
if(this.mIndex ==this.mTexts.size()){
this.mIndex =0;
}
if(this.isMove){
this.postInvalidateDelayed(2L);
}
}
}
public interface OnItemClickListener{
void onClick(Stringvar1);
}
}
网友评论