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

发布于 2024-10-12 05:37:08 字数 2719 浏览 1 评论 0原文

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

我正在 Nexus One(操作系统 2.2.1)上进行开发,项目的目标构建为 API 1.5(尽管我希望这不重要)。

下面是它的示例:

alt text

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

我猜测一个想法是将我的 on_off 图像覆盖到 ShapeDrawable 上并使用它作为背景(以及“@android:color/transparent”用于 ProgressDrawable),但我不太熟悉如何做到这一点。 ..

的新 actvitiy main.xml 的基本代码

    @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

<?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 搜索栏的背景

<?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>

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?

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).

Here is a sample of what it looks like:

alt text

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.

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...

Basic code for a new actvitiy

    @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());
            }
        });
    }

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 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第几種人 2024-10-19 05:37:08

绘图缓存质量与您想要执行的操作无关。确保将图像放入 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.

番薯 2024-10-19 05:37:08

嗯,这篇文章已经很老了,但是有一种方法可以用切换按钮来做到这一点。

本页对此进行了很好的描述: http:// www.mokasocial.com/2011/07/sexily-styled-toggle-buttons-for-android/

如果有人尝试做类似的事情,请看看那里。

Well, the post its pretty old but there's a way to do it with togglebuttons.

Its VERY WELL described in this page: http://www.mokasocial.com/2011/07/sexily-styled-toggle-buttons-for-android/

If anyone is trying to do something like that, take a look there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文