一、首先自定义的textview:
public class ScrollTextView extends TextSwitcher implements ViewSwitcher.ViewFactory { private Context context; // inAnimation,outAnimation分别构成翻页的进出动画 private ScrollAnimation inAnimation; private ScrollAnimation outAnimation; public ScrollTextView(Context context) { this(context, null); } public ScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); //将获取自定义textSize的值,如果没有,则使用默认的值,15。 TypedArray typedArray = context.obtainStyledAttributes(attrs, new int[] { 0x7f010000 }); typedArray.recycle(); this.context = context; init(); } private void init() { setFactory(this);//创建两个view inAnimation = createAnim(-90, 0, true, true); outAnimation = createAnim(0, 90, false, true); setInAnimation(inAnimation); setOutAnimation(outAnimation); } private ScrollAnimation createAnim(float start, float end, boolean turnIn, boolean turnUp) { final ScrollAnimation rotation = new ScrollAnimation(start, end, turnIn, turnUp); rotation.setDuration(800); rotation.setFillAfter(false); rotation.setInterpolator(new AccelerateInterpolator()); return rotation; } public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } /** * 这里返回的TextView,就是我们看到的View */ @Override public View makeView() { TextView textView = new TextView(context); textView.setGravity(Gravity.LEFT); textView.setPadding(px2dip(context,12), px2dip(context,12), px2dip(context,12), px2dip(context,12)); textView.setTextSize(px2dip(context,90)); textView.setTextColor(context.getResources().getColor(R.color.color_lamp_text)); textView.setGravity(Gravity.CENTER_VERTICAL);// gravity center_vertical textView.setText(""); textView.setSingleLine(true); //textView.setEllipsize(TextUtils.TruncateAt.END); return textView; } public void next() { if (getInAnimation() != inAnimation) { setInAnimation(inAnimation); } if (getOutAnimation() != outAnimation) { setOutAnimation(outAnimation); } } class ScrollAnimation 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 ScrollAnimation(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.getMatrix(matrix); camera.restore(); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } } }
二、设置布局
<com.lulubao.view.ScrollTextView android:id="@+id/switcher02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffc704" android:paddingBottom="5dp" android:paddingTop="5dp" > </com.lulubao.view.ScrollTextView>
三、dialog中设置效果
public class PreviewDialog extends Dialog { private ScrollTextView scrollTextView; private int count = 0; private boolean isContinue; private Thread myThread; private String strMess; private ArrayList<String> strs =new ArrayList<>(); public void setStrMess(String strMess) { this.strMess = strMess; StringBuffer stringBuffer = new StringBuffer(); try { int strMessLength = strMess.getBytes("GBK").length; if (strMessLength<17){ strs.add(strMess); return; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } for (int i = 0; i < strMess.length(); i++) { try { stringBuffer.append(strMess.substring(i,i+1)); int strBuffLength = stringBuffer.toString().getBytes("GBK").length; if (strBuffLength<17){ }else { stringBuffer.delete(stringBuffer.length()-1,stringBuffer.length()); strs.add(stringBuffer.toString()); stringBuffer.delete(0,stringBuffer.length()); stringBuffer.append(strMess.substring(i,i+1)); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } try { if (stringBuffer.toString().getBytes("GBK").length!=0){ strs.add(stringBuffer.toString()); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } stringBuffer.delete(0,stringBuffer.length()); } private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: scrollTextView.next(); count++; if (count==strs.size()){ count=0; } if (strs.size()==1){ count=0; } scrollTextView.setText(strs.get(count)); break; default: break; } }; }; public PreviewDialog(Context context, int theme) { super(context, theme); // TODO Auto-generated constructor stub } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.previewdialog); //mTextView = (TextView) findViewById(R.id.pao); init(); } private void init() { scrollTextView = (ScrollTextView) findViewById(R.id.switcher02); scrollTextView.setText(strs.get(0));// 设置初始值 isContinue = true; myThread = new Thread() { public void run() { while (isContinue) { SystemClock.sleep(2000); handler.sendEmptyMessage(0);// 每隔一秒 发一个空消息 滚动一次 } }; }; myThread.start(); } @Override public void onBackPressed() { super.onBackPressed(); isContinue = false; if (myThread != null && myThread.isAlive()) { myThread.interrupt(); } } }