想在java代码中动态的改变seekbar的ProgressDrawable图片
使用了如下代码,但是不但不能换成新的图片,而且原来的图片也不见了,显示为空白。
请高人指点,感谢mSeekBar.setProgressDrawable(this.getResources().getDrawable(R.drawable.ProgressDrawable));补充:
不过如果使用如下代码就可以描画
mSeekBar.setProgressDrawable(this.getResources().getDrawable(aa.getDrawingCache()))
aa为本画面中的别一个view.

解决方案 »

  1.   

    经过我数次试验,终于找到了问题发生所在了。
    在BitmapDrawable的draw(canvas)中
       @Override
        public void draw(Canvas canvas) {
            Bitmap bitmap = mBitmap;
            if (bitmap != null) {
                final BitmapState state = mBitmapState;
                if (mRebuildShader) {
                    Shader.TileMode tmx = state.mTileModeX;
                    Shader.TileMode tmy = state.mTileModeY;                if (tmx == null && tmy == null) {
                        state.mPaint.setShader(null);
                    } else {
                        Shader s = new BitmapShader(bitmap,
                                tmx == null ? Shader.TileMode.CLAMP : tmx,
                                tmy == null ? Shader.TileMode.CLAMP : tmy);
                        state.mPaint.setShader(s);
                    }
                    mRebuildShader = false;
                    copyBounds(mDstRect);
                }            Shader shader = state.mPaint.getShader();
                if (shader == null) {
                    if (mApplyGravity) {
                        Gravity.apply(state.mGravity, mBitmapWidth, mBitmapHeight,
                                getBounds(), mDstRect);
                        mApplyGravity = false;
                    }
                    canvas.drawBitmap(bitmap, null, mDstRect, state.mPaint);

                } else {
                    if (mApplyGravity) {
                        mDstRect.set(getBounds());
                        mApplyGravity = false;
                    }
                    canvas.drawRect(mDstRect, state.mPaint);
                }
            }
        }
    红色字部分,由于mApplyGravity值为false(默认值),所以不会进入分支,导致mDstRect为((0,0)~(0,0)),所以在 canvas.drawBitmap(bitmap, null, mDstRect, state.mPaint)时不会描画。
      

  2.   

    mApplyGravity这个值会在以下的代码中被置为true。
        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            mApplyGravity = true;
        }
        /** Set the gravity used to position/stretch the bitmap within its bounds.
            See android.view.Gravity
         * @param gravity the gravity
         */
        public void setGravity(int gravity) {
            mBitmapState.mGravity = gravity;
            mApplyGravity = true;
        }所以可以在setProgressDrawable()之前先设置一下gravity,这样就可以描画了。
      

  3.   

    不好意思,没太看明白,可不可说在说的详细点呀
    如下这个方法是继承谁的呀。我直接加上会出错呀,还有mApplyGravity这个变更设置完传给谁了。
        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            mApplyGravity = true;
        }mBitmapState这个是什么类型,gravity这个要传入什么。
        public void setGravity(int gravity) {
            mBitmapState.mGravity = gravity;
            mApplyGravity = true;
        }
      

  4.   

        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            mApplyGravity = true;
        }
    这个函数是protected,所以不能直接调用,但是看源代码发现在Drawable的setBounds中会调用这个函数。如下:
        /**
         * Specify a bounding rectangle for the Drawable. This is where the drawable
         * will draw when its draw() method is called.
         */
        public void setBounds(int left, int top, int right, int bottom) {
            Rect oldBounds = mBounds;        if (oldBounds == ZERO_BOUNDS_RECT) {
                oldBounds = mBounds = new Rect();
            }        if (oldBounds.left != left || oldBounds.top != top ||
                    oldBounds.right != right || oldBounds.bottom != bottom) {
                mBounds.set(left, top, right, bottom);
                onBoundsChange(mBounds);
            }
        }    /**
         * Specify a bounding rectangle for the Drawable. This is where the drawable
         * will draw when its draw() method is called.
         */
        public void setBounds(Rect bounds) {
            setBounds(bounds.left, bounds.top, bounds.right, bounds.bottom);
        }
    可以调用setBounds()。
    setGravity和view的setgravity是相同的,传入的值为Gravity的静态变量。如Gravity.LEFT等等。
      

  5.   

    十分感谢哈,方法好用
    整理后的结果,以便其它人使用。
    Drawable progressDrawable= his.getResources().getDrawable(R.drawable.ProgressDrawable);
    progressDrawable.setBounds(0, 5, 806, 30);
    seekBar.setProgressDrawable(progressDrawable);
      

  6.   

    帮了大忙了 感谢啊
    google了半天,都是抄的改xml的代码
      

  7.   

    可是有问题啊...
      if (oldBounds.left != left || oldBounds.top != top ||
                    oldBounds.right != right || oldBounds.bottom != bottom) {
                mBounds.set(left, top, right, bottom);
                onBoundsChange(mBounds);
            }
    我要是不想改这些bounds的值,该怎么办呢
      

  8.   

    我也碰到了这个问题,我想知道progressDrawable.setBounds(0, 5, 806, 30);
    里面要传的参数该如何填写
      

  9.   

    progressDrawable = this.getResources().getDrawable(R.drawable.image);
    progressDrawable.setBounds(mSeekBar.getProgressDrawable().getBounds());
    mSeekBar.setProgressDrawable(progressDrawable);这样试试 
      

  10.   

    progressDrawable = this.getResources().getDrawable(R.drawable.image);
    progressDrawable.setBounds(mSeekBar.getProgressDrawable().getBounds());
    mSeekBar.setProgressDrawable(progressDrawable);
    这样确实能够正常载入,但是变成了整个拖动条都变成了 载入的图片(连滑块没到达的部分都显示出来)
    请问如何解决?
      

  11.   

    progressDrawable = this.getResources().getDrawable(R.drawable.image);
    progressDrawable.setBounds(mSeekBar.getProgressDrawable().getBounds());
    mSeekBar.setProgressDrawable(progressDrawable);
    mSeekBar.setProgress(0);progressbar是进度如果没有发生变化, 是不会刷新图片的,所以选把进度设置为0试试.
      

  12.   

    我做的是进度条在拖动之后他的背景图片要变得,我的xml代码是
    <layer-list>
    <item android:id="@android:id/background" android:drawable="@drawable/slider_bg"
             />
          <item android:id="@+android:id/SecondaryProgress" android:drawable="@drawable/slider_bg" />
         <item android:id="@android:id/progress" android:drawable="@drawable/pro4" />
    </layer-list>  
    代码里面setProgressDrawable()这个方法调用这个布局之后显示的是只有一张图片,这是什么原因?我之前在xml里面调用这个布局 两张背景图都能出来   就是在拖动之后显示的是另外一张图片   ,但是为什么在代码里面就初步俩效果呢? 这张图片时我在代码里面调用这个布局的效果 ,我想要的效果是拖动之前的效果是下面那张灰色的背景图片
      

  13.   

    我现在也遇到这样的问题了。我要实现电量的显示,我就用了progressbar.我注册了一个广播,电量改变的时候就改变progressbar的颜色,可是现在颜色出不来啊。用了下面的代码也不起作用。
    progressDrawable = this.getResources().getDrawable(R.drawable.image);
    progressDrawable.setBounds(mSeekBar.getProgressDrawable().getBounds());
    mSeekBar.setProgressDrawable(progressDrawable);是怎么回事呢?
      

  14.   

    你们都错啦,改变颜色不是setProgressDrawable,而是setBackgroundResource(int resid),Progress跟ImageView不一样的。