布局式跑马灯,非TextView

原帖:
http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=42665&pid=259991&page=1&extra=#pid259991
如题,简单的实现了跑马灯效果,把Scroll.java放入android.view包下,XML使用如下:
<?xml version="1.0" encoding="utf-8"?>
<Scroll xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dip"
android:layout_height="wrap_content">
<!--注意:Scroll里的布局或者控件元素只能有一个可以用布局嵌套布局/控件来使用,当Scroll里的唯一元素的宽度超过Scroll效果最好,如果不超过没加处理,有兴趣可以自己加上-->
<TextView android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="放大镜流口水附近拉神经分裂卡上的经费里卡迪神经分裂空间爱上" />
</Scroll>
package android.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;

public class GroupScrollView extends HorizontalScrollView implements Runnable {
	private View inner;
	private Bitmap bitmap = null;
	private int x;
	private int width;
	private int pWidth;
	private int pHeight;

	/**
	 * 滚动步长
	 */
	private int step = 5;
	/**
	 * 滚动间隔距离
	 */
	private int space = 100;
	private int delay = 500;

	public GroupScrollView(Context context) {
		super(context);
		setBackgroundColor(android.R.color.transparent);
	}
	public GroupScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setBackgroundColor(android.R.color.transparent);
	}

	@Override
	protected void onFinishInflate() {
		if (getChildCount() == 1) {
			inner = getChildAt(0);
		}
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		handler.removeCallbacks(this);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (getWidth() == 0) {
			android.view.ViewGroup.LayoutParams lp = getLayoutParams();
			lp.width = pWidth;
			lp.height = pHeight;
			setLayoutParams(lp);
		}
		if (bitmap == null && inner != null) {
			width = inner.getMeasuredWidth();
			bitmap = Bitmap.createBitmap(width, inner.getHeight(),Config.RGB_565);
			Canvas canvas1 = new Canvas(bitmap);
			inner.draw(canvas1);
			pWidth = getWidth();
			pHeight = getHeight();
			if (inner != null) {
				removeViewInLayout(inner);
				inner = null;
			}
			run();
		}
		if (bitmap != null) {
			int nowX = x;
			nowX -= step;
			canvas.drawBitmap(bitmap, nowX, 0, null);
			if (nowX < 0) {
				canvas.drawBitmap(bitmap, width + nowX + space, 0, null);
			}
			if (nowX <= -width) {
				nowX = 0;
			}
			x = nowX;
		}
		super.onDraw(canvas);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			super.handleMessage(msg);
		}
	};

	@Override
	public void run() {
		invalidate();
		handler.postDelayed(this, delay);
	}

}

布局main.xml:
<?xml version="1.0" encoding="utf-8"?>
<GroupScrollView xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	>
	<LinearLayout android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:orientation="vertical"
		>
		<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="1234567890abcdefghijklmnopqrstuvwxyz1234567890" 
		/>
		<LinearLayout android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:orientation="horizontal"
		>
			<TextView android:layout_width="fill_parent"
			android:layout_height="wrap_content" 
			android:text="abcdefghijklmnopqrstuvwxyz" 
			android:layout_weight="1"
			/>
			<TextView android:layout_width="fill_parent"
			android:layout_height="wrap_content" 
			android:text="1234567890" 
			android:layout_weight="1"
			/>
		</LinearLayout>
	</LinearLayout>
</GroupScrollView>

你可能感兴趣的:(android,PHP,xml,OS,bbs)