0

いくつかのテキストを右から左にスムーズにスクロールするカスタム ビューを作成する必要があります。

さて、書いてみましたが、スクロールがスムーズに見えません。コードは次のとおりです。

private Runnable tick = new Runnable() {
    @Override
    public void run() {
        anim();
        invalidate();
        handler.postDelayed(this, 10);
    }
};

private void anim()
{
    long time = System.currentTimeMillis();
    long dTime = time - animStartTime;
    animStartTime = time;

    offsetX-=(float)(dTime*ANIM_SPEED);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);



    if(rates!=null)
    {
        for(int i=0; i<rates.length; i++)
        {
            Rate r = rates[i];
            float x = ((i*rateWidth) + offsetX + r.offsetX);
            float y = 120;

            if(x>w)
            {
                continue;
            }
            else if(x+rateWidth<0)
            {
                r.offsetX += rates.length*rateWidth;
                continue;
            }

            canvas.drawText(r.currency, x, y, paintBlack);

        }
    }

}

それほど複雑ではありませんが、スクロールがまったく滑らかに見えず、時々ガタガタします。抽選中に割り当ては行いません。

どうすればこれを改善できますか?

4

2 に答える 2

0

私は自分のデバイスで自分のコードをテストしただけで、あなたはそれを試すことができます. OnDraw で私のコードの代わりにあなたのコードを使用してください。

public class MoveText extends View {

private Scroller mScroller;

private Paint mPaint;

private int x;

public MoveText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public MoveText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MoveText(Context context) {
    super(context);
    init();
}

private void init() {
    mScroller = new Scroller(getContext(), new LinearInterpolator());
    mPaint = new Paint();
    mPaint.setColor(Color.BLACK);
    mPaint.setTextSize(30f);
}

@Override
public void computeScroll() {
    super.computeScroll();
    // loop invalidate until the scroller animation is finish.
    if (mScroller.computeScrollOffset()) {
        x = mScroller.getCurrX();
        invalidate();
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // please use your code instead of mine below.
    canvas.drawText("scrolling text", x, mPaint.getTextSize(), mPaint);
}

/**
 * start text translate animation.
 */
public void startScroll() {
    if (mScroller.isFinished()) {
        int width = getWidth();
        mScroller.startScroll(0, 0, width, 0, 10000);
        postInvalidate();
    }

}
}
于 2013-08-17T11:27:27.287 に答える
0

View の代わりに SurfaceView を使用することで、速度が向上しました。

于 2013-08-21T06:30:50.637 に答える