Android:SeekBar 小部件中的自定义 Drawable 正在绘制别名图像

2022-01-18 00:00:00 widget android java seekbar

问题是当我在 SeekBar 小部件中使用自定义 progressDrawable(可能不正确)时,它会以非常混叠/丑陋/低质量的形式呈现.我已将drawingCacheQuality"设置为高,没有区别.有人有什么想法吗?

Problem is when I am using a custom progressDrawable in a SeekBar widget (perhaps incorrectly) it is rendered in a very aliased/ugly/low quality form. I have set "drawingCacheQuality" to high and there is no difference. Anyone have any ideas?

我在 Nexus One (OS 2.2.1) 上开发,目标构建为 API 1.5(尽管我希望这不重要).

Am developing on a Nexus One (OS 2.2.1) with target build for project as API 1.5 (though I would hope that shouldn't matter).

这是它的外观示例:

我的目标是创建一个具有两种状态(开和关)的简单漂亮的视觉开关,以及在它们之间移动的滑块.我真的没有看到更好的小部件,而且几乎所有的事情都是用 SeekBar 为我完成的.如果有人有一个更好的主意,不涉及重写大量的样板代码,那就太好了.看起来这真的应该以最小的努力以某种方式使用或扩展 SeekBar 是可行的.不过我有点不知所措.

My aim is to create a simple nifty visual switch with two states (on and off), and a slider to move between them. I didn't really see a better widget for this and practically everything is done for me with SeekBar. If someone has a better idea which doesn't involve rewriting tons of boiler plate code that would be nice. It just seems like this should really be doable with minimal effort somehow working with or extending SeekBar. I'm at a bit of a loss where to start though.

我猜一个想法是将我的 on_off 图像叠加到 ShapeDrawable 上并将其用作背景(以及用于 progressDrawable 的@android:color/transparent"),但我不太熟悉如何这样做...

I'm guessing one idea would be to just overlay my on_off image onto the ShapeDrawable and use that as the background (and "@android:color/transparent" for progressDrawable), but I'm not too familiar with how to do that...

新活动的基本代码

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

        seek = (SeekBar)findViewById(R.id.seek);
        seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
            }
            public void onStartTrackingTouch(SeekBar seekBar) { 
            }
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (seek.getProgress() < seek.getMax() / 2.0)
                    seek.setProgress(0);
                else
                    seek.setProgress(seek.getMax());
            }
        });
    }

定义 SeekBar 的 main.xml

main.xml defining the SeekBar

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">
    <SeekBar android:id="@+id/seek"
                android:layout_alignParentTop="true" 
                android:layout_centerHorizontal="true"
                android:layout_height="wrap_content" android:layout_width="200dip" 
                android:background="@drawable/background"
                android:padding="4dip" 
                android:drawingCacheQuality="high" 
                android:progressDrawable="@drawable/slider_on_off"  
                android:progress="99" android:max="99" 
                android:maxHeight="100dip"
                android:thumb="@drawable/slider_box"
                android:thumbOffset="0dip"
                />
</RelativeLayout>

background.xml seekbar 的背景

background.xml Background of seekbar

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" 
    android:useLevel="false" >
    <corners 
        android:radius="4dip" />
    <solid
        android:color="#FF282828" />
</shape>

推荐答案

绘图缓存质量与您要执行的操作无关.确保将图像放在 res/drawable-hdpi 文件夹中.

The drawing cache quality has no relation with what you are trying to do. Make sure that you put your images in the res/drawable-hdpi folder.

相关文章