Android:翻译动画滞后

发布于 2024-12-23 18:39:06 字数 2209 浏览 2 评论 0原文

我必须实现一个“扫描栏”动画,制作翻译动画(有点像谷歌护目镜应用程序)。这是一个条形图片(自定义视图),从左到右穿过我的表面视图。

我做了一些接近我的期望的事情,除了动画滞后:(

这是我的 XML 的一部分:

<LinearLayout
    android:id="@+id/layoutCamera"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="8" >

    <SurfaceView 
        android:id="@+id/surfaceView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="top" />
</LinearLayout>

这是我的自定义视图:

class ScannerView extends View 
{
private Bitmap scanBar;
private int sizeBarW;
private int sizeBarH;   

public ScannerView(Context context, int heightParent) 
{
    super(context);     

    //init size
    sizeBarW = 5;
        //I adjust the height of the bar to my surfaceview
    sizeBarH = heightParent;

    //prepare bitmap
    scanBar = prepareBitmap(getResources().getDrawable(R.drawable.scan_bar), sizeBarW,
            sizeBarH);      
}

private static Bitmap prepareBitmap(Drawable drawable, int width, int height) 
{
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    drawable.setBounds(0, 0, width, height);
    Canvas canvas = new Canvas(bitmap);
    drawable.draw(canvas);
    return bitmap;
}

@Override
protected void onDraw(Canvas canvas) 
{
    canvas.drawBitmap(scanBar, 0, 0, null);
}
}

这是我的动画函数(从我的活动类中在 surfaceChanged() 上调用):

private void LaunchAnimation()
{
    // 1 - view
    mScanBar = new ScannerView(this, surfaceView.getHeight());
    addContentView(mScanBar, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    // 2 - Animation
    Animation animTranslation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 1.0f,
                                                       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    animTranslation.setDuration(5000);
    animTranslation.setRepeatCount(Animation.INFINITE);
    mScanBar.startAnimation(animTranslation);
}

我我是 android 编程的初学者,我不确定我是否以最好的方式...:?

谢谢你帮助我解决这个滞后/缓慢的问题:)

I have to realize a "scan bar" animation, making an animation of translation (a bit like the google goggles app). It's a bar picture (custom view), which cross my surfaceview from left to right.

I did something that is close to my expectations, except that the animation lagging :(

This is a part of my XML :

<LinearLayout
    android:id="@+id/layoutCamera"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="8" >

    <SurfaceView 
        android:id="@+id/surfaceView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="top" />
</LinearLayout>

This is my custom wiew :

class ScannerView extends View 
{
private Bitmap scanBar;
private int sizeBarW;
private int sizeBarH;   

public ScannerView(Context context, int heightParent) 
{
    super(context);     

    //init size
    sizeBarW = 5;
        //I adjust the height of the bar to my surfaceview
    sizeBarH = heightParent;

    //prepare bitmap
    scanBar = prepareBitmap(getResources().getDrawable(R.drawable.scan_bar), sizeBarW,
            sizeBarH);      
}

private static Bitmap prepareBitmap(Drawable drawable, int width, int height) 
{
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    drawable.setBounds(0, 0, width, height);
    Canvas canvas = new Canvas(bitmap);
    drawable.draw(canvas);
    return bitmap;
}

@Override
protected void onDraw(Canvas canvas) 
{
    canvas.drawBitmap(scanBar, 0, 0, null);
}
}

And this is my animation function (called on surfaceChanged() from my activity class):

private void LaunchAnimation()
{
    // 1 - view
    mScanBar = new ScannerView(this, surfaceView.getHeight());
    addContentView(mScanBar, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    // 2 - Animation
    Animation animTranslation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 1.0f,
                                                       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    animTranslation.setDuration(5000);
    animTranslation.setRepeatCount(Animation.INFINITE);
    mScanBar.startAnimation(animTranslation);
}

I am beginner in android programming and I'm not sure I do it the best way... :?

Thank you to help me to resolve this lagging/slow-down problem. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文