微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SeekBar可绘制对象在被Adapter复用后未绘制

如何解决SeekBar可绘制对象在被Adapter复用后未绘制

| 我需要在
ListView
行中将覆盖图像绘制到paint0ѭ上。这并不是很困难,此问题中的代码通过在位图中绘制行号并将其放置为可绘制进度来实现这一点。 我遇到的问题是,这只能运行一次。当视图被回收以用于另一行时,可绘制对象消失。 前10个视图(位置0到9)有效。之后的一切都会失败。没有错误或异常。 Android只是停止绘制该图层。如果我向下滚动然后又向上备份,它们都不起作用(因为它们都已被回收)。 如何让Android始终绘制此叠加图像?我想我需要执行某种缓存或无效操作,但我还不知道它是什么。
public class ViewTest extends ListActivity {
    // replace the progress drawable with my custom drawable
    public void setSeekBarOverlay(SeekBar seekBar,String overlayText) {
        Bitmap bitmap = Bitmap.createBitmap(100,12,Bitmap.Config.ARGB_4444);
        new Canvas(bitmap).drawText(overlayText,10,new Paint());

        Drawable d = new BitmapDrawable(bitmap);
        ((LayerDrawable) seekBar.getProgressDrawable()).setDrawableByLayerId(android.R.id.progress,d);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setlistadapter(new listadapter() {

            // return the view. Recycle them if possible.
            public View getView(int pos,View v,ViewGroup vg) {
                if (v == null) {
                    v = View.inflate(ViewTest.this,R.layout.seekbar,null);
                }

                String s = String.valueOf(pos);
                ((TextView) v.findViewById(android.R.id.text1)).setText(s);
                setSeekBarOverlay((SeekBar) v.findViewById(R.id.seekbar),s);
                return v;
            }

            ... cut ...
        });
    }
}
main.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<ListView
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:id=\"@android:id/list\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\"/>
seekbar.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"wrap_content\"
    android:gravity=\"center_vertical\"
    android:minHeight=\"?android:attr/listPreferredItemHeight\">

    <TextView
        android:id=\"@android:id/text1\"
        android:textAppearance=\"?android:attr/textAppearanceLarge\"
        android:layout_width=\"0dp\"
        android:layout_weight=\"1\"
        android:gravity=\"center\"
        android:layout_height=\"wrap_content\" />
    <SeekBar
        android:id=\"@+id/seekbar\"
        android:layout_width=\"0dp\"
        android:layout_weight=\"1\"
        android:layout_height=\"wrap_content\" />
</LinearLayout>
    

解决方法

        这是一个艰巨的任务,我认为不会带来太多流量,因此自找到解决方案以来,我将自己回答。 另一个SO问题/答案Android ProgressBar.setProgressDrawable仅能工作一次?让我中途了,但是解决方案对我却不起作用。在另一个问题中,代码试图替换整个progressDrawable(
setProgressDrawable()
)。我在这里不做这件事,因为我只是在更新现有progressDrawable中的图层。经过一些类似的尝试,我发现了一些可行的方法。您必须获取要更新的可绘制对象的边界(在本例中为图层之一),然后再设置边界。 似乎是平台中的错误,但是此替代方法似乎可以使其正常工作。希望有一天能对别人有所帮助。
public void setSeekBarOverlay(SeekBar seekBar,String overlayText) {
    LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
    Drawable overlayDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress);
    Rect bounds = overlayDrawable.getBounds();

    Bitmap bitmap = Bitmap.createBitmap(100,12,Bitmap.Config.ARGB_4444);
    new Canvas(bitmap).drawText(overlayText,10,new Paint());
    overlayDrawable = new BitmapDrawable(bitmap);

    overlayDrawable.setBounds(bounds);
    layerDrawable.setDrawableByLayerId(android.R.id.progress,overlayDrawable);
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。