【问题标题】:How to change Progress bar image in android如何在android中更改进度条图像
【发布时间】:2012-03-16 06:29:50
【问题描述】:

我想将进度条更改为自定义可绘制对象。如何更改进度条的图像?

【问题讨论】:

  • 对我来说也很痛苦。要求回答。

标签: android android-progressbar


【解决方案1】:

这是我的答案,我在 android 进度条中使用我的图像..

 <ProgressBar
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:indeterminate="true"
        android:indeterminateDrawable="@drawable/my_progress_indeterminate" />

my_progress_indeterminate.xml:

  <?xml version="1.0" encoding="utf-8"?>
    <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" 
        android:drawable="@drawable/animation" 
        android:pivotX="50%"
        android:pivotY="50%"/>

动画.xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <item android:drawable="@drawable/load" android:duration="50" />
    <item android:drawable="@drawable/load" android:duration="50" />
    <item android:drawable="@drawable/load" android:duration="50" />
     <rotate 
         xmlns:android="http://schemas.android.com/apk/res/android" 
         android:drawable="@drawable/load" 
         android:pivotX="50%" 
         android:pivotY="50%" 
         android:fromDegrees="330" 
         android:toDegrees="360" 
         android:repeatCount="1" />     

</animation-list>

【讨论】:

  • 什么是 android:drawable="@drawable/load" ?
  • 这是一个可绘制的图像 load.png @itzhar
  • 你在哪里使用 animatio.xml ?
  • @hamid in my_progress_indeterminate.xml
【解决方案2】:

您可以通过扩展ImageView 来创建一个类,并以与ProgressBar 类似的方式使用它,并带有旋转动画。

class CustomProgressBar : AppCompatImageView {

constructor(context: Context) : super(context)

constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr)

private var anim: RotateAnimation? = null

init {
    setImageResource(R.drawable.your_custom_drawable)
    anim = RotateAnimation(
        0f, 350f, Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f
    )
    anim!!.interpolator = LinearInterpolator()
    anim!!.repeatCount = Animation.INFINITE
    anim!!.duration = 1000
    startAnimation(anim)
}

fun getRotateAnimation(): RotateAnimation? {
    return anim
}}

在你的 layout.xml 中,只需使用这个类作为你的 ProgressBar

            <com.example.CustomProgressBar
            android:id="@+id/customProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />

并在您的片段或活动中显示或隐藏它,如下所示:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    customProgressBar = findViewById(R.id.customProgressBar)
}

fun showCustomProgressBar() {
    customProgressBar.visibility = VISIBLE
    customProgressBar.startAnimation(customProgressBar.getRotateAnimation())
}

fun hideCustomProgressBar() {
    customProgressBar.visibility = GONE
    customProgressBar.clearAnimation()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 2020-08-23
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多