自動垂直スクロール(aut Text)


ソースアドレスhttp://download.csdn.net/detail/daweibalang717/9337789
カスタムコントロール:
package cn.silent.view;

import java.util.ArrayList;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * @author silentyang
 * */
public class AutoTextLine extends FrameLayout {

	private ArrayList<TextView> listView;

	private Context mContext;

	private TextView curTextView;

	private TextView nextTextView;

	private final int MSG_START = 0;

	private final int MSG_NEXT = 1;

	//       
	private int delay = 5000;

	//    
	private int duartion = 500;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_START:
				start();
				break;

			case MSG_NEXT:
				nextView();
				break;
			}

		};
	};

	public AutoTextLine(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public AutoTextLine(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public void init(Context context) {
		listView = new ArrayList<>();
		mContext = context;
	}

	public void SetData(ArrayList<String> list) {
		for (int i = 0; i < list.size(); i++) {
			TextView txt = getView(list.get(i));
			if (i == list.size() - 1) {//    VIEW
				txt.setTag(0);
			} else {
				txt.setTag(i + 1);
			}
			listView.add(txt);
		}
		if (listView.size() > 1) {
			//     view      
			curTextView = listView.get(0);
			nextTextView = listView.get(1);
			addView(curTextView);
			mHandler.sendEmptyMessage(MSG_START);

		} else if (listView.size() == 1) {
			//    TextView
			curTextView = listView.get(0);
			addView(curTextView);
		}

	}

	public TextView getView(String string) {
		TextView txt = new TextView(mContext);
		txt.setText(string);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		txt.setLayoutParams(lp);
		txt.setGravity(Gravity.CENTER);
		return txt;
	}

	public void start() {
		//addView(curTextView);
		addView(nextTextView);
		//      (0,0)        X 0%Y -120%   
		TranslateAnimation out = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, -1.2f);
		//TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 100);
		out.setDuration(duartion);
		/*anim.setRepeatCount(2);
		anim.setRepeatMode(Animation.REVERSE);*/
		out.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				//TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0);
				//curTextView.setAnimation(anim);
				removeView(curTextView);
				mHandler.sendEmptyMessageDelayed(MSG_NEXT, delay);
			}
		});

		TranslateAnimation in = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1.2f, Animation.RELATIVE_TO_PARENT, 0);
		in.setDuration(duartion);
		curTextView.startAnimation(out);
		nextTextView.startAnimation(in);
	}

	public void nextView() {

		curTextView = nextTextView;
		int nextIndex = (int) curTextView.getTag();
		nextTextView = listView.get(nextIndex);
		start();

	}

	public void stop() {

	}

	public int getDelay() {
		return delay;
	}

	public void setDelay(int delay) {
		this.delay = delay;
	}

	public int getDuartion() {
		return duartion;
	}

	public void setDuartion(int duartion) {
		this.duartion = duartion;
	}

}
呼び出し方法:
package com.example.autoscroll;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import cn.silent.view.AutoTextLine;

/**
 * @author silent yang
 * 
 * */
public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		AutoTextLine auto = (AutoTextLine) findViewById(R.id.auto_txt);
		ArrayList<String> data = new ArrayList<String>();
		for (int i = 0; i < 2; i++) {
			data.add("  
: " + i); } auto.SetData(data); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
activitymain.xmlレイアウト:
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >   
    <cn.silent.view.AutoTextLine
        android:id="@+id/auto_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >
    </cn.silent.view.AutoTextLine>



</LinearLayout>
効果図は作れません.大体の意味が込めばいいです.運転スクロールは実は緩やかです.
自动垂直滚动(autoText)_第1张图片