Android 텍스트 주마등 컨트롤 (텍스트 자동 스크롤 컨트롤, 좌우 이동 테이프 원본)
전재 주소:http://blog.csdn.net/u014608640/article/details/52486324
최근 에 응용 프로그램 을 개발 하고 있 는데 텍스트 의 주마등 효과 가 필요 해서 간단하게 처 리 했 습 니 다.
우선: 효과 도 보기:
코드 는 다음 과 같 습 니 다:
홈 페이지:
public class MainActivity extends Activity implements OnClickListener {
private Button mBtnNext;
private Button mBtnPrev;
private AutoTextView mTextView02;
final Handler handler = new Handler();
//
private static int sCount = 0;
private List<String> str = new ArrayList<String>();
private HorizonScrollTextView tv_2;
private HorizonScrollTextView2 tv_3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
//
//
str.add(" 1");
str.add(" 2");
str.add(" 3");
sCount = str.size();
mTextView02 = (AutoTextView) findViewById(R.id.switcher02);
mTextView02.setText(str.get(0));
//
handler.postDelayed(runnable, 5000);
//handler.removeCallbacks(runnable);//
//
tv_2 = (HorizonScrollTextView)findViewById(R.id.tv_2);
tv_2.setText(" ! !2 !3 !4 ");
tv_2.setTextSize(20);
tv_2.setTextColor(Color.WHITE);
// 2
tv_3= (HorizonScrollTextView2)findViewById(R.id.tv_3);
tv_3.setText(" IE i ");
tv_3.init(getWindowManager());
tv_3.startScroll();
}
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//
mTextView02.next();
sCount++;
if(sCount>=Integer.MAX_VALUE){
sCount = str.size();
}
mTextView02.setText(str.get(sCount % (str.size())));
if (str.size()>1) {
handler.postDelayed(this, 5000);// 50
}
}
};
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.next:
mTextView02.next();
sCount++;
break;
case R.id.prev:
mTextView02.previous();
sCount--;
break;
}
// mTextView02.setText(sCount%2==0 ?
// sCount+"AAFirstAA" :
// sCount+"BBBBBBB");
mTextView02.setText(str.get(sCount % 3));
System.out.println("getH: [" + mTextView02.getHeight() + "]");
}
AutoTextView 클래스
스크롤 백 과 좌우 이동 처리
public class AutoTextView extends TextSwitcher implements
ViewSwitcher.ViewFactory {
private float mHeight;
private Context mContext;
//mInUp,mOutUp
private Rotate3dAnimation mInUp;
private Rotate3dAnimation mOutUp;
//mInDown,mOutDown
private Rotate3dAnimation mInDown;
private Rotate3dAnimation mOutDown;
public AutoTextView(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
public AutoTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.auto3d);
//mHeight = a.getDimension(R.styleable.auto3d_textSize, 36);
mHeight=20;
a.recycle();
mContext = context;
init();
}
private void init() {
// TODO Auto-generated method stub
setFactory(this);
mInUp = createAnim(-90, 0 , true, true);
mOutUp = createAnim(0, 90, false, true);
mInDown = createAnim(90, 0 , true , false);
mOutDown = createAnim(0, -90, false, false);
//TextSwitcher , A B,
//setInAnimation() ,A inAnimation,
//setOutAnimation() ,B OutAnimation
setInAnimation(mInUp);
setOutAnimation(mOutUp);
}
private Rotate3dAnimation createAnim(float start, float end, boolean turnIn, boolean turnUp){
final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, turnIn, turnUp);
//
rotation.setDuration(300);
rotation.setFillAfter(false);
rotation.setInterpolator(new AccelerateInterpolator());
return rotation;
}
public void setData(){
}
// TextView, View
@Override
public View makeView() {
// TODO Auto-generated method stub
TextView t = new TextView(mContext);
t.setGravity(Gravity.CENTER);
t.setTextSize(mHeight);
t.setMaxLines(2);
t.setPadding(0, 5, 0, 5);
//
t.setTextColor(Color.WHITE);
return t;
}
// ,
public void previous(){
if(getInAnimation() != mInDown){
setInAnimation(mInDown);
}
if(getOutAnimation() != mOutDown){
setOutAnimation(mOutDown);
}
}
// ,
public void next(){
if(getInAnimation() != mInUp){
setInAnimation(mInUp);
}
if(getOutAnimation() != mOutUp){
setOutAnimation(mOutUp);
}
}
class Rotate3dAnimation extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private float mCenterX;
private float mCenterY;
private final boolean mTurnIn;
private final boolean mTurnUp;
private Camera mCamera;
public Rotate3dAnimation(float fromDegrees, float toDegrees, boolean turnIn, boolean turnUp) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mTurnIn = turnIn;
mTurnUp = turnUp;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
mCenterY = getHeight() / 2;
mCenterX = getWidth() / 2;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX ;
final float centerY = mCenterY ;
final Camera camera = mCamera;
final int derection = mTurnUp ? 1: -1;
final Matrix matrix = t.getMatrix();
camera.save();
if (mTurnIn) {
camera.translate(0.0f, derection *mCenterY * (interpolatedTime - 1.0f), 0.0f);
} else {
camera.translate(0.0f, derection *mCenterY * (interpolatedTime), 0.0f);
}
camera.rotateX(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
}
HorizonScrollTextView 클래스
public class HorizonScrollTextView extends TextView{
private boolean mStopMarquee;
private String mText;
private float mCoordinateX;
private float mTextWidth;
public HorizonScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setText(String text) {
this.mText = text;
mTextWidth = getPaint().measureText(mText);
if (mHandler.hasMessages(0))
mHandler.removeMessages(0);
mHandler.sendEmptyMessageDelayed(0, 2000);
}
@Override
protected void onAttachedToWindow() {
mStopMarquee = false;
if (!(mText == null || mText.isEmpty()))
mHandler.sendEmptyMessageDelayed(0, 2000);
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
mStopMarquee = true;
if (mHandler.hasMessages(0))
mHandler.removeMessages(0);
super.onDetachedFromWindow();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!(mText == null || mText.isEmpty()))
canvas.drawText(mText, mCoordinateX, 30, getPaint());
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
if (Math.abs(mCoordinateX) > (mTextWidth + 5)) {
mCoordinateX = 0;
invalidate();
if (!mStopMarquee) {
sendEmptyMessageDelayed(0,500);
}
} else {
mCoordinateX -= 1;
invalidate();
if (!mStopMarquee) {
sendEmptyMessageDelayed(0, 30);
}
}
break;
}
super.handleMessage(msg);
}
};
}
HorizonScrollTextView 2 클래스
public class HorizonScrollTextView2 extends TextView implements OnClickListener {
private float textLength = 0f;//
private float viewWidth = 0f;
private float step = 0f;//
private float y = 0f;//
private float temp_view_plus_text_length = 0.0f;//
private float temp_view_plus_two_text_length = 0.0f;//
public boolean isStarting = false;//
private Paint paint = null;//
private String text = "";//
public HorizonScrollTextView2(Context context) {
super(context);
initView();
}
public HorizonScrollTextView2(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public HorizonScrollTextView2(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
private void initView() {
setOnClickListener(this);
}
public void init(WindowManager windowManager) {
paint = getPaint();
//
paint.setColor(Color.WHITE);
text = getText().toString();
textLength = paint.measureText(text);
viewWidth = getWidth();
if (viewWidth == 0) {
if (windowManager != null) {
Display display = windowManager.getDefaultDisplay();
viewWidth = display.getWidth();
}
}
step = textLength;
temp_view_plus_text_length = viewWidth + textLength;
temp_view_plus_two_text_length = viewWidth + textLength * 2;
y = getTextSize() + getPaddingTop();
}
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.step = step;
ss.isStarting = isStarting;
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
step = ss.step;
isStarting = ss.isStarting;
}
public static class SavedState extends BaseSavedState {
public boolean isStarting = false;
public float step = 0.0f;
SavedState(Parcelable superState) {
super(superState);
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeBooleanArray(new boolean[] { isStarting });
out.writeFloat(step);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
public SavedState[] newArray(int size) {
return new SavedState[size];
}
@Override
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
};
private SavedState(Parcel in) {
super(in);
boolean[] b = null;
in.readBooleanArray(b);
if (b != null && b.length > 0)
isStarting = b[0];
step = in.readFloat();
}
}
public void startScroll() {
isStarting = true;
invalidate();
}
public void stopScroll() {
isStarting = false;
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
if (!isStarting) {
return;
}
step += 0.8;// 0.5 。
if (step > temp_view_plus_two_text_length)
step = textLength;
invalidate();
}
//
@Override
public void onClick(View v) {
if (isStarting)
stopScroll();
else
startScroll();
}
}
마지막 으로 원본 주소 첨부:http://download.csdn.net/detail/u014608640/9626180
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
extjsiframe에서 컨트롤 값이나 변수 값을 가져오는 방법ext에서 iframe에 사용할 때 iframe 내외에서 값을 얻는 방법 1. iframe 가져오는 방법 1 2. 방법2 3. 방법 3 1、document.frames["frameName"].document.getE...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.