Android 自定义搜索栏渲染 [英] Android custom seekbar render

查看:53
本文介绍了Android 自定义搜索栏渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用程序创建自定义 seekbar.我想得到的是:

I'm trying to create a custom seekbar for my application. Waht I'd like to get is that:

到目前为止我得到了什么:

What I got so far:

我为 Thumb

<?xml version="1.0" encoding="UTF-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="oval">

  <solid android:color="#16A085"/>

  <size 
    android:height="30dp"
    android:width="30dp" />

</shape>

对于酒吧来说:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+android:id/SecondaryProgress">
        <shape android:shape="rectangle">
            <solid android:color="#EBEDEF" />
            <corners android:radius="35dp" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <shape android:shape="rectangle">
            <padding android:left="10dp" android:top="20dp" android:right="10dp" android:bottom="20dp" />
            <solid android:color="#1ABC9C" />
            <corners android:radius="15dp" />
        </shape>
    </item>
</layer-list>

我该怎么做才能获得真正的"搜索栏?

What can I do to achieve to get the "real" seekbar ?

感谢您的帮助.

推荐答案

我找到了另一种实现自定义搜索栏的方法.这是我目前使用的代码

I've found another way to achieve to make my custom seekbar. Here is the code I'm currently using

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.util.AttributeSet;
import android.view.Gravity;

public class FlatSeekBar extends android.widget.SeekBar
{

    private int size = 20;

    public FlatSeekBar(Context context)
    {
        super(context);
        init(null, true);
    }

    public FlatSeekBar(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(attrs, true);
    }

    public FlatSeekBar(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(attrs, true);
    }

    private void init(AttributeSet attrs, boolean applyAttributeTheme)
    {
        // setting thumb
        PaintDrawable thumb = new PaintDrawable(Color.parseColor("#2C3E50"));
        thumb.setCornerRadius(size * 9 / 8);
        thumb.setIntrinsicWidth(size * 9 / 4);
        thumb.setIntrinsicHeight(size * 9 / 4);
        setThumb(thumb);

        // progress
        PaintDrawable progress = new PaintDrawable(Color.parseColor("#34495E"));
        progress.setCornerRadius(size);
        progress.setIntrinsicHeight(size);
        progress.setIntrinsicWidth(size);
        progress.setDither(true);
        ClipDrawable progressClip = new ClipDrawable(progress, Gravity.LEFT, ClipDrawable.HORIZONTAL);

        // secondary progress
        PaintDrawable secondary = new PaintDrawable(Color.parseColor("#EBEDEF"));
        secondary.setCornerRadius(size);
        secondary.setIntrinsicHeight(size);
        ClipDrawable secondaryProgressClip = new ClipDrawable(secondary, Gravity.LEFT, ClipDrawable.HORIZONTAL);

        // background
        PaintDrawable background = new PaintDrawable(Color.parseColor("#EBEDEF"));
        background.setCornerRadius(size);
        background.setIntrinsicHeight(size);

        // applying drawable
        LayerDrawable ld = (LayerDrawable) getProgressDrawable();
        ld.setDrawableByLayerId(android.R.id.background, background);
        ld.setDrawableByLayerId(android.R.id.progress, progressClip);
        ld.setDrawableByLayerId(android.R.id.secondaryProgress, secondaryProgressClip);
    }
}

只需编辑这四种颜色即可获得您自己的颜色:

Just edit those four colours to get your own:

  • #2C3E50
  • #34495E
  • #EBEDEF
  • #EBEDEF

我希望它有帮助;)

这篇关于Android 自定义搜索栏渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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