安卓:自定义绘制对象的搜索栏小工具是借鉴混叠图像 [英] Android: Custom Drawable in a SeekBar widget is drawing aliased images

查看:182
本文介绍了安卓:自定义绘制对象的搜索栏小工具是借鉴混叠图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用的是自定义的progressDrawable在搜索栏小工具(也许不正确),它是在一个非常别名/丑/低质量的形式呈现的问​​题是什么。我设置了drawingCacheQuality,以高,没有什​​么区别。任何人有什么想法?

正在开发一个的Nexus One(OS 2.2.1)与目标构建项目作为API 1.5(虽然我希望不应该的问题)。

下面是什么样子的例子:

我的目标是创建一个简单漂亮的视觉开关两种状态(开和关),以及一个滑块它们之间移动。我并没有真正看到一个更好的部件,这和几乎一切都对我有搜索栏进行。如果有人有一个更好的主意,不涉及锅炉板code重写万吨,这将是很好的。这似乎只是这确实应该是可行的以最小的努力在某种程度上正与或扩展搜索栏。我在一个小的损失从哪里开始,虽然。

我猜一个想法是只覆盖我的ON_OFF形象搬上ShapeDrawable并使用它作为背景(和@android:彩色/透明的progressDrawable),但我不太熟悉如何做到这一点...

基本code新actvitiy

  @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        寻求=(搜索栏)findViewById(R.id.seek);
        seek.setOnSeekBarChangeListener(新SeekBar.OnSeekBarChangeListener(){
            公共无效onProgressChanged(搜索栏搜索栏,INT进步,布尔fromTouch){
            }
            公共无效onStartTrackingTouch(搜索栏搜索栏){
            }
            公共无效onStopTrackingTouch(搜索栏搜索栏){
                如果(seek.getProgress()&其中; seek.getMax()/ 2.0)
                    seek.setProgress(0);
                其他
                    seek.setProgress(seek.getMax());
            }
        });
    }  

main.xml中定义搜索栏

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:后台=#FFFFFF>
    <搜索栏机器人:ID =@ + ID /搜索
                机器人:layout_alignParentTop =真
                机器人:layout_centerHorizo​​ntal =真
                机器人:layout_height =WRAP_CONTENT机器人:layout_width =200dip
                机器人:背景=@可绘制/背景
                机器人:填充=4dip
                机器人:drawingCacheQuality =高
                机器人:progressDrawable =@可绘制/ slider_on_off
                机器人:进度=99的Andr​​oid版本:最大=99
                机器人:=了maxHeight100dip
                机器人:拇指=@可绘制/ slider_box
                机器人:thumbOffset =0dip
                />
< / RelativeLayout的>
 

搜索栏的background.xml背景

 < XML版本=1.0编码=UTF-8&GT?;
<形状
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:形状=矩形
    机器人:useLevel =假>
    <角落
        机器人:半径=4dip/>
    [固体
        机器人:颜色=#FF282828/>
< /形状>
 

解决方案

绘图缓存品质与你正在尝试做什么都没关系。请确保你把你的图片在res /绘制 - 华电国际的文件夹。

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:

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>

解决方案

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.

这篇关于安卓:自定义绘制对象的搜索栏小工具是借鉴混叠图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆