当前位置: 代码迷 >> Android >> android 布局式跑马灯,非TextView,该如何处理
  详细解决方案

android 布局式跑马灯,非TextView,该如何处理

热度:29   发布时间:2016-05-01 21:02:54.0
android 布局式跑马灯,非TextView
转自:http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=42665&pid=394775&page=1&extra=page%3D1#pid394775

如题,简单的实现了跑马灯效果,把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);

}

}

------解决方案--------------------
为什么要把Scroll.java 放到android.view 中啊,view 中个包在什么地方啊?
  相关解决方案