Android:自定义水平进度条动画

标签 android animation progress-bar

我正在尝试创建一个进度条,其中进度条本身在水平前进时以垂直旋转的方式进行动画处理。我通过以下方式成功地将我的进度可绘制对象用作可绘制对象:

<ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:progressDrawable="@drawable/custom_progress"
        android:layout_marginRight="5dp" />

这是我的可绘制对象:

enter image description here

但我希望它在进行时具有微妙的滚动效果。所以看起来垂直线有点向后移动。你跟着?任何帮助深表感谢。谢谢。

编辑: 我尝试创建一个动画列表作为我的可绘制进度,但我仍然看不到动画。动画列表可以放在进度项的剪辑中吗?

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@drawable/gutter"></item>

<item android:id="@android:id/progress">

<clip>
    <animation-list android:oneshot="false">
        <item android:drawable="@drawable/progress_bar_animate" android:duration="100" />
        <item android:drawable="@drawable/progress_bar_animate2" android:duration="100" />
        <item android:drawable="@drawable/progress_bar_animate3" android:duration="100" />
        <item android:drawable="@drawable/progress_bar_animate4" android:duration="100" />
        <item android:drawable="@drawable/progress_bar_animate5" android:duration="100" />
        <item android:drawable="@drawable/progress_bar_animate6" android:duration="100" />
        <item android:drawable="@drawable/progress_bar_animate7" android:duration="100" />
    </animation-list>
</clip>

</item>
</layer-list>

最佳答案

雅虎!最后 !有效! (但是 !!!! 会导致大图像内存泄漏。它已在下面的帖子中修复)

此代码获取瓦片图片 (tile1),重复它 (TileMode.REPEAT),制作移动动画(10 个 fragment ),将其添加到动画集​​中。

enter image description here

private void initAnimation() {
//      R.drawable.tile1 is PNG
        Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.tile1);
        AnimationDrawable shiftedAnimation = getAnimation(b);

//      R.id.img_3 is ImageView in my application
        View v = findViewById(R.id.img_3);
        v.setBackground(shiftedAnimation);
        shiftedAnimation.start();
}

private Bitmap getShiftedBitmap(Bitmap bitmap, int shiftX) {
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    Canvas newBitmapCanvas = new Canvas(newBitmap);

    Rect srcRect1 = new Rect(shiftX, 0, bitmap.getWidth(), bitmap.getHeight());
    Rect destRect1 = new Rect(srcRect1);
    destRect1.offset(-shiftX, 0);
    newBitmapCanvas.drawBitmap(bitmap, srcRect1, destRect1, null);

    Rect srcRect2 = new Rect(0, 0, shiftX, bitmap.getHeight());
    Rect destRect2 = new Rect(srcRect2);
    destRect2.offset(bitmap.getWidth() - shiftX, 0);
    newBitmapCanvas.drawBitmap(bitmap, srcRect2, destRect2, null);

    return newBitmap;
}

private List<Bitmap> getShiftedBitmaps(Bitmap bitmap) {
    List<Bitmap> shiftedBitmaps = new ArrayList<Bitmap>();
    int fragments = 10;
    int shiftLength = bitmap.getWidth() / fragments;

    for(int i = 0 ; i < fragments; ++i){
        shiftedBitmaps.add( getShiftedBitmap(bitmap,shiftLength * i));
    }

    return shiftedBitmaps;
}

private AnimationDrawable getAnimation(Bitmap bitmap) {
    AnimationDrawable animation = new AnimationDrawable();
    animation.setOneShot(false);

    List<Bitmap> shiftedBitmaps = getShiftedBitmaps(bitmap);
    int duration = 50;

    for(Bitmap image: shiftedBitmaps){
        BitmapDrawable navigationBackground = new BitmapDrawable(getResources(), image);
        navigationBackground.setTileModeX(TileMode.REPEAT);

        animation.addFrame(navigationBackground, duration);
    }
    return animation;
}

关于Android:自定义水平进度条动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13964520/

相关文章:

android - 如何通过应用程序将音乐排队到默认的 android 音乐播放器?

android - 如何使图像适合我的按钮

android - 如何在代码中将小部件添加到主屏幕

Android View.setKeepScreenOn() - 它在 View 消失时适用吗?

animation - D3多折线图-奇怪的动画

java - 进度条不会立即更新

php - gif 加载器作为顶层居中

c# - 摇动动画(3d版)

javascript - ASP.NET 中的动画

java - 如何在 Android 上使用两个或多个搜索栏