如题,简单的实现了跑马灯效果,把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>
代码如下:
----------------------------------------------转载请注明出处-----------------------------------------------
本帖隐藏的内容需要回复才可以浏览
public class Scroll extends HorizontalScrollView implements Runnable
{
private View inner;
private Bitmap bitmap = null;
/**
* 滚动步长
*/
private int step = 5;
private int x;
private int width;
private int pWidth;
private int pHeight;
/**
* 滚动间隔距离
*/
private int space = 10;
public Scroll(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, 500);
}
}
----------------------------------转载请注明出处-----------------------------------------------
另外提供了一个可以直接跑的附件,不过附件里的只是一个TextView,大家可以把它换上LinearLayout里加上n个TextView看看,前提是LinearLayout要是Horizontal的